refactoring EducationalInstitution resource

This commit is contained in:
aslan 2024-02-12 14:55:17 +03:00
parent 173a744f79
commit 1f67f437f3
16 changed files with 177 additions and 123 deletions

View File

@ -1,10 +1,10 @@
<?php <?php
namespace App\Http\Controllers\Catalog; namespace App\Http\Controllers\admin\Catalog;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\StoreEducationalInstitutionRequest; use App\Http\Requests\admin\Catalog\StoreEducationalInstitutionRequest;
use App\Http\Requests\UpdateEducationalInstitutionRequest; use App\Http\Requests\admin\Catalog\UpdateEducationalInstitutionRequest;
use App\Models\EducationalInstitution; use App\Models\EducationalInstitution;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
@ -15,12 +15,12 @@ class EducationalInstitutionController extends Controller
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{ {
$educationalInstitutions = EducationalInstitution::all(); $educationalInstitutions = EducationalInstitution::all();
return view('catalog.educational-institution.index', compact('educationalInstitutions')); return view('admin.catalog.educational_institution.index', compact('educationalInstitutions'));
} }
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{ {
return view('catalog.educational-institution.create'); return view('admin.catalog.educational_institution.create');
} }
public function store(StoreEducationalInstitutionRequest $request) public function store(StoreEducationalInstitutionRequest $request)
@ -30,20 +30,21 @@ class EducationalInstitutionController extends Controller
$educationalInstitution = new EducationalInstitution(); $educationalInstitution = new EducationalInstitution();
$educationalInstitution->name = $validated['name']; $educationalInstitution->name = $validated['name'];
$educationalInstitution->description = $validated['description']; $educationalInstitution->description = $validated['description'];
$educationalInstitution->slug = $validated['slug'];
$educationalInstitution->position = $validated['position']; $educationalInstitution->position = $validated['position'];
$educationalInstitution->save(); $educationalInstitution->save();
return redirect()->route('educational-institutions.index'); return redirect()->route('educational_institutions.index');
} }
public function show(EducationalInstitution $educationalInstitution) public function show(EducationalInstitution $educationalInstitution)
{ {
return view('catalog.educational-institution.show', compact('educationalInstitution')); return view('admin.catalog.educational_institution.show', compact('educationalInstitution'));
} }
public function edit(EducationalInstitution $educationalInstitution) public function edit(EducationalInstitution $educationalInstitution)
{ {
return view('catalog.educational-institution.edit', compact('educationalInstitution')); return view('admin.catalog.educational_institution.edit', compact('educationalInstitution'));
} }
public function update(UpdateEducationalInstitutionRequest $request, EducationalInstitution $educationalInstitution) public function update(UpdateEducationalInstitutionRequest $request, EducationalInstitution $educationalInstitution)
@ -53,9 +54,10 @@ class EducationalInstitutionController extends Controller
$educationalInstitution->name = $validated['name']; $educationalInstitution->name = $validated['name'];
$educationalInstitution->description = $validated['description']; $educationalInstitution->description = $validated['description'];
$educationalInstitution->position = $validated['position']; $educationalInstitution->position = $validated['position'];
$educationalInstitution->slug = $validated['slug'];
$educationalInstitution->save(); $educationalInstitution->save();
return redirect()->route('educational-institutions.index'); return redirect()->route('educational_institutions.index');
} }
public function destroy(EducationalInstitution $educationalInstitution) public function destroy(EducationalInstitution $educationalInstitution)
@ -65,6 +67,6 @@ class EducationalInstitutionController extends Controller
} }
$educationalInstitution->delete(); $educationalInstitution->delete();
return redirect()->route('educational-institutions.index'); return redirect()->route('educational_institutions.index');
} }
} }

View File

@ -1,30 +1,23 @@
<?php <?php
namespace App\Http\Requests\Catalog; namespace App\Http\Requests\admin\Catalog;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
class StoreEducationalInstitutionRequest extends FormRequest class StoreEducationalInstitutionRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool public function authorize(): bool
{ {
return true; return true;
} }
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array public function rules(): array
{ {
return [ return [
'position' => 'int|max:255', 'position' => 'int|max:255',
'name' => 'required|string|max:255|unique:educational_institutions,name', 'name' => 'required|string|max:255|unique:educational_institutions,name',
'description' => 'string', 'description' => 'string',
'slug' => 'required|string',
]; ];
} }
} }

View File

