88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\ApiService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class DirectionController extends Controller
|
|
{
|
|
protected ApiService $api;
|
|
|
|
public function __construct(ApiService $api)
|
|
{
|
|
$this->api = $api;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$response = $this->api->get('/employee/directions');
|
|
|
|
if ($response->successful()) {
|
|
$directions = $response->json();
|
|
return view('directions.index', compact('directions'));
|
|
}
|
|
|
|
abort($response->status());
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$departments = $this->api->get('/employee/departments')->json();
|
|
return view('directions.form', ['departments' => $departments]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$response = $this->api->post('/employee/directions', $request->all());
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('directions.index')
|
|
->with('success', 'Направление успешно создано');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors'] ?? []);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$response = $this->api->get("/employee/directions/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
$direction = $response->json();
|
|
$departments = $this->api->get('/employee/departments')->json();
|
|
return view('directions.form', [
|
|
'direction' => $direction,
|
|
'departments' => $departments,
|
|
'isEdit' => true
|
|
]);
|
|
}
|
|
|
|
abort($response->status());
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$response = $this->api->patch("/employee/directions/{$id}", $request->all());
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('directions.index')
|
|
->with('success', 'Данные направления обновлены');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors'] ?? []);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$response = $this->api->delete("/employee/directions/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('directions.index')
|
|
->with('success', 'Направление удалено');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['error'] ?? 'Ошибка при удалении');
|
|
}
|
|
}
|