add Period resource
Tests & Lint & Deploy to Railway / build (2.6.6, 20.x, 8.3) (push) Successful in 1m48s Details
Tests & Lint & Deploy to Railway / deploy (push) Successful in 14s Details

This commit is contained in:
aslan 2024-02-26 11:34:33 +03:00
parent 88cf25014c
commit cc64e20ad5
15 changed files with 591 additions and 0 deletions

View File

@ -0,0 +1,90 @@
<?php
namespace App\Http\Controllers\admin\Catalog\Direction;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Catalog\Direction\StoreCostRequest;
use App\Http\Requests\admin\Catalog\Direction\StorePeriodRequest;
use App\Http\Requests\admin\Catalog\Direction\UpdateCostRequest;
use App\Http\Requests\admin\Catalog\Direction\UpdatePeriodRequest;
use App\Models\Direction;
use App\Models\EducationForm;
use App\Models\Period;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
class PeriodController extends Controller
{
public function index(): View
{
$periods = Period::all();
return view('admin.catalog.direction.period.index', compact('periods'));
}
public function create(): View
{
$directions = Direction::pluck('name', 'id');
$educationForms = EducationForm::pluck('name', 'id');
return view(
'admin.catalog.direction.period.create',
compact(
'directions',
'educationForms',
)
);
}
public function store(StorePeriodRequest $request): RedirectResponse
{
$validated = $request->validated();
$period = new Period();
$period->position = $validated['position'];
$period->description = $validated['description'];
$period->period = $validated['period'];
$period->education_form_id = $validated['education_form_id'];
$period->direction_id = $validated['direction_id'];
$period->save();
return redirect()->route('periods.index');
}
public function show(Period $period): View
{
return view('admin.catalog.direction.period.show', compact('period'));
}
public function edit(Period $period): View
{
$directions = Direction::pluck('name', 'id');
$educationForms = EducationForm::pluck('name', 'id');
return view(
'admin.catalog.direction.period.edit',
compact(
'period',
'directions',
'educationForms',
)
);
}
public function update(UpdatePeriodRequest $request, Period $period): RedirectResponse
{
$validated = $request->validated();
$period->position = $validated['position'];
$period->description = $validated['description'];
$period->period = $validated['period'];
$period->education_form_id = $validated['education_form_id'];
$period->direction_id = $validated['direction_id'];
$period->save();
return redirect()->route('periods.index');
}
public function destroy(Period $period): RedirectResponse
{
$period->delete();
return redirect()->route('periods.index');
}
}

View File

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

View File

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

32
app/Models/Period.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Period extends Model
{
use HasFactory;
protected $fillable = [
'id',
'position',
'description',
'period',
'education_form_id',
'place_type_id',
'direction_id',
];
public function direction(): BelongsTo
{
return $this->belongsTo(Direction::class);
}
public function educationForm(): BelongsTo
{
return $this->belongsTo(EducationForm::class);
}
}

View File

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

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('periods', function (Blueprint $table) {
$table->id();
$table->integer('position');
$table->integer('period');
$table->foreignId('education_form_id')->constrained('education_forms');
$table->foreignId('direction_id')->constrained('directions');
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('periods');
}
};

View File

