add bootsrtap validation and Logging for Examination type

This commit is contained in:
aslan 2024-03-12 14:36:35 +03:00
parent db7704df6e
commit 4eee38fea3
7 changed files with 155 additions and 49 deletions

View File

@ -4,6 +4,7 @@ namespace App\Helpers;
use App\Models\Direction; use App\Models\Direction;
use App\Models\DirectionProfile; use App\Models\DirectionProfile;
use App\Models\ExaminationType;
use App\Models\Faculty; use App\Models\Faculty;
use App\Models\EducationalInstitution; use App\Models\EducationalInstitution;
use App\Models\SubjectType; use App\Models\SubjectType;
@ -46,4 +47,10 @@ class PositionHelper
$maxPosition = Subject::max('position'); $maxPosition = Subject::max('position');
return $maxPosition >= 254 ? 255 : $maxPosition + 1; return $maxPosition >= 254 ? 255 : $maxPosition + 1;
} }
public static function examinationType()
{
$maxPosition = ExaminationType::max('position');
return $maxPosition >= 254 ? 255 : $maxPosition + 1;
}
} }

View File

@ -2,12 +2,15 @@
namespace App\Http\Controllers\admin\Catalog\Direction; namespace App\Http\Controllers\admin\Catalog\Direction;
use App\Helpers\SlugHelper;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\Direction\StoreExaminationTypeRequest; use App\Http\Requests\admin\Catalog\Direction\StoreExaminationTypeRequest;
use App\Http\Requests\admin\Catalog\Direction\UpdateExaminationTypeRequest; use App\Http\Requests\admin\Catalog\Direction\UpdateExaminationTypeRequest;
use App\Models\ExaminationType; use App\Models\ExaminationType;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class ExaminationTypeController extends Controller class ExaminationTypeController extends Controller
{ {
@ -26,13 +29,21 @@ class ExaminationTypeController extends Controller
{ {
$validated = $request->validated(); $validated = $request->validated();
$slug = SlugHelper::get($validated);
$type = new ExaminationType(); $type = new ExaminationType();
$type->name = $validated['name']; $type->name = $validated['name'];
$type->description = $validated['description']; $type->description = $validated['description'];
$type->slug = $validated['slug']; $type->slug = $slug;
$type->position = $validated['position']; $type->position = $validated['position'];
$type->save(); $type->save();
Log::channel('app')
->info(
'CREATE тип экзамена {examinationType} - user {user}',
['user' => Auth::user()->name, 'faculty' => $type->name, 'data' => $validated]
);
return redirect()->route('examination_types.index'); return redirect()->route('examination_types.index');
} }
@ -49,21 +60,52 @@ class ExaminationTypeController extends Controller
public function update(UpdateExaminationTypeRequest $request, ExaminationType $examinationType): RedirectResponse public function update(UpdateExaminationTypeRequest $request, ExaminationType $examinationType): RedirectResponse
{ {
$validated = $request->validated(); $validated = $request->validated();
$oldData = $examinationType->toArray();
$examinationType->name = $validated['name']; $examinationType->name = $validated['name'];
$examinationType->description = $validated['description']; $examinationType->description = $validated['description'];
$examinationType->slug = $validated['slug']; $examinationType->slug = $validated['slug'];
$examinationType->position = $validated['position']; $examinationType->position = $validated['position'];
$examinationType->save();
Log::channel('app')
->warning(
'UPDATE тип экзамена {examinationType} - user {user}',
[
'user' => Auth::user()->name,
'examinationType' => $examinationType->name,
'oldData' => $oldData,
'newData' => $validated
]
);
$examinationType->save();
return redirect()->route('examination_types.index'); return redirect()->route('examination_types.index');
} }
public function destroy(ExaminationType $examinationType): RedirectResponse public function destroy(ExaminationType $examinationType): RedirectResponse
{ {
if ($examinationType->entranceExaminations()->exists()) { if ($examinationType->entranceExaminations()->exists()) {
Log::channel('app')
->error(
'NOT DELETE тип экзамена {examinationType} - user {user}',
[
'user' => Auth::user()->name,
'examinationType' => $examinationType->name,
'data' => $examinationType->toArray(),
]
);
return back(); return back();
} }
Log::channel('app')
->critical(
'DELETE тип экзамена {examinationType} - user {user}',
[
'user' => Auth::user()->name,
'examinationType' => $examinationType->name,
'data' => $examinationType->toArray(),
]
);
$examinationType->delete(); $examinationType->delete();
return redirect()->route('examination_types.index'); return redirect()->route('examination_types.index');
} }

View File

@ -16,8 +16,24 @@ class StoreExaminationTypeRequest extends FormRequest
return [ return [
'position' => 'required|int|numeric|max:255', 'position' => 'required|int|numeric|max:255',
'name' => 'required|string|max:255|unique:examination_types,name', 'name' => 'required|string|max:255|unique:examination_types,name',
'description' => 'string', 'description' => 'nullable|string',
'slug' => 'required|string|max:255|unique:examination_types,slug', 'slug' => 'nullable|string|max:255|unique:examination_types,slug',
];
}
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 символов.',
]; ];
} }
} }

