fix SubjectType recourse

This commit is contained in:
aslan 2024-02-17 12:31:22 +03:00
parent 84061c6c95
commit 5b1e4cab99
16 changed files with 522 additions and 1 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace App\Http\Controllers\admin\Catalog\Direction;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\Direction\StoreSubjectTypeRequest;
use App\Http\Requests\admin\Catalog\Direction\UpdateSubjectTypeRequest;
use App\Models\SubjectType;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
class SubjectTypeController extends Controller
{
public function index(): View
{
$subjectTypes = SubjectType::all();
return view('admin.catalog.direction.subject_type.index', compact('subjectTypes'));
}
public function create(): View
{
return view('admin.catalog.direction.subject_type.create');
}
public function store(StoreSubjectTypeRequest $request): RedirectResponse
{
$validated = $request->validated();
$type = new SubjectType();
$type->name = $validated['name'];
$type->description = $validated['description'];
$type->slug = $validated['slug'];
$type->position = $validated['position'];
$type->save();
return redirect()->route('subject_types.index');
}
public function show(SubjectType $type): View
{
return view('admin.catalog.direction.subject_type.show', compact('type'));
}
public function edit(SubjectType $subjectType): View
{
return view('admin.catalog.direction.subject_type.edit', compact('subjectType'));
}
public function update(UpdateSubjectTypeRequest $request, SubjectType $subjectType): RedirectResponse
{
$validated = $request->validated();
$subjectType->name = $validated['name'];
$subjectType->description = $validated['description'];
$subjectType->slug = $validated['slug'];
$subjectType->position = $validated['position'];
$subjectType->save();
return redirect()->route('subject_types.index');
}
public function destroy(SubjectType $subjectType): RedirectResponse
{
if ($subjectType->entranceExaminations()->exists()) {
return back();
}
$subjectType->delete();
return redirect()->route('subject_types.index');
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Requests\admin\Catalog\Direction;
use Illuminate\Foundation\Http\FormRequest;
class StoreSubjectTypeRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'position' => 'required|int|numeric|max:255',
'name' => 'required|string|max:255|unique:subject_types,name',
'description' => 'string',
'slug' => 'required|string|max:255|unique:subject_types,slug',
];
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests\admin\Catalog\Direction;
use Illuminate\Foundation\Http\FormRequest;
class UpdateSubjectTypeRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'position' => 'required|int|numeric|max:255',
'description' => 'string',
'slug' => [
'string',
'required',
'max:255',
"unique:subject_types,slug,{$this->subject_type->id}",
],
'name' => [
'required',
'string',
'max:255',
"unique:subject_types,name,{$this->subject_type->id}",
],
];
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class SubjectType extends Model
{
use HasFactory;
protected $fillable = [
'id',
'name',
'description',
'slug',
'position',
];
public function entranceExaminations(): HasMany
{
return $this->hasMany('App\Models\EntranceExamination', 'subject_type_id');
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class SubjectTypeFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->name(),
'description' => fake()->text(),
'slug' => fake()->slug(),
'position' => fake()->randomDigit(),
];
}
}

View File

@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('subject_types', function (Blueprint $table) {
$table->id();
$table->integer('position');
$table->string('name');
$table->text('description')->nullable();
$table->string('slug');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('subject_types');
}
};

View File

@ -29,6 +29,7 @@ class DatabaseSeeder extends Seeder
EducationFormSeeder::class, EducationFormSeeder::class,
ExaminationTypeSeeder::class, ExaminationTypeSeeder::class,
SubjectSeeder::class, SubjectSeeder::class,
SubjectTypeSeeder::class,
DirectionSeeder::class, DirectionSeeder::class,
]); ]);

View File

