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

122 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers\admin\Catalog;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreDepartmentRequest;
use App\Http\Requests\admin\Catalog\UpdateDepartmentRequest;
use App\Models\Department;
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;
class DepartmentController extends Controller
{
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$departments = Department::all();
return view('admin.catalog.department.index', compact('departments'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
return view('admin.catalog.department.create', compact('faculties'));
}
public function store(StoreDepartmentRequest $request): RedirectResponse
{
$validated = $request->validated();
$slug = SlugHelper::get($validated);
$department = new Department();
$department->name = $validated['name'];
$department->description = $validated['description'];
$department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id'];
$department->slug = $slug;
$department->save();
Log::channel('app')
->info(
'CREATE кафедра {department} - user {user}',
['user' => Auth::user()->name, 'department' => $department->name, 'data' => $validated]
);
return redirect()->route('departments.index');
}
public function show(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculty = Faculty::find($department->faculty->id);
$educationalInstitution = $faculty->educationalInstitution;
return view('admin.catalog.department.show', compact('department', 'faculty', 'educationalInstitution'));
}
public function edit(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
return view('admin.catalog.department.edit', compact('department', 'faculties'));
}
public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse
{
$validated = $request->validated();
$oldData = $department->toArray();
$department->name = $validated['name'];
$department->description = $validated['description'];
$department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id'];
$department->slug = $validated['slug'];
Log::channel('app')
->warning(
'UPDATE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'oldData' => $oldData,
'newData' => $validated
]
);
$department->save();
return redirect()->route('departments.index');
}
public function destroy(Department $department): RedirectResponse
{
if ($department->directions()->exists()) {
Log::channel('app')
->error(
'NOT DELETE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'data' => $department->toArray(),
]
);
return back();
}
Log::channel('app')
->critical(
'DELETE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'data' => $department->toArray(),
]
);
$department->delete();
return redirect()->route('departments.index');
}
}