refactoring Department resource

This commit is contained in:
aslan 2024-02-12 09:19:08 +03:00
parent abfa800707
commit 35104455c2
12 changed files with 49 additions and 122 deletions

View File

@ -1,10 +1,10 @@
<?php
namespace App\Http\Controllers\Catalog;
namespace App\Http\Controllers\admin\Catalog;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreDepartmentRequest;
use App\Http\Requests\UpdateDepartmentRequest;
use App\Http\Requests\admin\Catalog\StoreDepartmentRequest;
use App\Http\Requests\admin\Catalog\UpdateDepartmentRequest;
use App\Models\Department;
use App\Models\Faculty;
use Illuminate\Contracts\View\Factory;
@ -17,13 +17,13 @@ class DepartmentController extends Controller
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$departments = Department::all();
return view('catalog.department.index', compact('departments'));
return view('admin.catalog.department.index', compact('departments'));
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
return view('catalog.department.create', compact('faculties'));
return view('admin.catalog.department.create', compact('faculties'));
}
public function store(StoreDepartmentRequest $request): RedirectResponse
@ -42,13 +42,15 @@ class DepartmentController extends Controller
public function show(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('catalog.department.show', compact('department'));
$faculty = Faculty::find($department->faculty->id);
$educationalInstitution = $faculty->educationalInstitution;
return view('admin.catalog.department.show', compact('department', 'faculty', 'educationalInstitution'));
}
public function edit(Department $department): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
$faculties = Faculty::pluck('name', 'id');
return view('catalog.department.edit', compact('department', 'faculties'));
return view('admin.catalog.department.edit', compact('department', 'faculties'));
}
public function update(UpdateDepartmentRequest $request, Department $department): RedirectResponse

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;
@ -26,7 +26,8 @@ class StoreDepartmentRequest extends FormRequest
'position' => 'required|int|max:255',
'name' => 'required|string|max:255|unique:educational_institutions,name',
'description' => 'string',
'faculty_id' => 'required|int'
'slug' => 'required|string',
'faculty_id' => 'required|int',
];
}
}

View File

@ -1,30 +1,22 @@
<?php
namespace App\Http\Requests\Catalog;
namespace App\Http\Requests\admin\Catalog;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class UpdateDepartmentRequest 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, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'position' => 'int|max:255',
'position' => 'required|int|max:255',
'description' => 'string',
'slug' => 'string|required',
'faculty_id' => 'int|required',
'name' => [
'required',

View File

@ -14,6 +14,7 @@ class Department extends Model
protected $fillable = [
'id',
'name',
'slug',
'description',
'position',
];

View File

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

View File

@ -3,21 +3,19 @@
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Department>
*/
class DepartmentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
'name' => fake()->name(),
'description' => fake()->text(),
'slug' => fake()->slug(),
'position' => fake()->randomNumber(),
'faculty_id' => fake()->realTextBetween(1,5,1),
];
}
}

View File

@ -14,8 +14,9 @@ return new class extends Migration
Schema::create('departments', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->text('description');
$table->integer('position');
$table->string('slug');
$table->foreignId('faculty_id')->constrained('faculties');
$table->timestamps();
});

View File

@ -20,12 +20,14 @@ class DepartmentSeeder extends Seeder
'name' => 'Кафедра инф. без.',
'description' => 'Кафедра инф. без. описание',
'position' => 1,
'slug' => 'departmentInfWithout',
'faculty_id' => 1,
],
[
'name' => 'кафедра стоматологии',
'description' => 'кафедра стоматологии описание',
'position' => 2,
'slug' => 'departmentOfDentistry',
'faculty_id' => 2,
],
]);

View File

@ -13,6 +13,7 @@
<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>
@ -24,6 +25,7 @@
<th scope="row">{{ $department->position }}</th>
<td><a href="{{ route('departments.show', $department) }}">{{ $department->name }}</a></td>
<td>{{ Str::words($department->description, 10, '...') }}</td>
<td>{{ $department->slug }}</td>
<td>{{ $department->faculty->name }}</td>
<td>
<a href="{{ route("departments.edit", $department) }}"

View File

@ -1,17 +1,9 @@
@php
use App\Models\Department;
use App\Models\Faculty;
use Illuminate\Support\Collection;
/** @var Collection|Department[] $department */
$faculty = Faculty::find($department->faculty->id)
@endphp
@extends('layouts.admin-layout')
@section('content')
@auth()
<h6>
<a href="{{ route('educational-institutions.show', $faculty->educationalInstitution) }}">
{{ $faculty->educationalInstitution->name }}
<a href="{{ route('educational-institutions.show', $educationalInstitution) }}">
{{ $educationalInstitution->name }}
</a>-> <a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a>
-> {{ $department->name }}
</h6>
@ -22,10 +14,10 @@
<p>{{ $department->description }}</p>
<h2>Позиция</h2>
<p>{{ $department->position }}</p>
{{-- <h2>Факультеты</h2>--}}
{{-- @foreach($department->faculties as $faculty)--}}
{{-- <p><a href="{{ route('faculties.show', $faculty) }}">{{ $faculty->name }}</a></p>--}}
{{-- @endforeach--}}
<h2>Направления подготовки</h2>
@foreach($department->directions as $direction)
<p><a href="{{ route('faculties.show', $direction) }}">{{ $direction->name }}</a></p>
@endforeach
</div>
@endauth
@endsection

View File

@ -1,31 +1,33 @@
<?php
use App\Http\Controllers\Catalog\DepartmentController;
use App\Http\Controllers\Catalog\DirectionController;
use App\Http\Controllers\Catalog\EducationalInstitutionController;
use App\Http\Controllers\Catalog\FacultyController;
use App\Http\Controllers\DocumentController;
use App\Http\Controllers\ReceptionScreenController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\admin\Catalog\DepartmentController;
use App\Http\Controllers\admin\Catalog\DirectionController;
use App\Http\Controllers\admin\Catalog\EducationalInstitutionController;
use App\Http\Controllers\admin\Catalog\FacultyController;
use App\Http\Controllers\admin\DocumentController;
use App\Http\Controllers\admin\ReceptionScreenController;
use App\Http\Controllers\admin\UserController;
Route::middleware(['auth', 'verified'])->prefix('admin')->group(function () {
Route::get('/dashboard', function () {
return view('admin');
})->name('dashboard');
Route::get('/files/create/{document?}', [DocumentController::class, 'create'])->name('document_create');
Route::get('/files/download/{Document}', [DocumentController::class, 'download'])->name('document.download');
Route::get('/documents/create/{document?}', [DocumentController::class, 'create'])->name('documents_create');
Route::get('/documents/download/{Document}', [DocumentController::class, 'download'])->name('documents_download');
Route::resource('/educational-institutions', EducationalInstitutionController::class)
->scoped(['educational_institution' => 'slug']);
Route::resource('/directions', DirectionController::class)
->scoped(['educational_institution' => 'slug']);
->scoped(['directions' => 'slug']);
Route::resource('/departments', DepartmentController::class)
->scoped(['department' => 'slug']);
Route::resources([
'/files' => DocumentController::class,
'/documents' => DocumentController::class,
'/users' => UserController::class,
'/admin-reception-screen' => ReceptionScreenController::class,
'/faculties' => FacultyController::class,
'/departments' => DepartmentController::class,
]);
});