@ -0,0 +1,28 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class SubjectTypeSeeder extends Seeder
{
public function run(): void
{
DB::table('subject_types')->insert([
[
'name' => 'Обязательные',
'description' => 'compulsory',
'position' => '1',
'slug' => 'compulsory',
],
[
'name' => 'Предметы по выбору',
'description' => 'optional',
'position' => '2',
'slug' => 'optional',
],
]);
}
}

View File

@ -1,7 +1,7 @@
@extends('layouts.admin_layout') @extends('layouts.admin_layout')
@section('content') @section('content')
<div class="container"> <div class="container">
<h2>Тип Экзамена</h2> <h2>Предметы</h2>
<br> <br>
<a href="{{ route('subjects.create') }}" class="btn btn-primary">Создать предмет</a> <a href="{{ route('subjects.create') }}" class="btn btn-primary">Создать предмет</a>
<br> <br>

View File

@ -0,0 +1,65 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class=""> Создать Тип предмета</h1>
{{ Form::open(['url' => route('subject_types.store'), 'method' => 'POST', 'class' => '']) }}
<div class="col">
<div class="mt-3">
{{ Form::label('name', 'Название') }}
</div>
<div class="mt-1">
{{ Form::text('name', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('description', 'Описание') }}
</div>
<div class="mt-1">
{{ Form::text('description', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('description') }}
@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::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>
</div>
{{ Form::close() }}
</div>
</div>
@endauth
@endsection

View File

@ -0,0 +1,66 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class="">Изменить тип предмета</h1>
{{ Form::open(['url' => route('subject_types.update', $subjectType), 'method' => 'PATCH', 'class' => '']) }}
<div class="col">
<div class="mt-3">
{{ Form::label('name', 'Название') }}
</div>
<div class="mt-1">
{{ Form::text('name', $subjectType->name, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('description', 'Описание') }}
</div>
<div class="mt-1">
{{ Form::text('description', $subjectType->description, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('description') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('slug', 'URL') }}
</div>
<div class="mt-1">
{{ Form::text('slug', $subjectType->slug, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('slug') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('position', 'Позиция') }}
</div>
<div class="mt-1">
{{ Form::text('position', $subjectType->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>
</div>
{{ Form::close() }}
</div>
</div>
@endauth
@endsection

View File

@ -0,0 +1,41 @@
@extends('layouts.admin_layout')
@section('content')
<div class="container">
<h2>Тип предмета</h2>
<br>
<a href="{{ route('subject_types.create') }}" class="btn btn-primary">Создать тип предмет</a>
<br>
<br>
<table class="table">
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
<tr>
<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>
</tr>
</thead>
<tbody>
@foreach($subjectTypes as $subjectType)
<tr class="">
<td>{{ $subjectType->position }}</td>
<td><a href="{{ route('subject_types.show', $subjectType) }}">{{ $subjectType->name }}</a></td>
<td>{{ Str::words($subjectType->description, 10, '...') }}</td>
<td>{{ $subjectType->slug }}</td>
<td>
<a href="{{ route("subject_types.edit", $subjectType) }}"
class="btn btn-secondary">редактировать</a>
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('subject_types.destroy', $subjectType) }}"
class="btn btn-danger">удалить</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<br>
<br>
</div>
@endsection

View File

@ -0,0 +1,19 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="container mt-4">
<h2>Название</h2>
<p>{{ $type->name }}</p>
<h2>Описание</h2>
<p>{{ $type->description }}</p>
<h2>URL</h2>
<p>{{ $type->slug }}</p>
<h2>Позиция</h2>
<p>{{ $type->position }}</p>
<h2>Вступительные испытания</h2>
@foreach($type->entranceExaminations as $entranceExamination)
<p><a href="{{ route('entrance_examinations.show', $entranceExamination) }}">{{ $entranceExamination->name }}</a></p>
@endforeach
</div>
@endauth
@endsection

View File

@ -60,6 +60,7 @@
<li class="list-group-item"><a href="{{ route('education_forms.index') }}">Формы образования</a></li> <li class="list-group-item"><a href="{{ route('education_forms.index') }}">Формы образования</a></li>
<li class="list-group-item"><a href="{{ route('examination_types.index') }}">Типы Экзаменов</a></li> <li class="list-group-item"><a href="{{ route('examination_types.index') }}">Типы Экзаменов</a></li>
<li class="list-group-item"><a href="{{ route('subjects.index') }}">Предметы</a></li> <li class="list-group-item"><a href="{{ route('subjects.index') }}">Предметы</a></li>
<li class="list-group-item"><a href="{{ route('subject_types.index') }}">Типы Предметов</a></li>
</ul> </ul>
</aside> </aside>
<main class="col-10">@yield('content')</main> <main class="col-10">@yield('content')</main>

View File

@ -6,6 +6,7 @@ use App\Http\Controllers\admin\Catalog\Direction\EducationFormController;
use App\Http\Controllers\admin\Catalog\Direction\EducationLevelController; use App\Http\Controllers\admin\Catalog\Direction\EducationLevelController;
use App\Http\Controllers\admin\Catalog\Direction\ExaminationTypeController; use App\Http\Controllers\admin\Catalog\Direction\ExaminationTypeController;
use App\Http\Controllers\admin\Catalog\Direction\SubjectController; use App\Http\Controllers\admin\Catalog\Direction\SubjectController;
use App\Http\Controllers\admin\Catalog\Direction\SubjectTypeController;
use App\Http\Controllers\admin\Catalog\DirectionController; use App\Http\Controllers\admin\Catalog\DirectionController;
use App\Http\Controllers\admin\Catalog\EducationalInstitutionController; use App\Http\Controllers\admin\Catalog\EducationalInstitutionController;
use App\Http\Controllers\admin\Catalog\FacultyController; use App\Http\Controllers\admin\Catalog\FacultyController;
@ -49,6 +50,9 @@ Route::middleware(['auth', 'verified'])->prefix('admin')->group(function () {
Route::resource('/subjects', SubjectController::class) Route::resource('/subjects', SubjectController::class)
->scoped(['subject' => 'slug']); ->scoped(['subject' => 'slug']);
Route::resource('/subject_types', SubjectTypeController::class)
->scoped(['subject_type' => 'slug']);
Route::resources([ Route::resources([
'/documents' => DocumentController::class, '/documents' => DocumentController::class,
'/users' => UserController::class, '/users' => UserController::class,

View File

@ -0,0 +1,102 @@
<?php
namespace Tests\Feature\admin\catalog\direction;
use App\Models\Subject;
use App\Models\SubjectType;
use App\Models\User;
use Tests\TestCase;
class SubjectTypeTest extends TestCase
{
private User $user;
private SubjectType $type;
private array $data;
protected function setUp(): void
{
parent::setUp();
$this->type = SubjectType::factory()->create();
$this->data = SubjectType::factory()->make()->only([
'name',
'description',
'position',
'slug',
]);
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexSubjectTypesPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('subject_types.index'));
$response->assertOk();
}
public function testCreateSubjectTypePage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('subject_types.create'));
$response->assertOk();
}
public function testStoreSubjectType(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('subject_types.store', $this->data));
$response->assertRedirect(route('subject_types.index'));
$this->assertDatabaseHas('subject_types', $this->data);
}
public function testShowSubjectTypePage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('subject_types.show', $this->type));
$response->assertOk();
}
public function testEditSubjectTypePage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('subject_types.edit', $this->type));
$response->assertOk();
}
public function testUpdateSubjectType(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('subject_types.update', $this->type), $this->data);
$response->assertRedirect(route('subject_types.index'));
$this->assertDatabaseHas('subject_types', $this->data);
}
public function testDestroySubjectType(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('subject_types.destroy', $this->type));
$response->assertRedirect(route('subject_types.index'));
$this->assertDatabaseMissing('subject_types', $this->type->toArray());
}
}