diff --git a/app/Http/Controllers/Catalog/FacultyController.php b/app/Http/Controllers/admin/Catalog/FacultyController.php similarity index 79% rename from app/Http/Controllers/Catalog/FacultyController.php rename to app/Http/Controllers/admin/Catalog/FacultyController.php index f32476d..0bec55f 100644 --- a/app/Http/Controllers/Catalog/FacultyController.php +++ b/app/Http/Controllers/admin/Catalog/FacultyController.php @@ -1,11 +1,10 @@ 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(); diff --git a/app/Http/Requests/Catalog/StoreFacultyRequest.php b/app/Http/Requests/admin/Catalog/StoreFacultyRequest.php similarity index 74% rename from app/Http/Requests/Catalog/StoreFacultyRequest.php rename to app/Http/Requests/admin/Catalog/StoreFacultyRequest.php index f333260..55f5986 100644 --- a/app/Http/Requests/Catalog/StoreFacultyRequest.php +++ b/app/Http/Requests/admin/Catalog/StoreFacultyRequest.php @@ -1,6 +1,6 @@ - */ 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' ]; } diff --git a/app/Http/Requests/Catalog/UpdateFacultyRequest.php b/app/Http/Requests/admin/Catalog/UpdateFacultyRequest.php similarity index 61% rename from app/Http/Requests/Catalog/UpdateFacultyRequest.php rename to app/Http/Requests/admin/Catalog/UpdateFacultyRequest.php index addc8b5..4969eed 100644 --- a/app/Http/Requests/Catalog/UpdateFacultyRequest.php +++ b/app/Http/Requests/admin/Catalog/UpdateFacultyRequest.php @@ -1,30 +1,23 @@ |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' ]; } diff --git a/app/Models/Faculty.php b/app/Models/Faculty.php index 5f65286..2a48d31 100644 --- a/app/Models/Faculty.php +++ b/app/Models/Faculty.php @@ -15,6 +15,7 @@ class Faculty extends Model 'id', 'name', 'description', + 'slug', 'position', ]; diff --git a/app/Policies/FacultyPolicy.php b/app/Policies/FacultyPolicy.php deleted file mode 100644 index 2f2623c..0000000 --- a/app/Policies/FacultyPolicy.php +++ /dev/null @@ -1,66 +0,0 @@ - fake()->name(), + 'description' => fake()->text(), + 'position' => fake()->randomDigit(), + 'slug' => fake()->slug(), + 'educational_institution_id' => 1, ]; } } diff --git a/database/migrations/2024_02_07_073254_create_faculties_table.php b/database/migrations/2024_02_07_073254_create_faculties_table.php index eefb309..76eb311 100644 --- a/database/migrations/2024_02_07_073254_create_faculties_table.php +++ b/database/migrations/2024_02_07_073254_create_faculties_table.php @@ -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(); }); diff --git a/database/seeders/FacultySeeder.php b/database/seeders/FacultySeeder.php index b69cece..38c9313 100644 --- a/database/seeders/FacultySeeder.php +++ b/database/seeders/FacultySeeder.php @@ -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, ], ]); diff --git a/resources/views/catalog/faculty/create.blade.php b/resources/views/admin/catalog/faculty/create.blade.php similarity index 85% rename from resources/views/catalog/faculty/create.blade.php rename to resources/views/admin/catalog/faculty/create.blade.php index 66a44b6..9fa6346 100644 --- a/resources/views/catalog/faculty/create.blade.php +++ b/resources/views/admin/catalog/faculty/create.blade.php @@ -1,6 +1,5 @@ @extends('layouts.admin-layout') @section('content') - @auth()
@@ -53,6 +52,17 @@ {{ $errors->first('educational_institution_id') }} @endif
+
+ {{ Form::label('slug', 'URL') }} +
+
+ {{ Form::text('slug', '', ['class' => 'form-control']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('slug') }} + @endif +
{{ Form::submit('создать', ['class' => 'btn btn-primary']) }}
diff --git a/resources/views/catalog/faculty/edit.blade.php b/resources/views/admin/catalog/faculty/edit.blade.php similarity index 85% rename from resources/views/catalog/faculty/edit.blade.php rename to resources/views/admin/catalog/faculty/edit.blade.php index 085620d..b7c7c13 100644 --- a/resources/views/catalog/faculty/edit.blade.php +++ b/resources/views/admin/catalog/faculty/edit.blade.php @@ -53,6 +53,17 @@ {{ $errors->first('educational_institution_id') }} @endif
+
+ {{ Form::label('slug', 'URL') }} +
+
+ {{ Form::text('slug', $faculty->slug, ['class' => 'form-control']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('slug') }} + @endif +
{{ Form::submit('создать', ['class' => 'btn btn-primary']) }}
diff --git a/tests/Feature/admin/catalog/FacultyTest.php b/tests/Feature/admin/catalog/FacultyTest.php new file mode 100644 index 0000000..418e8e9 --- /dev/null +++ b/tests/Feature/admin/catalog/FacultyTest.php @@ -0,0 +1,109 @@ +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()); + } +}