refactor ClientScheduleController; replace complex query logic with ListSchedulesAction for improved maintainability and clarity
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user