refactor ClientScheduleController; replace complex query logic with ListSchedulesAction for improved maintainability and clarity
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Schedule\Actions;
|
||||
|
||||
use App\Containers\Schedule\Tasks\GetFilteredEducationalGroupsTask;
|
||||
use App\Containers\Schedule\Tasks\GetAllFormsEducationTask;
|
||||
use App\Containers\Schedule\Tasks\GetAllActiveFacultiesTask;
|
||||
use App\Containers\Schedule\Tasks\BuildScheduleFiltersTask;
|
||||
use App\Containers\Schedule\UI\WEB\Transformers\EducationalGroupResource;
|
||||
use App\Ship\Contracts\SeoServiceInterface;
|
||||
|
||||
class ListSchedulesAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetFilteredEducationalGroupsTask $getFilteredEducationalGroupsTask,
|
||||
private readonly SeoServiceInterface $seoPageProvider,
|
||||
private readonly GetAllFormsEducationTask $getAllFormsEducationTask,
|
||||
private readonly GetAllActiveFacultiesTask $getAllActiveFacultiesTask,
|
||||
private readonly BuildScheduleFiltersTask $buildScheduleFiltersTask,
|
||||
) {}
|
||||
|
||||
public function run(array $filters): array
|
||||
{
|
||||
$educationalGroups = $this->getFilteredEducationalGroupsTask->run($filters);
|
||||
|
||||
$schedulesPaginate = $educationalGroups->toArray();
|
||||
unset($schedulesPaginate['data']);
|
||||
$educationalGroups = EducationalGroupResource::collection($educationalGroups->items());
|
||||
|
||||
$schedulesByFaculty = $educationalGroups->groupBy(function ($group) {
|
||||
return $group->faculty->title;
|
||||
});
|
||||
|
||||
$schedulesByFaculty = $schedulesByFaculty->toArray();
|
||||
|
||||
$forms_education = $this->getAllFormsEducationTask->run();
|
||||
|
||||
$faculties = $this->getAllActiveFacultiesTask->run();
|
||||
|
||||
$filtersData = $this->buildScheduleFiltersTask->run($filters);
|
||||
|
||||
$seo = $this->seoPageProvider->getSeoForCurrentPage();
|
||||
|
||||
return [
|
||||
'filters' => $filtersData,
|
||||
'forms_education' => $forms_education,
|
||||
'schedulesByFaculty' => inertia()->deepMerge(fn() => $schedulesByFaculty),
|
||||
'schedules_paginator' => $schedulesPaginate,
|
||||
'seo' => $seo,
|
||||
'faculties' => $faculties,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Schedule\Tasks;
|
||||
|
||||
use App\Ship\Builders\FilterBuilder;
|
||||
|
||||
class BuildScheduleFiltersTask
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FilterBuilder $filterBuilder
|
||||
) {}
|
||||
|
||||
public function run(array $requestFilters): array
|
||||
{
|
||||
// Сброс фильтров перед новым запуском
|
||||
$this->filterBuilder->reset();
|
||||
|
||||
// 1. Поисковый фильтр
|
||||
$this->filterBuilder->add(
|
||||
key: 'search_filter',
|
||||
type: 'search',
|
||||
value: $requestFilters['search'] ?? null,
|
||||
param: 'search'
|
||||
);
|
||||
|
||||
// 2. Фильтр по форме образования
|
||||
$this->filterBuilder->add(
|
||||
key: 'form_education_filter',
|
||||
type: 'form',
|
||||
value: $requestFilters['form'] ?? null,
|
||||
param: 'form'
|
||||
);
|
||||
|
||||
// 3. Фильтр по факультету
|
||||
$this->filterBuilder->add(
|
||||
key: 'faculty_filter',
|
||||
type: 'faculty',
|
||||
value: $requestFilters['faculty'] ?? null,
|
||||
param: 'faculty'
|
||||
);
|
||||
|
||||
// 4. Фильтр избранного
|
||||
$this->filterBuilder->add(
|
||||
key: 'favorite_filter',
|
||||
type: 'favorite',
|
||||
value: $requestFilters['favorite'] ?? null,
|
||||
param: 'favorite'
|
||||
);
|
||||
|
||||
return $this->filterBuilder->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Schedule\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
|
||||
class GetAllActiveFacultiesTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Faculty::query()->where('is_active', true)->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Schedule\Tasks;
|
||||
|
||||
use App\Ship\Enums\Education\FormEducation;
|
||||
|
||||
class GetAllFormsEducationTask
|
||||
{
|
||||
public function run(): array
|
||||
{
|
||||
$forms_education = [];
|
||||
foreach (FormEducation::cases() as $case) {
|
||||
$forms_education[$case->name] = $case->getLabel();
|
||||
}
|
||||
|
||||
return $forms_education;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Schedule\Tasks;
|
||||
|
||||
use App\Containers\Schedule\Models\EducationalGroup;
|
||||
use App\Ship\Exceptions\NotFoundException;
|
||||
use App\Ship\Enums\Education\FormEducation;
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
|
||||
class GetFilteredEducationalGroupsTask
|
||||
{
|
||||
public function run(array $filters)
|
||||
{
|
||||
$educationalGroups = EducationalGroup::query()
|
||||
->has('schedules')
|
||||
->when(isset($filters['search']), function ($query) use ($filters) {
|
||||
$query->whereRaw('LOWER(title) like ?', ["%".strtolower($filters['search'])."%"]);
|
||||
})
|
||||
->when(isset($filters['favorite']), function ($query) use ($filters) {
|
||||
$query->whereIn('id', $filters['favorite']);
|
||||
})
|
||||
->when(isset($filters['form']), function ($query) use ($filters) {
|
||||
$query->where('education_form_id', FormEducation::fromName($filters['form'])->value);
|
||||
})
|
||||
->when(isset($filters['faculty']), function ($query) use ($filters) {
|
||||
$query->whereHas('faculty', function ($q) use ($filters) {
|
||||
$q->where('slug', $filters['faculty']);
|
||||
});
|
||||
})
|
||||
->with('schedules')
|
||||
->with('faculty')
|
||||
->orderBy('faculty_id')
|
||||
->orderBy('title');
|
||||
|
||||
return $educationalGroups->paginate(10);
|
||||
}
|
||||
}
|
||||
@@ -2,103 +2,22 @@
|
||||
|
||||
namespace App\Containers\Schedule\UI\WEB\Controllers;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Containers\Schedule\Models\EducationalGroup;
|
||||
use App\Containers\Schedule\UI\WEB\Transformers\EducationalGroupResource;
|
||||
use App\Ship\Contracts\SeoServiceInterface;
|
||||
use App\Containers\Schedule\Actions\ListSchedulesAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use App\Ship\Enums\Education\FormEducation;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ClientScheduleController extends Controller
|
||||
{
|
||||
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
||||
public function __construct(
|
||||
private readonly ListSchedulesAction $listSchedulesAction,
|
||||
) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$educationalGroups = EducationalGroup::query()
|
||||
->has('schedules')
|
||||
->when(request()->input('search'), function ($query, $search) {
|
||||
$query->whereRaw('LOWER(title) like ?', ["%".strtolower($search)."%"]);
|
||||
})
|
||||
->when(request()->input('favorite'), function ($query, $favorite) {
|
||||
$query->whereIn('id', $favorite);
|
||||
})
|
||||
->when(request()->input('form'), function ($query, $form) {
|
||||
$query->where('education_form_id', FormEducation::fromName($form)->value);
|
||||
})
|
||||
->when(request()->input('faculty'), function ($query, $facultySlug) {
|
||||
$query->whereHas('faculty', function ($q) use ($facultySlug) {
|
||||
$q->where('slug', $facultySlug);
|
||||
});
|
||||
})
|
||||
->with('schedules')
|
||||
->with('faculty')
|
||||
->orderBy('faculty_id')
|
||||
->orderBy('title')
|
||||
->paginate(10);
|
||||
$filters = $request->only(['search', 'form', 'faculty', 'favorite']);
|
||||
|
||||
$schedulesPaginate = $educationalGroups->toArray();
|
||||
unset($schedulesPaginate['data']);
|
||||
$educationalGroups = EducationalGroupResource::collection($educationalGroups->items());
|
||||
$data = $this->listSchedulesAction->run($filters);
|
||||
|
||||
|
||||
$schedulesByFaculty = $educationalGroups->groupBy(function ($group) {
|
||||
return $group->faculty->title;
|
||||
});
|
||||
|
||||
$schedulesByFaculty = $schedulesByFaculty->toArray();
|
||||
|
||||
$forms_education = [];
|
||||
foreach (FormEducation::cases() as $case) {
|
||||
$forms_education[$case->name] = $case->getLabel();
|
||||
}
|
||||
|
||||
$faculties = Faculty::query()->where('is_active', true)->get();
|
||||
|
||||
$filters = [
|
||||
'form_education_filter' => [
|
||||
'type' => 'form',
|
||||
'value' => request()->input('form'),
|
||||
'param' => 'form'
|
||||
],
|
||||
'faculty_filter' => [
|
||||
'type' => 'faculty',
|
||||
'value' => request()->input('faculty'),
|
||||
'param' => 'faculty'
|
||||
],
|
||||
'search_filter' => [
|
||||
'type' => 'search',
|
||||
'value' => $request->input('search'),
|
||||
'param' => 'search'
|
||||
],
|
||||
'favorite_filter' => [
|
||||
'type' => 'favorite',
|
||||
'value' => $request->input('favorite'),
|
||||
'param' => 'favorite'
|
||||
]
|
||||
];
|
||||
|
||||
$seo = $this->seoPageProvider->getSeoForCurrentPage();
|
||||
|
||||
return inertia()->render(
|
||||
'Client/Schedules/Index',
|
||||
[
|
||||
'filters' => $filters,
|
||||
'forms_education' => $forms_education,
|
||||
'schedulesByFaculty' => inertia()->deepMerge(fn() => $schedulesByFaculty),
|
||||
'schedules_paginator' => $schedulesPaginate,
|
||||
'seo' => $seo,
|
||||
'faculties' => $faculties,
|
||||
]
|
||||
);
|
||||
|
||||
// Возвращаем данные в представление
|
||||
return inertia()->render('Client/Schedules/Index', $data);
|
||||
}
|
||||
|
||||
// public function show($id)
|
||||
// {
|
||||
// $schedule = Schedule::find($id);
|
||||
// return Inertia::render('Client/Schedules/Show', compact('schedule'));
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user