refactor ClientDepartmentController and ClientFacultyController; replace caching logic with action classes for department and faculty retrieval, enhancing code clarity and maintainability

This commit is contained in:
F4ilji
2025-07-20 18:02:14 +05:00
parent 5179f45ef9
commit 3cac482409
11 changed files with 259 additions and 144 deletions
@@ -2,92 +2,29 @@
namespace App\Containers\InstituteStructure\UI\WEB\Controllers;
use App\Containers\InstituteStructure\Models\Department;
use App\Containers\InstituteStructure\Models\Faculty;
use App\Containers\InstituteStructure\UI\WEB\Transformers\DepartmentPreviewResource;
use App\Containers\InstituteStructure\Actions\FindDepartmentBySlugAction;
use App\Containers\InstituteStructure\UI\WEB\Transformers\DepartmentResource;
use App\Ship\Contracts\SeoServiceInterface;
use App\Ship\Controllers\Controller;
use App\Ship\Enums\CacheKeys;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
class ClientDepartmentController extends Controller
{
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
public function __construct(
readonly SeoServiceInterface $seoPageProvider,
private readonly FindDepartmentBySlugAction $findDepartmentBySlugAction
) {
}
public function show(string $facultySlug, string $departmentSlug)
public function show(string $facultySlug, string $departmentSlug): \Inertia\Response
{
// Ключ для кеширования
$cacheKey = "{$facultySlug}_{$departmentSlug}";
$data = $this->findDepartmentBySlugAction->run($facultySlug, $departmentSlug);
// Кешируем факультет
$faculty = Cache::remember(
CacheKeys::FACULTY_PREFIX->value . $facultySlug,
now()->addDay(),
function () use ($facultySlug) {
return Faculty::query()
->where('slug', $facultySlug)
->first();
}
);
// Кешируем список активных кафедр факультета
$departments = Cache::remember(
CacheKeys::DEPARTMENTS_PREFIX->value . 'active_' . $faculty->id,
now()->addDay(),
function () use ($faculty) {
return DepartmentPreviewResource::collection(
Department::query()
->where('is_active', true)
->where('faculty_id', $faculty->id)
->get()
);
}
);
$departmentModel = Cache::remember(
CacheKeys::DEPARTMENT_PREFIX->value . $cacheKey,
now()->addDay(),
function () use ($departmentSlug) {
return Department::query()
->where('slug', $departmentSlug)
->where('is_active', true)
->with([
'faculty',
'workers' => fn ($query) => $query->orderBy('sort', 'asc'),
'teachers' => fn ($query) => $query->orderBy('sort', 'asc'),
'programs' => fn ($query) => $query->orderBy('sort', 'asc'),
'workers.userDetail',
'teachers.userDetail',
'programs.directionStudy',
'seo'
])
->firstOrFail();
}
);
$seo = Cache::remember(
CacheKeys::DEPARTMENT_PREFIX->value . 'seo_' . $cacheKey,
now()->addDay(),
function () use ($departmentModel) {
return $this->seoPageProvider->getSeoForModel($departmentModel);
}
);
$department = new DepartmentResource($departmentModel);
$directions = Cache::remember(
CacheKeys::DEPARTMENT_PREFIX->value . 'directions_' . $cacheKey,
now()->addDay(),
function () use ($department) {
return $this->groupProgramsByDirection($department->programs);
}
);
$seo = $this->seoPageProvider->getSeoForModel($data['departmentModel']);
$department = $data['department'];
$departments = $data['departments'];
$directions = $data['directions'];
return Inertia::render('Client/Departments/Show', compact(
'department',
@@ -96,13 +33,4 @@ class ClientDepartmentController extends Controller
'seo',
));
}
private function groupProgramsByDirection(Collection $programs): Collection
{
// Группируем программы по имени направления
return $programs->groupBy(function ($program) {
return $program->directionStudy->code . " " . $program->directionStudy->name; // Используем имя направления как ключ
});
}
}