refactoring Faculty resource
Tests & Lint & Deploy to Railway / build (2.6.6, 20.x, 8.3) (push) Failing after 1m34s Details
Tests & Lint & Deploy to Railway / deploy (push) Has been skipped Details

This commit is contained in:
aslan 2024-02-12 12:15:46 +03:00
parent 54765f6202
commit f391a26b7d
11 changed files with 154 additions and 91 deletions

View File

@ -1,11 +1,10 @@
<?php
namespace App\Http\Controllers\Catalog;
namespace App\Http\Controllers\admin\Catalog;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreFacultyRequest;
use App\Http\Requests\UpdateFacultyRequest;
use App\Models\Department;
use App\Http\Requests\admin\Catalog\StoreFacultyRequest;
use App\Http\Requests\admin\Catalog\UpdateFacultyRequest;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use Illuminate\Contracts\View\Factory;
@ -18,13 +17,13 @@ class FacultyController extends Controller
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::all();
return view('catalog.faculty.index', compact('faculties'));
return view('admin.catalog.faculty.index', compact('faculties'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
return view('catalog.faculty.create', compact('educationalInstitutions'));
return view('admin.catalog.faculty.create', compact('educationalInstitutions'));
}
public function store(StoreFacultyRequest $request): RedirectResponse
@ -35,6 +34,7 @@ class FacultyController extends Controller
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $validated['slug'];
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();
@ -43,13 +43,13 @@ class FacultyController extends Controller
public function show(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('catalog.faculty.show', compact('faculty'));
return view('admin.catalog.faculty.show', compact('faculty'));
}
public function edit(Faculty $faculty): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$educationalInstitutions = EducationalInstitution::pluck('name', 'id');
return view('catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
return view('admin.catalog.faculty.edit', compact('faculty', 'educationalInstitutions'));
}
public function update(UpdateFacultyRequest $request, Faculty $faculty): RedirectResponse
@ -59,6 +59,7 @@ class FacultyController extends Controller
$faculty->name = $validated['name'];
$faculty->description = $validated['description'];
$faculty->position = $validated['position'];
$faculty->slug = $validated['slug'];
$faculty->educational_institution_id = $validated['educational_institution_id'];
$faculty->save();

View File

@ -1,6 +1,6 @@
<?php
namespace App\Http\Requests\Catalog;
namespace App\Http\Requests\admin\Catalog;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
@ -12,17 +12,13 @@ class StoreFacultyRequest extends FormRequest
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'position' => 'int|max:255',
'name' => 'required|string|max:255|unique:educational_institutions,name',
'description' => 'string',
'slug' => 'required|string',
'educational_institution_id' => 'int'
];
}

View File

@ -1,30 +1,23 @@
<?php
namespace App\Http\Requests\Catalog;
namespace App\Http\Requests\admin\Catalog;
use Illuminate\Foundation\Http\FormRequest;
class UpdateFacultyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
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
{
return [
'position' => 'int|max:255',
'name' => 'required|string|max:255|unique:educational_institutions,name',
'description' => 'string',
'slug' => 'required|string',
'educational_institution_id' => 'int'
];
}

View File

@ -15,6 +15,7 @@ class Faculty extends Model
'id',
'name',
'description',
'slug',
'position',
];

View File

@ -1,66 +0,0 @@
<?php
namespace App\Policies;
use App\Models\Faculty;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class FacultyPolicy
{
/**
* 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, Faculty $faculty): 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, Faculty $faculty): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Faculty $faculty): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Faculty $faculty): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Faculty $faculty): bool
{
//
}
}

View File

@ -17,7 +17,11 @@ class FacultyFactory extends Factory
public function definition(): array
{
return [
//
'name' => fake()->name(),
'description' => fake()->text(),
'position' => fake()->randomDigit(),
'slug' => fake()->slug(),
'educational_institution_id' => 1,
];
}
}

View File

@ -16,6 +16,7 @@ return new class extends Migration
$table->string('name');
$table->string('description');
$table->integer('position');
$table->string('slug');
$table->foreignId('educational_institution_id')->constrained('educational_institutions');
$table->timestamps();
});

View File

@ -14,17 +14,20 @@ class FacultySeeder extends Seeder
*/
public function run(): void
{
// Faculty::factory(3)->create();
DB::table('faculties')->insert([
[
'name' => 'Информационная безопасность',
'description' => 'Факультет информационной безопасности описание',
'position' => 1,
'slug' => 'new-slug-inf',
'educational_institution_id' => 1,
],
[
'name' => 'Лечебный факультет',
'description' => 'Факультет Лечебный описание',
'position' => 1,
'slug' => 'new-slug-med',
'educational_institution_id' => 2,
],
]);

View File

@ -1,6 +1,5 @@
@extends('layouts.admin-layout')
@section('content')
@auth()
<div class="row">
<div class="col">
@ -53,6 +52,17 @@
{{ $errors->first('educational_institution_id') }}
@endif
</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-3">
{{ Form::submit('создать', ['class' => 'btn btn-primary']) }}
</div>

View File

@ -53,6 +53,17 @@
{{ $errors->first('educational_institution_id') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('slug', 'URL') }}
</div>
<div class="mt-1">
{{ Form::text('slug', $faculty->slug, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('slug') }}
@endif
</div>
<div class="mt-3">
{{ Form::submit('создать', ['class' => 'btn btn-primary']) }}
</div>

View File

@ -0,0 +1,109 @@
<?php
namespace Tests\Feature\admin\catalog;
use App\Models\Department;
use App\Models\Direction;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use App\Models\User;
use Tests\TestCase;
class FacultyTest extends TestCase
{
private User $user;
private Faculty $faculty;
private array $data;
protected function setUp(): void
{
parent::setUp();
EducationalInstitution::factory()->create();
$this->faculty = Faculty::factory()->create();
$this->data = Faculty::factory()->make()->only([
'position',
'name',
'description',
'slug',
'educational_institution_id',
]);
$this->department = Department::factory()->create();
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexFacultysPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('faculties.index'));
$response->assertOk();
}
public function testCreateFacultyPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('faculties.create'));
$response->assertOk();
}
public function testStoreFaculty(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('faculties.store', $this->data));
$response->assertRedirect(route('faculties.index'));
$this->assertDatabaseHas('faculties', $this->data);
}
public function testShowFacultyPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('faculties.show', $this->faculty));
$response->assertOk();
}
public function testEditFacultyPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('faculties.edit', $this->faculty));
$response->assertOk();
}
public function testUpdateFaculty(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('faculties.update', $this->faculty), $this->data);
$response->assertRedirect(route('faculties.index'));
$this->assertDatabaseHas('faculties', $this->data);
}
public function testDestroyFaculty(): void
{
$this->department->delete();
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('faculties.destroy', $this->faculty));
$response->assertRedirect(route('faculties.index'));
$this->assertDatabaseMissing('faculties', $this->faculty->toArray());
}
}