@ -1,30 +1,23 @@
<?php <?php
namespace App\Http\Requests\Catalog; namespace App\Http\Requests\admin\Catalog;
use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
class UpdateEducationalInstitutionRequest extends FormRequest class UpdateEducationalInstitutionRequest extends FormRequest
{ {
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool public function authorize(): bool
{ {
return true; return true;
} }
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array public function rules(): array
{ {
return [ return [
'position' => 'int|max:255', 'position' => 'int|max:255',
'description' => 'string', 'description' => 'string',
'slug' => 'required|string',
'name' => [ 'name' => [
'required', 'required',
'string', 'string',

View File

@ -15,10 +15,15 @@ class UpdateFacultyRequest extends FormRequest
{ {
return [ return [
'position' => 'int|max:255', 'position' => 'int|max:255',
'name' => 'required|string|max:255|unique:educational_institutions,name',
'description' => 'string', 'description' => 'string',
'slug' => 'required|string', 'slug' => 'required|string',
'educational_institution_id' => 'int' 'educational_institution_id' => 'int',
'name' => [
'required',
'string',
'max:255',
"unique:educational_institutions,name,{$this->faculty->id}",
],
]; ];
} }
} }

View File

@ -1,66 +0,0 @@
<?php
namespace App\Policies;
use App\Models\EducationalInstitution;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class EducationalInstitutionPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, EducationalInstitution $educationalInstitution): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, EducationalInstitution $educationalInstitution): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, EducationalInstitution $educationalInstitution): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, EducationalInstitution $educationalInstitution): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, EducationalInstitution $educationalInstitution): bool
{
//
}
}

View File

@ -4,20 +4,15 @@ namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\EducationalInstitution>
*/
class EducationalInstitutionFactory extends Factory class EducationalInstitutionFactory extends Factory
{ {
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array public function definition(): array
{ {
return [ return [
// 'name' => fake()->name(),
'description' => fake()->text(),
'slug' => fake()->slug(),
'position' => fake()->randomDigit(),
]; ];
} }
} }

View File

@ -2,7 +2,7 @@
@section('content') @section('content')
@auth() @auth()
<h6> <h6>
<a href="{{ route('educational-institutions.show', $educationalInstitution) }}"> <a href="{{ route('educational_institutions.show', $educationalInstitution) }}">
{{ $educationalInstitution->name }} {{ $educationalInstitution->name }}
</a>-> <a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a> </a>-> <a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a>
-> {{ $department->name }} -> {{ $department->name }}

View File

@ -2,7 +2,7 @@
@section('content') @section('content')
@auth() @auth()
<h6> <h6>
<a href="{{ route('educational-institutions.show', $educationalInstitution) }}"> <a href="{{ route('educational_institutions.show', $educationalInstitution) }}">
{{ $educationalInstitution->name }} {{ $educationalInstitution->name }}
</a> </a>
-> ->

View File

