CourseWork/app/Http/Controllers/StudentController.php
2024-06-23 17:02:18 +04:00

138 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\StudentPostRequest;
use App\Models\Grade;
use App\Models\Student;
use App\Models\Subject;
use App\Services\StudentService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class StudentController extends Controller
{
public function __construct(
protected StudentService $service
) {
}
/**
* Display a listing of the resource.
*/
public function index(): View
{
if(request()->user()->cannot('viewAny', Student::class)) {
abort(403);
}
return view('students.index', [
'students' => Student::filter()->paginate(5)->withQueryString(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
if(request()->user()->cannot('create', Student::class)) {
abort(403);
}
return view('students.create', [
'grades' => Grade::all(),
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(StudentPostRequest $request): RedirectResponse
{
if(request()->user()->cannot('create', Student::class)) {
abort(403);
}
return redirect()->route(
'students.show',
$this->service->create($request->validated())
);
}
/**
* Display the specified resource.
*/
public function show(Student $student): View
{
if(request()->user()->cannot('view', $student)) {
abort(403);
}
return view('students.show', [
'student' => $student,
'grades' => Grade::all(),
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Student $student): View
{
if(request()->user()->cannot('update', $student)) {
abort(403);
}
return view('students.edit', [
'student' => $student,
'grades' => Grade::all(),
]);
}
/**
* Update the specified resource in storage.
*/
public function update(StudentPostRequest $request, Student $student): RedirectResponse
{
if(request()->user()->cannot('update', $student)) {
abort(403);
}
return redirect()->route(
'students.show',
$this->service->update($student, $request->validated())
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Student $student): RedirectResponse
{
if(request()->user()->cannot('delete', $student)) {
abort(403);
}
$student->user()->delete();
$student->delete();
return redirect()->route('students.index');
}
public function scores(StudentService $service, Subject $subject): View
{
return view('students.scores', [
'lessons' => $service->getScores($subject),
'avgScore' => $service->getAvgScore($subject),
]);
}
public function debts(StudentService $service): View
{
return view('students.debts', [
'lessons' => $service->getDebts(),
]);
}
}