add bootstrap validation and Logging for Department

This commit is contained in:
aslan 2024-03-13 10:56:14 +03:00
parent 4e2d8bec4b
commit 5fa4b20ec7
8 changed files with 153 additions and 34 deletions

View File

@ -60,4 +60,10 @@ class PositionHelper
$maxPosition = EntranceExamination::max('position'); $maxPosition = EntranceExamination::max('position');
return $maxPosition >= 254 ? 255 : $maxPosition + 1; return $maxPosition >= 254 ? 255 : $maxPosition + 1;
} }
public static function department()
{
$maxPosition = EntranceExamination::max('position');
return $maxPosition >= 254 ? 255 : $maxPosition + 1;
}
} }

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\admin\Catalog; namespace App\Http\Controllers\admin\Catalog;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\StoreDepartmentRequest; use App\Http\Requests\admin\Catalog\StoreDepartmentRequest;
use App\Http\Requests\admin\Catalog\UpdateDepartmentRequest; use App\Http\Requests\admin\Catalog\UpdateDepartmentRequest;
@ -11,6 +12,8 @@ use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class DepartmentController extends Controller class DepartmentController extends Controller
{ {
@ -30,6 +33,8 @@ class DepartmentController extends Controller
{ {
$validated = $request->validated(); $validated = $request->validated();
$slug = SlugHelper::get($validated);
$department = new Department(); $department = new Department();
$department->name = $validated['name']; $department->name = $validated['name'];
$department->description = $validated['description']; $department->description = $validated['description'];
@ -38,6 +43,12 @@ class DepartmentController extends Controller
$department->slug = $validated['slug']; $department->slug = $validated['slug'];
$department->save(); $department->save();
Log::channel('app')
->info(
'CREATE кафедра {department} - user {user}',
['user' => Auth::user()->name, 'department' => $department->name, 'data' => $validated]
);
return redirect()->route('departments.index'); return redirect()->route('departments.index');
} }
@ -57,12 +68,25 @@ class DepartmentController extends Controller
public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse
{ {
$validated = $request->validated(); $validated = $request->validated();
$oldData = $department->toArray();
$department->name = $validated['name']; $department->name = $validated['name'];
$department->description = $validated['description']; $department->description = $validated['description'];
$department->position = $validated['position']; $department->position = $validated['position'];
$department->faculty_id = $validated['faculty_id']; $department->faculty_id = $validated['faculty_id'];
$department->slug = $validated['slug']; $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(); $department->save();
return redirect()->route('departments.index'); return redirect()->route('departments.index');
@ -71,8 +95,26 @@ class DepartmentController extends Controller
public function destroy(Department $department): RedirectResponse public function destroy(Department $department): RedirectResponse
{ {
if ($department->directions()->exists()) { 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(); return back();
} }
Log::channel('app')
->critical(
'DELETE кафедра {department} - user {user}',
[
'user' => Auth::user()->name,
'department' => $department->name,
'data' => $department->toArray(),
]
);
$department->delete(); $department->delete();
return redirect()->route('departments.index'); return redirect()->route('departments.index');
} }

View File

@ -17,9 +17,30 @@ class StoreDepartmentRequest extends FormRequest
return [ return [
'position' => 'required|int|numeric|max:255', 'position' => 'required|int|numeric|max:255',
'name' => 'required|string|max:255|unique:departments,name', 'name' => 'required|string|max:255|unique:departments,name',
'description' => 'string', 'description' => 'nullable|string',
'slug' => 'required|string|max:255|unique:departments,slug', 'slug' => 'nullable|string|max:255|unique:departments,slug',
'faculty_id' => 'required|numeric|int|max:1000', 'faculty_id' => 'required|numeric|int|max:1000',
]; ];
} }
public function messages(): array
{
return [
'position.required' => 'Поле позиция обязательно.',
'position.int' => 'Позиция должно быть целым числом.',
'position.numeric' => 'Позиция должно быть числом.',
'position.max' => 'Позиция не должен быть больше :max',
'name.required' => 'Поле название обязательно.',
'name.string' => 'Поле название должен быть строкой.',
'name.max' => 'Поле название не должен превышать :max символов.',
'name.unique' => 'Название уже занят.',
'description.string' => 'Поле описание должен быть строкой.',
'slug.string' => 'Поле URL должен быть строкой.',
'slug.max' => 'Поле URL не должен превышать :max символов.',
'faculty_id.required' => 'Поле учебное заведение обязательно.',
'faculty_id.int' => 'Учебное заведение должно быть целым числом.',
'faculty_id.numeric' => 'Учебное заведение должно быть числом.',
'faculty_id.max' => 'Поле учебное заведение не должен быть больше :max.',
];
}
} }

View File

@ -14,7 +14,7 @@ class UpdateDepartmentRequest extends FormRequest
{ {
return [ return [
'position' => 'required|int|numeric|max:255', 'position' => 'required|int|numeric|max:255',
'description' => 'string', 'description' => 'nullable|string',
'slug' => [ 'slug' => [
'string', 'string',
'required', 'required',
@ -30,4 +30,25 @@ class UpdateDepartmentRequest extends FormRequest
], ],
]; ];
} }
public function messages(): array
{
return [
'position.required' => 'Поле позиция обязательно.',
'position.int' => 'Позиция должно быть целым числом.',
'position.numeric' => 'Позиция должно быть числом.',
'position.max' => 'Позиция не должен быть больше :max',
'name.required' => 'Поле название обязательно.',
'name.string' => 'Поле название должен быть строкой.',
'name.max' => 'Поле название не должен превышать :max символов.',
'name.unique' => 'Название уже занят.',
'description.string' => 'Поле описание должен быть строкой.',
'slug.string' => 'Поле URL должен быть строкой.',
'slug.max' => 'Поле URL не должен превышать :max символов.',
'faculty_id.required' => 'Поле учебное заведение обязательно.',
'faculty_id.int' => 'Учебное заведение должно быть целым числом.',
'faculty_id.numeric' => 'Учебное заведение должно быть числом.',
'faculty_id.max' => 'Поле учебное заведение не должен быть больше :max.',
];
}
} }

