CourseWork/app/Http/Controllers/GradeController.php

114 lines
2.6 KiB
PHP
Raw Normal View History

2024-04-16 13:27:51 +04:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\GradePostRequest;
use App\Models\Grade;
2024-06-24 00:51:05 +04:00
use App\Services\FileService;
use App\Services\GradeService;
2024-04-17 10:11:31 +04:00
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
2024-04-16 13:27:51 +04:00
class GradeController extends Controller
{
/**
* Display a listing of the resource.
*/
2024-06-24 00:51:05 +04:00
public function index(GradeService $service): View
2024-04-16 13:27:51 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('viewAny', Grade::class)) {
abort(403);
}
2024-04-16 13:27:51 +04:00
return view('grades.index', [
2024-06-24 00:51:05 +04:00
'grades' => $service->getGrades(),
2024-04-16 13:27:51 +04:00
]);
}
/**
* Show the form for creating a new resource.
*/
2024-04-17 10:11:31 +04:00
public function create(): View
2024-04-16 13:27:51 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('create', Grade::class)) {
abort(403);
}
2024-04-16 13:27:51 +04:00
return view('grades.create');
}
/**
* Store a newly created resource in storage.
*/
2024-04-17 10:11:31 +04:00
public function store(GradePostRequest $request): RedirectResponse
2024-04-16 13:27:51 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('create', Grade::class)) {
abort(403);
}
2024-06-16 00:41:31 +04:00
return redirect()->route('grades.show', Grade::create($request->validated()));
2024-04-16 13:27:51 +04:00
}
/**
* Display the specified resource.
*/
2024-04-17 10:11:31 +04:00
public function show(Grade $grade): View
2024-04-16 13:27:51 +04:00
{
2024-06-22 23:59:16 +04:00
if(request()->user()->cannot('view', $grade)) {
abort(403);
}
2024-04-16 13:27:51 +04:00
return view('grades.show', [
2024-05-06 17:51:16 +04:00
'grade' => $grade,
2024-05-07 12:14:18 +04:00
'subjects' => $grade->subjects,
2024-04-16 13:27:51 +04:00
]);
}
/**
* Show the form for editing the specified resource.
*/
2024-04-17 10:11:31 +04:00
public function edit(Grade $grade): View
2024-04-16 13:27:51 +04:00
{
2024-06-25 00:52:48 +04:00
if(request()->user()->cannot('update', $grade)) {
2024-06-22 23:59:16 +04:00
abort(403);
}
2024-04-16 13:27:51 +04:00
return view('grades.edit', [
'grade' => $grade,
]);
}
/**
* Update the specified resource in storage.
*/
2024-04-17 10:11:31 +04:00
public function update(GradePostRequest $request, Grade $grade): RedirectResponse
2024-04-16 13:27:51 +04:00
{
2024-06-25 00:52:48 +04:00
if(request()->user()->cannot('update', $grade)) {
2024-06-22 23:59:16 +04:00
abort(403);
}
2024-06-25 00:52:48 +04:00
$grade->update($request->validated());
return redirect()->route('grades.show', $grade);
2024-04-16 13:27:51 +04:00
}
/**
* Remove the specified resource from storage.
*/
2024-04-17 10:11:31 +04:00
public function destroy(Grade $grade): RedirectResponse
2024-04-16 13:27:51 +04:00
{
2024-06-25 00:52:48 +04:00
if(request()->user()->cannot('delete', $grade)) {
2024-06-22 23:59:16 +04:00
abort(403);
}
2024-06-25 00:52:48 +04:00
2024-06-16 00:41:31 +04:00
$grade->delete();
2024-04-16 13:27:51 +04:00
return redirect()->route('grades.index');
}
2024-06-24 00:51:05 +04:00
public function listStudents(Grade $grade, FileService $fileService)
{
return $fileService->exportStudents($grade);
}
2024-04-16 13:27:51 +04:00
}