CourseWork/app/Http/Controllers/GradeController.php
2024-06-22 23:59:16 +04:00

105 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\GradePostRequest;
use App\Models\Grade;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class GradeController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): View
{
if(request()->user()->cannot('viewAny', Grade::class)) {
abort(403);
}
return view('grades.index', [
'grades' => Grade::filter()->paginate(5)->withQueryString(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
if(request()->user()->cannot('create', Grade::class)) {
abort(403);
}
return view('grades.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(GradePostRequest $request): RedirectResponse
{
if(request()->user()->cannot('create', Grade::class)) {
abort(403);
}
return redirect()->route('grades.show', Grade::create($request->validated()));
}
/**
* Display the specified resource.
*/
public function show(Grade $grade): View
{
if(request()->user()->cannot('view', $grade)) {
abort(403);
}
return view('grades.show', [
'grade' => $grade,
'subjects' => $grade->subjects,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Grade $grade): View
{
if(request()->user()->cannot('update', Grade::class)) {
abort(403);
}
return view('grades.edit', [
'grade' => $grade,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(GradePostRequest $request, Grade $grade): RedirectResponse
{
if(request()->user()->cannot('update', Grade::class)) {
abort(403);
}
return redirect()->route('grades.show', $grade->update($request->validated()));
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Grade $grade): RedirectResponse
{
if(request()->user()->cannot('delete', Grade::class)) {
abort(403);
}
$grade->delete();
return redirect()->route('grades.index');
}
}