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:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Actions;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Department;
|
||||
use App\Containers\InstituteStructure\Tasks\GetActiveDepartmentsByFacultyIdTask;
|
||||
use App\Containers\InstituteStructure\Tasks\GetDepartmentBySlugTask;
|
||||
use App\Containers\InstituteStructure\Tasks\GetFacultyBySlugOnlyTask;
|
||||
use App\Containers\InstituteStructure\Tasks\GroupProgramsByDirectionTask;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\DepartmentResource;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class FindDepartmentBySlugAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetFacultyBySlugOnlyTask $getFacultyBySlugOnlyTask,
|
||||
private readonly GetActiveDepartmentsByFacultyIdTask $getActiveDepartmentsByFacultyIdTask,
|
||||
private readonly GetDepartmentBySlugTask $getDepartmentBySlugTask,
|
||||
private readonly GroupProgramsByDirectionTask $groupProgramsByDirectionTask
|
||||
) {
|
||||
}
|
||||
|
||||
public function run(string $facultySlug, string $departmentSlug): array
|
||||
{
|
||||
$cacheKey = "{$facultySlug}_{$departmentSlug}";
|
||||
|
||||
$faculty = $this->getFacultyBySlugOnlyTask->run($facultySlug);
|
||||
|
||||
$departments = $this->getActiveDepartmentsByFacultyIdTask->run($faculty->id);
|
||||
|
||||
$departmentModel = $this->getDepartmentBySlugTask->run($departmentSlug, $cacheKey);
|
||||
|
||||
$department = new DepartmentResource($departmentModel);
|
||||
|
||||
$directions = $this->groupProgramsByDirectionTask->run($department->programs);
|
||||
|
||||
return compact('department', 'departments', 'directions', 'departmentModel');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Actions;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Containers\InstituteStructure\Tasks\GetFacultyBySlugTask;
|
||||
|
||||
class FindFacultyBySlugAction
|
||||
{
|
||||
public function __construct(private readonly GetFacultyBySlugTask $getFacultyBySlugTask)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $slug): Faculty
|
||||
{
|
||||
return $this->getFacultyBySlugTask->run($slug);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Actions;
|
||||
|
||||
use App\Containers\InstituteStructure\Tasks\GetActiveFacultiesTask;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class ListFacultiesAction
|
||||
{
|
||||
public function __construct(private readonly GetActiveFacultiesTask $getActiveFacultiesTask)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(): AnonymousResourceCollection
|
||||
{
|
||||
return $this->getActiveFacultiesTask->run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Department;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\DepartmentPreviewResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetActiveDepartmentsByFacultyIdTask
|
||||
{
|
||||
public function run(int $facultyId): AnonymousResourceCollection
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::DEPARTMENTS_PREFIX->value . 'active_' . $facultyId,
|
||||
now()->addDay(),
|
||||
function () use ($facultyId) {
|
||||
return DepartmentPreviewResource::collection(
|
||||
Department::query()
|
||||
->where('is_active', true)
|
||||
->where('faculty_id', $facultyId)
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\FacultyPreviewResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetActiveFacultiesTask
|
||||
{
|
||||
public function run(): AnonymousResourceCollection
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::FACULTIES_PREFIX->value . 'active_list',
|
||||
now()->addDay(), // Кешируем на 1 день
|
||||
function () {
|
||||
return FacultyPreviewResource::collection(
|
||||
Faculty::query()
|
||||
->where('is_active', true)
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Department;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetDepartmentBySlugTask
|
||||
{
|
||||
public function run(string $departmentSlug, string $cacheKey): Department
|
||||
{
|
||||
return 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();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetFacultyBySlugOnlyTask
|
||||
{
|
||||
public function run(string $facultySlug): Faculty
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::FACULTY_PREFIX->value . $facultySlug,
|
||||
now()->addDay(),
|
||||
function () use ($facultySlug) {
|
||||
return Faculty::query()
|
||||
->where('slug', $facultySlug)
|
||||
->firstOrFail();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetFacultyBySlugTask
|
||||
{
|
||||
public function run(string $slug): Faculty
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::FACULTY_PREFIX->value . $slug,
|
||||
now()->addDay(),
|
||||
function () use ($slug) {
|
||||
return Faculty::where('slug', $slug)
|
||||
->where('is_active', true)
|
||||
->with(['departments.faculty',
|
||||
'workers' => fn ($query) => $query->orderBy('sort', 'asc'),
|
||||
'workers.userDetail',
|
||||
'seo'
|
||||
])
|
||||
->firstOrFail();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class GroupProgramsByDirectionTask
|
||||
{
|
||||
public function run(Collection $programs): Collection
|
||||
{
|
||||
// Группируем программы по имени направления
|
||||
return $programs->groupBy(function ($program) {
|
||||
return $program->directionStudy->code . " " . $program->directionStudy->name; // Используем имя направления как ключ
|
||||
});
|
||||
}
|
||||
}
|
||||
+12
-84
@@ -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; // Используем имя направления как ключ
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,85 +2,40 @@
|
||||
|
||||
namespace App\Containers\InstituteStructure\UI\WEB\Controllers;
|
||||
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Faculty;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\FacultyPreviewResource;
|
||||
use App\Containers\InstituteStructure\Actions\FindFacultyBySlugAction;
|
||||
use App\Containers\InstituteStructure\Actions\ListFacultiesAction;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\FacultyResource;
|
||||
use App\Ship\Contracts\SeoServiceInterface;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Inertia\Inertia;
|
||||
|
||||
|
||||
class ClientFacultyController extends Controller
|
||||
{
|
||||
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
||||
public function __construct(
|
||||
readonly SeoServiceInterface $seoPageProvider,
|
||||
private readonly ListFacultiesAction $listFacultiesAction,
|
||||
private readonly FindFacultyBySlugAction $findFacultyBySlugAction
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
public function index(Request $request)
|
||||
public function index(): \Inertia\Response
|
||||
{
|
||||
$faculties = Cache::remember(
|
||||
CacheKeys::FACULTIES_PREFIX->value . 'active_list',
|
||||
now()->addDay(), // Кешируем на 1 день
|
||||
function () {
|
||||
return FacultyPreviewResource::collection(
|
||||
Faculty::query()
|
||||
->where('is_active', true)
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
$faculties = $this->listFacultiesAction->run();
|
||||
|
||||
$seo = $this->seoPageProvider->getSeoForCurrentPage();
|
||||
|
||||
return Inertia::render('Client/Faculties/Index', compact('faculties', 'seo'));
|
||||
}
|
||||
|
||||
|
||||
public function show(string $slug)
|
||||
public function show(string $slug): \Inertia\Response
|
||||
{
|
||||
// Кешируем список факультетов
|
||||
$faculties = Cache::remember(
|
||||
CacheKeys::FACULTIES_PREFIX->value . 'active_list',
|
||||
now()->addDay(),
|
||||
function () {
|
||||
return FacultyPreviewResource::collection(
|
||||
Faculty::query()
|
||||
->where('is_active', true)
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
$faculties = $this->listFacultiesAction->run();
|
||||
|
||||
// Кешируем данные конкретного факультета
|
||||
$faculty = Cache::remember(
|
||||
CacheKeys::FACULTY_PREFIX->value . $slug,
|
||||
now()->addDay(),
|
||||
function () use ($slug) {
|
||||
return Faculty::where('slug', $slug)
|
||||
->where('is_active', true)
|
||||
->with(['departments.faculty',
|
||||
'workers' => fn ($query) => $query->orderBy('sort', 'asc'),
|
||||
'workers.userDetail',
|
||||
'seo'
|
||||
])
|
||||
->firstOrFail();
|
||||
}
|
||||
);
|
||||
|
||||
$seo = Cache::remember(
|
||||
CacheKeys::FACULTY_PREFIX->value . "SEO_" . $slug,
|
||||
now()->addDay(),
|
||||
function () use ($faculty) {
|
||||
return $this->seoPageProvider->getSeoForModel($faculty);
|
||||
}
|
||||
);
|
||||
|
||||
$faculty = new FacultyResource($faculty);
|
||||
$facultyData = $this->findFacultyBySlugAction->run($slug);
|
||||
|
||||
$seo = $this->seoPageProvider->getSeoForModel($facultyData);
|
||||
|
||||
$faculty = new FacultyResource($facultyData);
|
||||
|
||||
return Inertia::render('Client/Faculties/Show', compact('faculty', 'faculties', 'seo'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user