@ -1,11 +1,10 @@
@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('educational-institutions.store'), 'method' => 'POST', 'class' => '']) }} {{ Form::open(['url' => route('educational_institutions.store'), 'method' => 'POST', 'class' => '']) }}
<div class="col"> <div class="col">
<div> <div>
{{ Form::label('position', 'Позиция') }} {{ Form::label('position', 'Позиция') }}
@ -42,6 +41,17 @@
{{ $errors->first('description') }} {{ $errors->first('description') }}
@endif @endif
</div> </div>
<div class="mt-3">
{{ Form::label('slug', 'URL') }}
</div>
<div class="mt-1">
{{ Form::text('slug', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('slug') }}
@endif
</div>
<div class="mt-4"> <div class="mt-4">
{{ Form::submit('создать', ['class' => 'btn btn-primary']) }} {{ Form::submit('создать', ['class' => 'btn btn-primary']) }}
</div> </div>

View File

@ -1,11 +1,10 @@
@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::model($educationalInstitution, ['route' => ['educational-institutions.update', $educationalInstitution], 'method' => 'PATCH', 'class' => '']) }} {{ Form::model($educationalInstitution, ['route' => ['educational_institutions.update', $educationalInstitution], 'method' => 'PATCH', 'class' => '']) }}
<div class="col"> <div class="col">
<div> <div>
{{ Form::label('position', 'Позиция') }} {{ Form::label('position', 'Позиция') }}
@ -42,6 +41,17 @@
{{ $errors->first('description') }} {{ $errors->first('description') }}
@endif @endif
</div> </div>
<div class="mt-3">
{{ Form::label('slug', 'URL') }}
</div>
<div class="mt-1">
{{ Form::text('slug', $educationalInstitution->slug, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('slug') }}
@endif
</div>
<div class="mt-4"> <div class="mt-4">
{{ Form::submit('Изменить', ['class' => 'btn btn-primary']) }} {{ Form::submit('Изменить', ['class' => 'btn btn-primary']) }}
</div> </div>

View File

@ -3,16 +3,16 @@
<div class="container"> <div class="container">
<h2>Учебные заведения</h2> <h2>Учебные заведения</h2>
<br> <br>
<a href="{{ route('educational-institutions.create') }}" class="btn btn-primary">Создать учебное заведение</a> <a href="{{ route('educational_institutions.create') }}" class="btn btn-primary">Создать учебное заведение</a>
<br> <br>
<br> <br>
<table class="table"> <table class="table">
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left"> <thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
<tr> <tr>
<th scope="col">Позиция</th> <th scope="col">Позиция</th>
<th scope="col">Название</th> <th scope="col">Название</th>
<th scope="col">Описание</th> <th scope="col">Описание</th>
<th scope="col">URL</th>
<th scope="col">действия</th> <th scope="col">действия</th>
<th scope="col"></th> <th scope="col"></th>
</tr> </tr>
@ -21,12 +21,15 @@
@foreach($educationalInstitutions as $educationalInstitution) @foreach($educationalInstitutions as $educationalInstitution)
<tr class=""> <tr class="">
<th scope="row">{{ $educationalInstitution->position }}</th> <th scope="row">{{ $educationalInstitution->position }}</th>
<td><a href="{{ route('educational-institutions.show', $educationalInstitution) }}">{{ $educationalInstitution->name }}</a></td> <td>
<a href="{{ route('educational_institutions.show', $educationalInstitution) }}">{{ $educationalInstitution->name }}</a>
</td>
<td>{{ Str::words($educationalInstitution->description, 10, '...') }}</td> <td>{{ Str::words($educationalInstitution->description, 10, '...') }}</td>
<td class="col-3"><a href="{{ route("educational-institutions.edit", $educationalInstitution) }}" <th scope="row">{{ $educationalInstitution->slug }}</th>
<td class="col-3"><a href="{{ route("educational_institutions.edit", $educationalInstitution) }}"
class="btn btn-secondary">редактировать</a> class="btn btn-secondary">редактировать</a>
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?" <a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('educational-institutions.destroy', $educationalInstitution) }}" href="{{ route('educational_institutions.destroy', $educationalInstitution) }}"
class="btn btn-danger"> class="btn btn-danger">
удалить удалить
</a> </a>
@ -38,5 +41,4 @@
<br> <br>
<br> <br>
</div> </div>
@endsection @endsection

View File

@ -1,6 +1,5 @@
@extends('layouts.admin-layout') @extends('layouts.admin-layout')
@section('content') @section('content')
@auth() @auth()
<div class="container mt-4"> <div class="container mt-4">
<h2>Название</h2> <h2>Название</h2>
@ -9,6 +8,8 @@
<p>{{ $educationalInstitution->description }}</p> <p>{{ $educationalInstitution->description }}</p>
<h2>Позиция</h2> <h2>Позиция</h2>
<p>{{ $educationalInstitution->position }}</p> <p>{{ $educationalInstitution->position }}</p>
<h2>URL</h2>
<p>{{ $educationalInstitution->slug }}</p>
<h2>Факультеты</h2> <h2>Факультеты</h2>
@foreach($educationalInstitution->faculties as $faculty) @foreach($educationalInstitution->faculties as $faculty)
<p><a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a></p> <p><a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a></p>

View File

@ -13,6 +13,7 @@
<th scope="col">Позиция</th> <th scope="col">Позиция</th>
<th scope="col">Название</th> <th scope="col">Название</th>
<th scope="col">Описание</th> <th scope="col">Описание</th>
<th scope="col">URL</th>
<th scope="col">Учебное заведение</th> <th scope="col">Учебное заведение</th>
<th scope="col">действия</th> <th scope="col">действия</th>
<th scope="col"></th> <th scope="col"></th>
@ -24,6 +25,7 @@
<th scope="row">{{ $faculty->position }}</th> <th scope="row">{{ $faculty->position }}</th>
<td><a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a></td> <td><a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a></td>
<td>{{ Str::words($faculty->description, 10, '...') }}</td> <td>{{ Str::words($faculty->description, 10, '...') }}</td>
<td>{{ Str::words($faculty->slug, 10, '...') }}</td>
<td>{{ $faculty->educationalInstitution->name }}</td> <td>{{ $faculty->educationalInstitution->name }}</td>
<td><a href="{{ route("faculties.edit", $faculty) }}" <td><a href="{{ route("faculties.edit", $faculty) }}"
class="btn btn-secondary">редактировать</a> class="btn btn-secondary">редактировать</a>

View File

@ -1,8 +1,7 @@
@extends('layouts.admin-layout') @extends('layouts.admin-layout')
@section('content') @section('content')
@auth() @auth()
<h6><a href="{{ route('educational-institutions.show', $faculty->educationalInstitution) }}">{{ $faculty->educationalInstitution->name }}</a> -> {{ $faculty->name }}</h6> <h6><a href="{{ route('educational_institutions.show', $faculty->educationalInstitution) }}">{{ $faculty->educationalInstitution->name }}</a> -> {{ $faculty->name }}</h6>
<div class="container mt-4"> <div class="container mt-4">
<h2>Название</h2> <h2>Название</h2>
<p>{{ $faculty->name }}</p> <p>{{ $faculty->name }}</p>
@ -10,6 +9,8 @@
<p>{{ $faculty->description }}</p> <p>{{ $faculty->description }}</p>
<h2>Позиция</h2> <h2>Позиция</h2>
<p>{{ $faculty->position }}</p> <p>{{ $faculty->position }}</p>
<h2>URL</h2>
<p>{{ $faculty->slug }}</p>
<h2>Кафедры</h2> <h2>Кафедры</h2>
@foreach($faculty->departments as $department) @foreach($faculty->departments as $department)
<p><a href="{{ route('departments.show', $department) }}">{{ $department->name }}</a></p> <p><a href="{{ route('departments.show', $department) }}">{{ $department->name }}</a></p>

View File

@ -0,0 +1,106 @@
<?php
namespace Tests\Feature\admin\catalog;
use App\Models\Department;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use App\Models\User;
use Tests\TestCase;
class EducationalInstitutionTest extends TestCase
{
private User $user;
private EducationalInstitution $educationalInstitution;
private Faculty $faculty;
private array $data;
protected function setUp(): void
{
parent::setUp();
$this->educationalInstitution = EducationalInstitution::factory()->create();
$this->data = EducationalInstitution::factory()->make()->only([
'position',
'name',
'description',
'slug',
]);
$this->faculty = Faculty::factory()->create();
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexEducationalInstitutionsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('educational_institutions.index'));
$response->assertOk();
}
public function testCreateEducationalInstitutionPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('educational_institutions.create'));
$response->assertOk();
}
public function testStoreEducationalInstitution(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('educational_institutions.store', $this->data));
$response->assertRedirect(route('educational_institutions.index'));
$this->assertDatabaseHas('educational_institutions', $this->data);
}
public function testShowEducationalInstitutionPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('educational_institutions.show', $this->educationalInstitution));
$response->assertOk();
}
public function testEditEducationalInstitutionPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('educational_institutions.edit', $this->educationalInstitution));
$response->assertOk();
}
public function testUpdateEducationalInstitution(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('educational_institutions.update', $this->educationalInstitution), $this->data);
$response->assertRedirect(route('educational_institutions.index'));
$this->assertDatabaseHas('educational_institutions', $this->data);
}
public function testDestroyFaculty(): void
{
$this->faculty->delete();
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('educational_institutions.destroy', $this->educationalInstitution));
$response->assertRedirect(route('educational_institutions.index'));
$this->assertDatabaseMissing('educational_institutions', $this->educationalInstitution->toArray());
}
}

View File

@ -3,7 +3,6 @@
namespace Tests\Feature\admin\catalog; namespace Tests\Feature\admin\catalog;
use App\Models\Department; use App\Models\Department;
use App\Models\Direction;
use App\Models\EducationalInstitution; use App\Models\EducationalInstitution;
use App\Models\Faculty; use App\Models\Faculty;
use App\Models\User; use App\Models\User;
@ -13,6 +12,7 @@ class FacultyTest extends TestCase
{ {
private User $user; private User $user;
private Faculty $faculty; private Faculty $faculty;
private Department $department;
private array $data; private array $data;
protected function setUp(): void protected function setUp(): void
{ {
@ -37,7 +37,7 @@ class FacultyTest extends TestCase
]); ]);
} }
public function testIndexFacultysPage(): void public function testIndexFacultiesPage(): void
{ {
$response = $this->actingAs($this->user) $response = $this->actingAs($this->user)
->withSession(['banned' => false]) ->withSession(['banned' => false])