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

118 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\TeacherPostRequest;
use App\Models\Teacher;
use App\Services\TeacherService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class TeacherController extends Controller
{
public function __construct(
protected TeacherService $service
) {
}
/**
* Display a listing of the resource.
*/
public function index(TeacherService $service): View
{
if(request()->user()->cannot('viewAny', Teacher::class)) {
abort(403);
}
return view('teachers.index', [
'teachers' => $service->getTeachers(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
if(request()->user()->cannot('create', Teacher::class)) {
abort(403);
}
return view('teachers.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(TeacherPostRequest $request): RedirectResponse
{
if(request()->user()->cannot('create', Teacher::class)) {
abort(403);
}
return redirect()->route(
'teachers.show',
$this->service->create($request->validated())
);
}
/**
* Display the specified resource.
*/
public function show(Teacher $teacher): View
{
if(request()->user()->cannot('view', $teacher)) {
abort(403);
}
return view('teachers.show', [
'teacher' => $teacher,
'subjects' => $teacher->subjects,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Teacher $teacher): View
{
if(request()->user()->cannot('update', $teacher)) {
abort(403);
}
return view('teachers.edit', [
'teacher' => $teacher,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(TeacherPostRequest $request, Teacher $teacher): RedirectResponse
{
if(request()->user()->cannot('update', $teacher)) {
abort(403);
}
return redirect()->route(
'teachers.show',
$this->service->update($teacher, $request->validated())
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Teacher $teacher): RedirectResponse
{
if(request()->user()->cannot('update', $teacher)) {
abort(403);
}
$teacher->user()->delete();
$teacher->delete();
return redirect()->route('teachers.index');
}
}