applicant-site/app/Http/Controllers/admin/Catalog/FacultyController.php

120 lines
4.2 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreFacultyRequest;
use App\Http\Requests\admin\Catalog\UpdateFacultyRequest;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class FacultyController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::all();
return view('admin.catalog.faculty.index', compact('faculties'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
return view('admin.catalog.faculty.create', compact('educationalInstitutions'));
}
public function store(StoreFacultyRequest $request): RedirectResponse
{
$validated = $request->validated();
$slug = SlugHelper::get($validated);
$faculty = new Faculty();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $slug;
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();
Log::channel('app')
->info(
'CREATE факультет {faculty} - user {user}',
['user' => Auth::user()->name, 'faculty' => $faculty->name, 'data' => $validated]
);
return redirect()->route('faculties.index');
}
public function show(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('admin.catalog.faculty.show', compact('faculty'));
}
public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
return view('admin.catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
}
public function update(UpdateFacultyRequest $request, Faculty $faculty): RedirectResponse
{
$validated = $request->validated();
$oldData = $faculty->toArray();
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $validated['slug'];
$faculty->educational_institution_id = $validated['educational_institution_id'];
Log::channel('app')
->warning(
'UPDATE факультет {faculty} - user {user}',
[
'user' => Auth::user()->name,
'faculty' => $faculty->name,
'oldData' => $oldData,
'newData' => $validated
]
);
$faculty->save();
return redirect()->route('faculties.index');
}
public function destroy(Faculty $faculty): RedirectResponse
{
if ($faculty->departments()->exists()) {
Log::channel('app')
->critical(
'NOT DELETE факультет {faculty} - user {user}',
[
'user' => Auth::user()->name,
'faculty' => $faculty->name,
'data' => $faculty->toArray(),
]
);
return back();
}
Log::channel('app')
->critical(
'DELETE факультет {faculty} - user {user}',
[
'user' => Auth::user()->name,
'faculty' => $faculty->name,
'data' => $faculty->toArray(),
]
);
$faculty->delete();
return redirect()->route('faculties.index');
}
}