View File

@ -15,7 +15,7 @@ class UpdateExaminationTypeRequest 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,21 @@ class UpdateExaminationTypeRequest 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 символов.',
];
}
} }

View File

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

View File

@ -1,59 +1,69 @@
@php use App\Helpers\PositionHelper; @endphp
@extends('layouts.admin_layout') @extends('layouts.admin_layout')
@section('content') @section('content')
@auth() @auth()
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h1 class=""> Создать тип экзамена</h1> <h1 class=""> Создать тип экзамена</h1>
{{ Form::open(['url' => route('examination_types.store'), 'method' => 'POST', 'class' => '']) }} {{ Form::open(['url' => route('examination_types.store'), 'method' => 'POST', 'class' => 'needs-validation', 'novalidate']) }}
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
{{ Form::label('name', 'Название') }} {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.position')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('name', '', ['class' => 'form-control']) }} {{ Form::text('position', PositionHelper::examinationType(), ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.position'), 'required']) }}
<div class="invalid-feedback">
Поле "Позиция" обязательно!
</div> </div>
<div> </div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('position') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.name')]) }}
<span class="text-danger">*</span>
</div>
<div class="mt-1">
{{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.name'), 'required']) }}
<div class="invalid-feedback">
Поле "Название" обязательно!
</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.examination_types.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.examination_types.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('slug', 'URL') }} {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.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.examination_types.slug')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('slug') }} {{ $errors->first('slug') }}
@endif @endif
</div> </div>
<div class="mt-3">
{{ Form::label('position', 'Позиция') }}
</div>
<div class="mt-1">
{{ Form::text('position', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('position') }}
@endif
</div>
<div class="mt-3"> <div class="mt-3">
{{ Form::submit('Создать', ['class' => 'btn btn-primary']) }} {{ Form::submit('Создать', ['class' => 'btn btn-primary']) }}
</div> </div>

View File

@ -5,56 +5,64 @@
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h1 class="">Изменить тип экзамена</h1> <h1 class="">Изменить тип экзамена</h1>
{{ Form::open(['url' => route('examination_types.update', $examinationType), 'method' => 'PATCH', 'class' => '']) }} {{ Form::open(['url' => route('examination_types.update', $examinationType), 'method' => 'PATCH', 'class' => 'needs-validation', 'novalidate']) }}
<div class="col"> <div class="col">
<div class="mt-3"> <div class="mt-3">
{{ Form::label('name', 'Название') }} {{ Form::label('position', 'Позиция', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.position')]) }}
<span class="text-danger">*</span>
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('name', $examinationType->name, ['class' => 'form-control']) }} {{ Form::number('position', $examinationType->position, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.position'), 'required']) }}
<div class="invalid-feedback">
Поле "Позиция" обязательно!
</div> </div>
<div> </div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('position') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.faculty.name')]) }}
</div>
<div class="mt-1">
{{ Form::text('name', $examinationType->name, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.name'), 'required']) }}
<div class="invalid-feedback">
Поле "Название" обязательно!
</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.examination_types.description')]) }}
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('description', $examinationType->description, ['class' => 'form-control']) }} {{ Form::text('description', $examinationType->description, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.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('slug', 'URL') }} {{ Form::label('slug', 'URL', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.slug')]) }}
</div> </div>
<div class="mt-1"> <div class="mt-1">
{{ Form::text('slug', $examinationType->slug, ['class' => 'form-control']) }} {{ Form::text('slug', $examinationType->slug, ['class' => 'form-control','data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.examination_types.slug')]) }}
</div> </div>
<div> <div class="text-danger">
@if ($errors->any()) @if ($errors->any())
{{ $errors->first('slug') }} {{ $errors->first('slug') }}
@endif @endif
</div> </div>
<div class="mt-3">
{{ Form::label('position', 'Позиция') }}
</div>
<div class="mt-1">
{{ Form::text('position', $examinationType->position, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('position') }}
@endif
</div>
<div class="mt-3"> <div class="mt-3">
{{ Form::submit('Изменить', ['class' => 'btn btn-primary']) }} {{ Form::submit('Изменить', ['class' => 'btn btn-primary']) }}
</div> </div>