View File

@ -75,4 +75,11 @@ return [
'description' => 'Поле "Описание" может быть пустым для отображения на сайте', 'description' => 'Поле "Описание" может быть пустым для отображения на сайте',
'slug' => 'Поле "URL" нужно для отображения в браузере' 'slug' => 'Поле "URL" нужно для отображения в браузере'
], ],
'departments' => [
'position' => 'Поле "Позиция" нужно для упорядочивания отображения на сайте',
'name' => 'Поле "Название" должно быть уникальным для отображения на сайте',
'description' => 'Поле "Описание" может быть пустым для отображения на сайте',
'faculty_id' => 'Поле "Факультет" указывает на привязку к факультету',
'slug' => 'Поле "URL" нужно для отображения в браузере'
],
]; ];

View File

@ -1,3 +1,4 @@
@php use App\Helpers\PositionHelper; @endphp
@extends('layouts.admin_layout') @extends('layouts.admin_layout')
@section('content') @section('content')
@auth() @auth()
@ -6,60 +7,71 @@
<h1 class=""> Создать кафедру</h1> <h1 class=""> Создать кафедру</h1>
{{ Form::open(['url' => route('departments.store'), 'method' => 'POST', 'class' => '']) }} {{ Form::open(['url' => route('departments.store'), 'method' => 'POST', 'class' => '']) }}
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
{{ Form::label('position', 'Позиция') }} {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('position', '', ['class' => 'form-control']) }} {{ Form::number('position', PositionHelper::department(), ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position'), 'required']) }}
<div class="invalid-feedback">
Поле "Позиция" обязательно!
</div>
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('position') }} {{ $errors->first('position') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('name', 'Название') }} {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('name', '', ['class' => 'form-control']) }} {{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name'), 'required']) }}
<div class="invalid-feedback">
Поле "Название" обязательно!
</div>
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('name') }} {{ $errors->first('name') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('description', 'Описание') }} {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('description', '', ['class' => 'form-control']) }} {{ Form::text('description', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('description') }} {{ $errors->first('description') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('faculty_id', 'Факультет') }} {{ Form::label('faculty_id', 'Факультет', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::select('faculty_id', $faculties, null, ['class' => 'form-select']) }} {{ Form::select('faculty_id', $faculties, null, ['class' => 'form-select', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('faculty_id') }} {{ $errors->first('faculty_id') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('slug', 'URL') }} {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('slug', '', ['class' => 'form-control']) }} {{ Form::text('slug', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('slug') }} {{ $errors->first('slug') }}
@endif @endif

View File

@ -7,59 +7,68 @@
{{ Form::open(['url' => route('departments.update', $department), 'method' => 'PATCH', 'class' => '']) }} {{ Form::open(['url' => route('departments.update', $department), 'method' => 'PATCH', 'class' => '']) }}
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
{{ Form::label('position', 'Позиция') }} {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('position', $department->position, ['class' => 'form-control']) }} {{ Form::number('position', $department->position, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.position'), 'required']) }}
<div class="invalid-feedback">
Поле "Позиция" обязательно!
</div>
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('position') }} {{ $errors->first('position') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('name', 'Название') }} {{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('name', $department->name, ['class' => 'form-control']) }} {{ Form::text('name', $department->name, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.name'), 'required']) }}
<div class="invalid-feedback">
Поле "Название" обязательно!
</div>
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('name') }} {{ $errors->first('name') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('description', 'Описание') }} {{ Form::label('description', 'Описание', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('description', $department->description, ['class' => 'form-control']) }} {{ Form::text('description', $department->description, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.description')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('description') }} {{ $errors->first('description') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('educational_institution_id', 'Факультет') }} {{ Form::label('faculty_id', 'Факультет', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::select('faculty_id', $faculties, $department->faculty->id, ['class' => 'form-select']) }} {{ Form::select('faculty_id', $faculties, $department->faculty->id, ['class' => 'form-select', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.faculty_id')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('educational_institution_id') }} {{ $errors->first('faculty_id') }}
@endif @endif
</div> </div>
<div class="mt-3"> <div class="mt-3">
{{ Form::label('slug', 'URL') }} {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('slug', $department->slug, ['class' => 'form-control']) }} {{ Form::text('slug', $department->slug, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.departments.slug')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('slug') }} {{ $errors->first('slug') }}
@endif @endif

View File

@ -7,6 +7,7 @@
<h1 class=""> Создать факультет</h1> <h1 class=""> Создать факультет</h1>
{{ Form::open(['url' => route('faculties.store'), 'method' => 'POST', 'class' => 'needs-validation', 'novalidate']) }} {{ Form::open(['url' => route('faculties.store'), 'method' => 'POST', 'class' => 'needs-validation', 'novalidate']) }}
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
{{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.position')]) }} {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.position')]) }}
<span class="text-danger">*</span> <span class="text-danger">*</span>