95 lines
2.7 KiB
PHP
95 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\ApiService;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Request;
|
|
|
|
class StudentController extends Controller
|
|
{
|
|
protected ApiService $api;
|
|
|
|
public function __construct(ApiService $api)
|
|
{
|
|
$this->api = $api;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$responseStudents = $this->api->get('/employee/students');
|
|
$responseGroups = $this->api->get('/employee/groups');
|
|
|
|
if ($responseStudents->successful() && $responseGroups->successful()) {
|
|
$students = $responseStudents->json();
|
|
$groups = $responseGroups->json();
|
|
$groupsList = collect($groups)->pluck('name', 'id')->toArray();
|
|
return view('students.index', compact('students', 'groupsList'));
|
|
}
|
|
|
|
abort($responseStudents->status());
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$groups = $this->api->get('/employee/groups')->json();
|
|
return view('students.form', ['groups' => $groups]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$response = $this->api->post('/employee/students', $request->all());
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('students.index')
|
|
->with('success', 'Студент успешно создан');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors'] ?? []);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$response = $this->api->get("/employee/students/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
$student = $response->json();
|
|
$groups = $this->api->get('/employee/groups')->json();
|
|
return view('students.form', [
|
|
'student' => $student,
|
|
'groups' => $groups,
|
|
'isEdit' => true
|
|
]);
|
|
}
|
|
|
|
abort($response->status());
|
|
}
|
|
|
|
/**
|
|
* @throws ConnectionException
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$response = $this->api->patch("/employee/students/{$id}", $request->all());
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('students.index')
|
|
->with('success', 'Данные студента обновлены');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors'] ?? []);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$response = $this->api->delete("/employee/students/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('students.index')
|
|
->with('success', 'Студент удален');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['error'] ?? 'Ошибка при удалении');
|
|
}
|
|
}
|