add bootstrap validated and logging to Subject

This commit is contained in:
aslan 2024-03-12 13:50:42 +03:00
parent ed47971981
commit 5cf8061fd5
7 changed files with 141 additions and 50 deletions

View File

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

View File

@ -2,12 +2,15 @@
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
{
@ -26,13 +29,21 @@ class SubjectController extends Controller
{
$validated = $request->validated();
$slug = SlugHelper::get($validated);
$subject = new Subject();
$subject->name = $validated['name'];
$subject->description = $validated['description'];
$subject->slug = $validated['slug'];
$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');
}
@ -49,21 +60,51 @@ class SubjectController extends Controller
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'];
$subject->save();
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');
}

View File

@ -16,8 +16,25 @@ class StoreSubjectRequest extends FormRequest
return [
'position' => 'required|int|numeric|max:255',
'name' => 'required|string|max:255|unique:subjects,name',
'description' => 'string',
'slug' => 'required|string|max:255|unique:subjects,slug',
'description' => 'nullable|string',
'slug' => 'nullable|string|max:255|unique:subjects,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 UpdateSubjectRequest extends FormRequest
{
return [
'position' => 'required|int|numeric|max:255',
'description' => 'string',
'description' => 'nullable|string',
'slug' => [
'string',
'required',

View File

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

View File

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

View File

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