CourseWork/app/Http/Controllers/StudentController.php

93 lines
2.1 KiB
PHP
Raw Normal View History

2024-04-23 14:52:49 +04:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StudentPostRequest;
use App\Models\Grade;
use App\Models\Student;
use App\Services\ServiceInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class StudentController extends Controller
{
public function __construct(
2024-05-06 17:47:25 +04:00
protected ServiceInterface $service
2024-05-06 17:51:16 +04:00
) {
}
2024-04-23 14:52:49 +04:00
/**
* Display a listing of the resource.
*/
public function index(): View
{
return view('students.index', [
2024-05-06 17:47:25 +04:00
'students' => $this->service->getAll(),
2024-04-23 14:52:49 +04:00
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('students.create', [
'grades' => Grade::all(),
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(StudentPostRequest $request): RedirectResponse
{
return redirect()->route(
'students.show',
2024-05-06 17:47:25 +04:00
$this->service->create($request->validated())
2024-04-23 14:52:49 +04:00
);
}
/**
* Display the specified resource.
*/
public function show(Student $student): View
{
return view('students.show', [
'student' => $student,
'grades' => Grade::all(),
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Student $student): View
{
return view('students.edit', [
'student' => $student,
'grades' => Grade::all(),
]);
}
/**
* Update the specified resource in storage.
*/
public function update(StudentPostRequest $request, Student $student): RedirectResponse
{
return redirect()->route(
'students.show',
2024-05-06 17:47:25 +04:00
$this->service->update($student, $request->validated())
2024-04-23 14:52:49 +04:00
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Student $student): RedirectResponse
{
2024-05-06 17:47:25 +04:00
$this->service->delete($student);
2024-04-23 14:52:49 +04:00
return redirect()->route('students.index');
}
}