applicant-site/app/Http/Controllers/admin/Catalog/Direction/SubjectController.php

112 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog\Direction;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\Direction\StoreSubjectRequest;
use App\Http\Requests\admin\Catalog\Direction\UpdateSubjectRequest;
use App\Models\Subject;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class SubjectController extends Controller
{
public function index(): View
{
$subjects = Subject::all();
return view('admin.catalog.direction.subject.index', compact('subjects'));
}
public function create(): View
{
return view('admin.catalog.direction.subject.create');
}
public function store(StoreSubjectRequest $request): RedirectResponse
{
$validated = $request->validated();
$slug = SlugHelper::get($validated);
$subject = new Subject();
$subject->name = $validated['name'];
$subject->description = $validated['description'];
$subject->slug = $slug;
$subject->position = $validated['position'];
$subject->save();
Log::channel('app')
->info(
'CREATE предмет {subject} - user {user}',
['user' => Auth::user()->name, 'faculty' => $subject->name, 'data' => $validated]
);
return redirect()->route('subjects.index');
}
public function show(Subject $subject): View
{
return view('admin.catalog.direction.subject.show', compact('subject'));
}
public function edit(Subject $subject): View
{
return view('admin.catalog.direction.subject.edit', compact('subject'));
}
public function update(UpdateSubjectRequest $request, Subject $subject): RedirectResponse
{
$validated = $request->validated();
$oldData = $subject->toArray();
$subject->name = $validated['name'];
$subject->description = $validated['description'];
$subject->slug = $validated['slug'];
$subject->position = $validated['position'];
Log::channel('app')
->warning(
'UPDATE предмет {subject} - user {user}',
[
'user' => Auth::user()->name,
'subject' => $subject->name,
'oldData' => $oldData,
'newData' => $validated
]
);
$subject->save();
return redirect()->route('subjects.index');
}
public function destroy(Subject $subject): RedirectResponse
{
if ($subject->entranceExaminations()->exists()) {
Log::channel('app')
->error(
'NOT DELETE предмет {subject} - user {user}',
[
'user' => Auth::user()->name,
'subject' => $subject->name,
'data' => $subject->toArray(),
]
);
return back();
}
Log::channel('app')
->critical(
'DELETE предмет {subject} - user {user}',
[
'user' => Auth::user()->name,
'subject' => $subject->name,
'data' => $subject->toArray(),
]
);
$subject->delete();
return redirect()->route('subjects.index');
}
}