CourseWork/app/Http/Controllers/SubjectController.php
2024-06-23 00:06:14 +04:00

110 lines
2.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\SubjectPostRequest;
use App\Models\Subject;
use App\Services\SubjectService;
use App\Services\FileService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class SubjectController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(SubjectService $service): View
{
return view('subjects.index', [
'subjects' => $service->getSubjects(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
if(request()->user()->cannot('create', Subject::class)) {
abort(403);
}
return view('subjects.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(SubjectPostRequest $request): RedirectResponse
{
if(request()->user()->cannot('create', Subject::class)) {
abort(403);
}
return redirect()->route(
'subjects.show',
Subject::create($request->validated()),
);
}
/**
* Display the specified resource.
*/
public function show(Subject $subject): View
{
return view('subjects.show', [
'subject' => $subject,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Subject $subject): View
{
if(request()->user()->cannot('update', $subject)) {
abort(403);
}
return view('subjects.edit', [
'subject' => $subject,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(SubjectPostRequest $request, Subject $subject): RedirectResponse
{
if(request()->user()->cannot('update', $subject)) {
abort(403);
}
return redirect()->route(
'subjects.show',
$subject->update($request->validated())
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Subject $subject): RedirectResponse
{
if(request()->user()->cannot('delete', $subject)) {
abort(403);
}
$subject->delete();
return redirect()->route('subjects.index');
}
public function exportToPDF(FileService $fileService)
{
return $fileService->exportSubjects();
}
}