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:
@@ -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