@ -36,6 +36,7 @@ class DatabaseSeeder extends Seeder
PlaceSeeder::class,
CostSeeder::class,
DirectionProfileSeeder::class,
PeriodSeeder::class,
]);
$this->call([

View File

@ -0,0 +1,30 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class PeriodSeeder extends Seeder
{
public function run(): void
{
DB::table('periods')->insert([
[
'position' => 1,
'period' => 4.5,
'education_form_id' => 1,
'direction_id' => 1,
'description' => 'срок обучения 4.5 года',
],
[
'position' => 1,
'period' => 5,
'education_form_id' => 2,
'direction_id' => 1,
'description' => 'срок обучения 5 года',
],
]);
}
}

View File

@ -0,0 +1,79 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class="">Создать срок обучения</h1>
{{ Form::open(['url' => route('periods.store'), 'method' => 'POST', 'class' => '']) }}
<div class="col">
<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::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('period', 'Срок обучения') }}
</div>
<div class="mt-1">
{{ Form::text('period', '', ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('period') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('direction_id', 'Направление подготовки') }}
</div>
<div class="mt-1">
{{ Form::select('direction_id', $directions, null, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('direction_id') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('education_form_id', 'Форма обучения') }}
</div>
<div class="mt-1">
{{ Form::select('education_form_id', $educationForms, null, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('education_form_id') }}
@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,79 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class="">Изменить Стоимость</h1>
{{ Form::open(['url' => route('periods.update', $period), 'method' => 'PATCH', 'class' => '']) }}
<div class="col">
<div class="mt-3">
{{ Form::label('position', 'Позиция') }}
</div>
<div class="mt-1">
{{ Form::text('position', $period->position, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('position') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('description', 'Описание') }}
</div>
<div class="mt-1">
{{ Form::text('description', $period->description, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('description') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('period', 'Срок обучения') }}
</div>
<div class="mt-1">
{{ Form::text('period', $period->period, ['class' => 'form-control']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('period') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('direction_id', 'Направление подготовки') }}
</div>
<div class="mt-1">
{{ Form::select('direction_id', $directions, $period->direction->id, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('direction_id') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('education_form_id', 'Форма обучения') }}
</div>
<div class="mt-1">
{{ Form::select('education_form_id', $educationForms, $period->educationForm->id, ['class' => 'form-select']) }}
</div>
<div>
@if ($errors->any())
{{ $errors->first('education_form_id') }}
@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,45 @@
@extends('layouts.admin_layout')
@section('content')
<div class="container">
<h2>Срок обучения</h2>
<br>
<a href="{{ route('periods.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">направление</th>
<th scope="col">Срок обучения</th>
<th scope="col">действия</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach($periods as $period)
<tr class="">
<td>{{ $period->position }}</td>
<td>{{ Str::words($period->description, 10, '...') }}</td>
<td><a href="{{ route('education_forms.show', $period->educationForm) }}">{{ $period->educationForm->name }}</a></td>
<td><a href="{{ route('directions.show', $period->direction) }}">{{ $period->direction->name }}</a></td>
<td>{{ $period->period }}</td>
<td>
<a href="{{ route("periods.show", $period) }}"
class="btn btn-info">посмотреть</a>
<a href="{{ route("periods.edit", $period) }}"
class="btn btn-secondary">редактировать</a>
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('periods.destroy', $period) }}"
class="btn btn-danger">удалить</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<br>
<br>
</div>
@endsection

View File

@ -0,0 +1,17 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="container mt-4">
<h2>позиция</h2>
<p>{{ $period->position }}</p>
<h2>описание</h2>
<p>{{ $period->description }}</p>
<h2>Форма обучения</h2>
<p>{{ $period->educationForm->name }}</p>
<h2>Направление</h2>
<p>{{ $period->direction->name }}</p>
<h2>Срок обучения</h2>
<p>{{ $period->period }}</p>
</div>
@endauth
@endsection

View File

@ -65,6 +65,7 @@
<li class="list-group-item"><a href="{{ route('places.index') }}">Кол-во Мест</a></li>
<li class="list-group-item"><a href="{{ route('place_types.index') }}">Типы Мест</a></li>
<li class="list-group-item"><a href="{{ route('costs.index') }}">Стоимость об.</a></li>
<li class="list-group-item"><a href="{{ route('periods.index') }}">Срок. обучения</a></li>
<li class="list-group-item"><a href="{{ route('direction_profiles.index') }}">Профили подготовки</a></li>
</ul>
</aside>

View File

@ -8,6 +8,7 @@ use App\Http\Controllers\admin\Catalog\Direction\EducationFormController;
use App\Http\Controllers\admin\Catalog\Direction\EducationLevelController;
use App\Http\Controllers\admin\Catalog\Direction\EntranceExaminationController;
use App\Http\Controllers\admin\Catalog\Direction\ExaminationTypeController;
use App\Http\Controllers\admin\Catalog\Direction\PeriodController;
use App\Http\Controllers\admin\Catalog\Direction\PlaceController;
use App\Http\Controllers\admin\Catalog\Direction\PlaceTypeController;
use App\Http\Controllers\admin\Catalog\Direction\SubjectController;
@ -65,6 +66,7 @@ Route::middleware(['auth', 'verified'])->prefix('admin')->group(function () {
Route::resource('/places', PlaceController::class);
Route::resource('/costs', CostController::class);
Route::resource('/periods', PeriodController::class);
Route::resource('/direction_profiles', DirectionProfileController::class)
->scoped(['direction_profile' => 'slug']);

View File

@ -0,0 +1,116 @@
<?php
namespace Tests\Feature\admin\catalog\direction;
use App\Models\Cost;
use App\Models\Department;
use App\Models\Direction;
use App\Models\EducationalInstitution;
use App\Models\EducationForm;
use App\Models\EducationLevel;
use App\Models\Faculty;
use App\Models\Period;
use App\Models\User;
use Tests\TestCase;
class PeriodTest extends TestCase
{
private User $user;
private Period $period;
private array $data;
protected function setUp(): void
{
parent::setUp();
EducationalInstitution::factory()->create();
Faculty::factory()->create();
Department::factory()->create();
EducationLevel::factory()->create();
EducationForm::factory()->create();
Direction::factory()->create();
$this->period = Period::factory()->create();
$this->data = Period::factory()->make()->only([
'position',
'description',
'period',
'education_form_id',
'direction_id',
]);
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexPeriodsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('periods.index'));
$response->assertOk();
}
public function testCreatePeriodPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('periods.create'));
$response->assertOk();
}
public function testStorePeriod(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('periods.store', $this->data));
$response->assertRedirect(route('periods.index'));
$this->assertDatabaseHas('periods', $this->data);
}
public function testShowPeriod(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('periods.show', $this->period));
$response->assertOk();
}
public function testEditPeriodPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('periods.edit', $this->period));
$response->assertOk();
}
public function testUpdatePeriod(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('periods.update', $this->period), $this->data);
$response->assertRedirect(route('periods.index'));
$this->assertDatabaseHas('periods', $this->data);
}
public function testDestroyPeriod(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('periods.destroy', $this->period));
$response->assertRedirect(route('periods.index'));
$this->assertDatabaseMissing('periods', $this->period->toArray());
}
}