Rework frontend
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\CacheKeys;
|
||||
use App\Enums\FormEducation;
|
||||
use App\Http\Resources\AdditionalEducationCategoryPreviewResource;
|
||||
use App\Http\Resources\AdditionalEducationCategoryResource;
|
||||
@@ -14,128 +15,148 @@ use App\Models\AdditionalEducation;
|
||||
use App\Models\AdditionalEducationCategory;
|
||||
use App\Models\DirectionAdditionalEducation;
|
||||
use App\Models\Page;
|
||||
use App\Services\App\Breadcrumb\BreadcrumbService;
|
||||
use App\Services\App\Seo\SeoPageProvider;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class ClientAdditionalEducationController extends Controller
|
||||
{
|
||||
public function __construct(readonly SeoPageProvider $seoPageProvider){}
|
||||
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$cacheKey = md5(serialize([
|
||||
'direction' => $request->input('direction'),
|
||||
'form' => $request->input('form'),
|
||||
'category' => $request->input('category'),
|
||||
]));
|
||||
|
||||
$directionAdditionalEducations = DirectionAdditionalEducationResource::collection(
|
||||
DirectionAdditionalEducation::query()
|
||||
->where('is_active', true)
|
||||
->whereHas('additionalEducationCategories', function ($q) {
|
||||
$q->whereHas('additionalEducations');
|
||||
})->get());
|
||||
|
||||
$additionalEducations = AdditionalEducationCategoryResource::collection(AdditionalEducationCategory::query()
|
||||
->WithActivePrograms()
|
||||
->where('is_active', '=', true)
|
||||
->when($request->input('direction'), function ($q, $direction) {
|
||||
$q->whereHas('direction', function ($query) use ($direction) {
|
||||
$query->where('slug', $direction);
|
||||
});
|
||||
})
|
||||
->when(request()->input('form'), function ($query, $form) {
|
||||
$query->whereHas('additionalEducations', function ($q) use ($form) {
|
||||
$q->where('form_education', FormEducation::fromName($form));
|
||||
});
|
||||
$query->with(['additionalEducations' => function ($q) use ($form) {
|
||||
$q->where('form_education', FormEducation::fromName($form));
|
||||
}]);
|
||||
})
|
||||
->when(request()->input('category'), function ($query) {
|
||||
$slugs = request()->input('category');
|
||||
if (is_array($slugs)) {
|
||||
$query->whereIn('slug', $slugs);
|
||||
}
|
||||
})
|
||||
->has('additionalEducations')
|
||||
->get());
|
||||
|
||||
$categories = AdditionalEducationCategoryPreviewResource::collection(
|
||||
AdditionalEducationCategory::query()
|
||||
->where('is_active', true)
|
||||
->has('additionalEducations')
|
||||
->get()
|
||||
// Основные данные (кешируются)
|
||||
$directionAdditionalEducations = Cache::remember(
|
||||
CacheKeys::ADDITIONAL_EDUCATIONAL_PROGRAMS_PREFIX->value . 'directions_' . $cacheKey,
|
||||
now()->addDay(),
|
||||
function () {
|
||||
return DirectionAdditionalEducationResource::collection(
|
||||
DirectionAdditionalEducation::query()
|
||||
->where('is_active', true)
|
||||
->whereHas('additionalEducationCategories', fn ($q) => $q->whereHas('additionalEducations'))
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$additionalEducations = Cache::remember(
|
||||
CacheKeys::ADDITIONAL_EDUCATIONAL_PROGRAMS_PREFIX->value . $cacheKey,
|
||||
now()->addDay(),
|
||||
function () use ($request) {
|
||||
return AdditionalEducationCategoryResource::collection(
|
||||
AdditionalEducationCategory::query()
|
||||
->WithActivePrograms()
|
||||
->where('is_active', true)
|
||||
->when($request->direction, fn ($q, $direction) =>
|
||||
$q->whereHas('direction', fn ($query) => $query->where('slug', $direction))
|
||||
)
|
||||
->when($request->form, fn ($query, $form) =>
|
||||
$query->whereHas('additionalEducations', fn ($q) =>
|
||||
$q->where('form_education', FormEducation::fromName($form))
|
||||
)
|
||||
->when($request->category, fn ($query) =>
|
||||
is_array($request->category)
|
||||
? $query->whereIn('slug', $request->category)
|
||||
: $query
|
||||
)
|
||||
->has('additionalEducations')
|
||||
->get()
|
||||
));
|
||||
}
|
||||
);
|
||||
|
||||
$categories = Cache::remember(
|
||||
CacheKeys::ADDITIONAL_EDUCATIONAL_PROGRAMS_PREFIX->value . 'categories',
|
||||
now()->addWeek(),
|
||||
function () {
|
||||
return AdditionalEducationCategoryPreviewResource::collection(
|
||||
AdditionalEducationCategory::query()
|
||||
->where('is_active', true)
|
||||
->has('additionalEducations')
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// Динамические данные (не кешируются)
|
||||
$categoriesContent = [];
|
||||
if (request()->input('category')) {
|
||||
foreach (request()->input('category') as $item) {
|
||||
$categoriesContent[$item] = new AdditionalEducationCategoryResource(AdditionalEducationCategory::where('slug', $item)->first());
|
||||
if ($request->category) {
|
||||
foreach ((array)$request->category as $item) {
|
||||
$categoriesContent[$item] = new AdditionalEducationCategoryResource(
|
||||
AdditionalEducationCategory::where('slug', $item)->first()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$forms_education = [];
|
||||
foreach (FormEducation::cases() as $case) {
|
||||
$forms_education[$case->name] = $case->getLabel();
|
||||
}
|
||||
$forms_education = array_reduce(
|
||||
FormEducation::cases(),
|
||||
fn ($acc, $case) => $acc + [$case->name => $case->getLabel()],
|
||||
[]
|
||||
);
|
||||
|
||||
$filters = [
|
||||
'direction_filter' => [
|
||||
'type' => 'direction',
|
||||
'value' => request()->input('direction'),
|
||||
'value' => $request->input('direction'),
|
||||
'param' => 'direction'
|
||||
],
|
||||
'form_education_filter' => [
|
||||
'type' => 'form',
|
||||
'value' => request()->input('form'),
|
||||
'value' => $request->input('form'),
|
||||
'param' => 'form'
|
||||
],
|
||||
'category_filter' => [
|
||||
'type' => 'category',
|
||||
'value' => request()->input('category'),
|
||||
'value' => $request->input('category'),
|
||||
'param' => 'category',
|
||||
'content' => $categoriesContent,
|
||||
],
|
||||
];
|
||||
|
||||
$routeUrl = route('client.additionalEducation.index');
|
||||
$path = ltrim(parse_url($routeUrl, PHP_URL_PATH), '/');
|
||||
$seo = $this->seoPageProvider->getSeoForCurrentPage();
|
||||
|
||||
$page = Page::where('path', '=', $path)->with('section.pages.section', 'section.mainSection')->first();
|
||||
|
||||
if (isset($page->section)) {
|
||||
$breadcrumbs = [
|
||||
'mainSection' => new ClientBreadcrumbSection($page->section->mainSection),
|
||||
'subSection' => new ClientBreadcrumbSubSection($page->section),
|
||||
'page' => new ClientBreadcrumbPage($page),
|
||||
];
|
||||
} else {
|
||||
$breadcrumbs = null;
|
||||
}
|
||||
|
||||
return Inertia::render('Client/Additional-educations/Index',
|
||||
compact(
|
||||
'directionAdditionalEducations',
|
||||
'additionalEducations',
|
||||
'filters',
|
||||
'forms_education',
|
||||
'categories',
|
||||
'breadcrumbs'
|
||||
));
|
||||
return Inertia::render('Client/Additional-educations/Index', compact(
|
||||
'directionAdditionalEducations',
|
||||
'additionalEducations',
|
||||
'filters',
|
||||
'forms_education',
|
||||
'categories',
|
||||
'seo'
|
||||
));
|
||||
}
|
||||
|
||||
public function show(string $slug)
|
||||
{
|
||||
$additionalEducation = new AdditionalEducationResource(AdditionalEducation::query()->with('category.direction')->where('slug', $slug)->first());
|
||||
$routeUrl = route('client.additionalEducation.index');
|
||||
$path = ltrim(parse_url($routeUrl, PHP_URL_PATH), '/');
|
||||
// Кешируем основную программу дополнительного образования
|
||||
[$additionalEducation, $seo] = Cache::remember(
|
||||
CacheKeys::ADDITIONAL_EDUCATIONAL_PROGRAM_PREFIX->value . $slug,
|
||||
now()->addDay(),
|
||||
function () use ($slug) {
|
||||
$additionalEducation = AdditionalEducation::query()
|
||||
->with('category.direction')
|
||||
->where('slug', $slug)
|
||||
->first();
|
||||
$seo = $this->seoPageProvider->getSeoForModel($additionalEducation);
|
||||
return [
|
||||
new AdditionalEducationResource($additionalEducation),
|
||||
$seo
|
||||
];
|
||||
}
|
||||
);
|
||||
|
||||
$page = Page::where('path', '=', $path)->with('section.pages.section', 'section.mainSection')->first();
|
||||
// SEO-данные берём из кешированного ресурса
|
||||
|
||||
if (isset($page->section)) {
|
||||
$breadcrumbs = [
|
||||
'mainSection' => new ClientBreadcrumbSection($page->section->mainSection),
|
||||
'subSection' => new ClientBreadcrumbSubSection($page->section),
|
||||
'page' => new ClientBreadcrumbPage($page),
|
||||
];
|
||||
} else {
|
||||
$breadcrumbs = null;
|
||||
}
|
||||
|
||||
$seo = $additionalEducation->seo ?? null;
|
||||
|
||||
return Inertia::render('Client/Additional-educations/Show', compact('additionalEducation', 'breadcrumbs', 'seo'));
|
||||
}
|
||||
}
|
||||
return Inertia::render('Client/Additional-educations/Show', compact(
|
||||
'additionalEducation',
|
||||
'seo'
|
||||
));
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user