refactor multiple widget controllers; replace caching logic with action classes for data retrieval, enhancing code clarity and maintainability
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\GetActiveAdditionalEducationalProgramsTask;
|
||||
|
||||
class GetAdditionalEducationalProgramsAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetActiveAdditionalEducationalProgramsTask $getActiveAdditionalEducationalProgramsTask
|
||||
) {}
|
||||
|
||||
public function run()
|
||||
{
|
||||
return $this->getActiveAdditionalEducationalProgramsTask->run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\GetPublishedEducationalProgramsTask;
|
||||
|
||||
class GetEducationalProgramsAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetPublishedEducationalProgramsTask $getPublishedEducationalProgramsTask
|
||||
) {}
|
||||
|
||||
public function run()
|
||||
{
|
||||
return $this->getPublishedEducationalProgramsTask->run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\FindPageByIdTask;
|
||||
use App\Containers\Widget\UI\API\Transformers\PageNavigateResource;
|
||||
|
||||
class GetPageAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FindPageByIdTask $findPageByIdTask
|
||||
) {}
|
||||
|
||||
public function run(int $id)
|
||||
{
|
||||
$page = $this->findPageByIdTask->run($id);
|
||||
|
||||
if (isset($page->section)) {
|
||||
$breadcrumbs = [
|
||||
'mainSection' => $page->section->mainSection->title,
|
||||
'subSection' => $page->section->title,
|
||||
'page' => $page->title,
|
||||
];
|
||||
} else {
|
||||
$breadcrumbs = null;
|
||||
}
|
||||
|
||||
return [
|
||||
'page' => new PageNavigateResource($page),
|
||||
'breadcrumbs' => $breadcrumbs
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\GetFilteredPostsTask;
|
||||
|
||||
class GetPostsAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetFilteredPostsTask $getFilteredPostsTask
|
||||
) {}
|
||||
|
||||
public function run(?int $category_id, int $count)
|
||||
{
|
||||
return $this->getFilteredPostsTask->run($category_id, $count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\FindPostByIdTask;
|
||||
|
||||
class GetSinglePostAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FindPostByIdTask $findPostByIdTask
|
||||
) {}
|
||||
|
||||
public function run(int $id)
|
||||
{
|
||||
return $this->findPostByIdTask->run($id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Actions;
|
||||
|
||||
use App\Containers\Widget\Tasks\FindSliderBySlugTask;
|
||||
|
||||
class GetSliderBySlugAction
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FindSliderBySlugTask $findSliderBySlugTask
|
||||
) {}
|
||||
|
||||
public function run(string $slug): ?object
|
||||
{
|
||||
return $this->findSliderBySlugTask->run($slug);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FindPageByIdTask
|
||||
{
|
||||
public function run(int $id)
|
||||
{
|
||||
$cacheKey = 'page_' . md5($id);
|
||||
|
||||
return Cache::remember($cacheKey, now()->addHours(1), function () use ($id) {
|
||||
return Page::where('id', '=', $id)
|
||||
->with('section.pages.section', 'section.mainSection')
|
||||
->firstOrFail();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\Article\Models\Post;
|
||||
use App\Containers\Widget\UI\API\Transformers\PostThumbnailResource;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FindPostByIdTask
|
||||
{
|
||||
public function run(int $id)
|
||||
{
|
||||
$cacheKey = 'post_' . $id;
|
||||
|
||||
return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($id) {
|
||||
return new PostThumbnailResource(
|
||||
Post::query()->with('category')->find($id)->firstOrFail()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\Widget\Models\Slider;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class FindSliderBySlugTask
|
||||
{
|
||||
public function run(string $slug): ?object
|
||||
{
|
||||
$cacheKey = 'slider_' . $slug;
|
||||
|
||||
return Cache::remember($cacheKey, now()->addHour(), function () use ($slug) {
|
||||
$slider = Slider::query()
|
||||
->where('slug', $slug)
|
||||
->where('is_active', true)
|
||||
->with(['slides' => function($query) {
|
||||
$query->where('is_active', true);
|
||||
}])
|
||||
->firstOrFail();
|
||||
|
||||
return $slider ?: null;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducation;
|
||||
use App\Containers\Search\UI\API\Transformers\AdditionalEducationSearchResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetActiveAdditionalEducationalProgramsTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::ADDITIONAL_EDUCATIONAL_PROGRAMS_PREFIX->value . 'search_list',
|
||||
now()->addDay(), // Кешируем на 1 день
|
||||
function () {
|
||||
return AdditionalEducationSearchResource::collection(
|
||||
AdditionalEducation::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('title', 'desc')
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\Article\Enums\PostStatus;
|
||||
use App\Containers\Article\Models\Post;
|
||||
use App\Containers\Widget\UI\API\Transformers\PostThumbnailResource;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetFilteredPostsTask
|
||||
{
|
||||
public function run(?int $category_id, int $count)
|
||||
{
|
||||
$cacheKey = 'posts_' . $category_id . '_' . $count;
|
||||
|
||||
return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($category_id, $count) {
|
||||
return PostThumbnailResource::collection(
|
||||
Post::query()
|
||||
->where('status', PostStatus::PUBLISHED)
|
||||
->when($category_id, function ($query, $category_id) {
|
||||
$query->where('category_id', $category_id);
|
||||
})
|
||||
->with('category')
|
||||
->orderBy('publish_at', 'desc')
|
||||
->take($count)
|
||||
->get()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\Widget\Tasks;
|
||||
|
||||
use App\Containers\Education\Models\EducationalProgram;
|
||||
use App\Containers\Search\UI\API\Transformers\EducationalProgramSearchResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use App\Ship\Enums\Education\EducationalProgramStatus;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetPublishedEducationalProgramsTask
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::EDUCATION_PROGRAMS_PREFIX->value . 'search_list',
|
||||
now()->addDay(), // Кешируем на 1 день
|
||||
function () {
|
||||
return EducationalProgramSearchResource::collection(
|
||||
EducationalProgram::query()
|
||||
->where('status', EducationalProgramStatus::PUBLISHED)
|
||||
->orderBy('name', 'desc')
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+6
-17
@@ -2,28 +2,17 @@
|
||||
|
||||
namespace App\Containers\Widget\UI\API\Controllers;
|
||||
|
||||
use App\Containers\AdditionalEducation\Models\AdditionalEducation;
|
||||
use App\Containers\Search\UI\API\Transformers\AdditionalEducationSearchResource;
|
||||
use App\Containers\Widget\Actions\GetAdditionalEducationalProgramsAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ClientWidgetAdditionalEducationalProgramController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetAdditionalEducationalProgramsAction $getAdditionalEducationalProgramsAction
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::ADDITIONAL_EDUCATIONAL_PROGRAMS_PREFIX->value . 'search_list',
|
||||
now()->addDay(), // Кешируем на 1 день
|
||||
function () {
|
||||
return AdditionalEducationSearchResource::collection(
|
||||
AdditionalEducation::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('title', 'desc')
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
return $this->getAdditionalEducationalProgramsAction->run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
-17
@@ -2,28 +2,17 @@
|
||||
|
||||
namespace App\Containers\Widget\UI\API\Controllers;
|
||||
|
||||
use App\Containers\Education\Models\EducationalProgram;
|
||||
use App\Containers\Search\UI\API\Transformers\EducationalProgramSearchResource;
|
||||
use App\Containers\Widget\Actions\GetEducationalProgramsAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use App\Ship\Enums\Education\EducationalProgramStatus;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ClientWidgetEducationalProgramController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetEducationalProgramsAction $getEducationalProgramsAction
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
{
|
||||
return Cache::remember(
|
||||
CacheKeys::EDUCATION_PROGRAMS_PREFIX->value . 'search_list',
|
||||
now()->addDay(), // Кешируем на 1 день
|
||||
function () {
|
||||
return EducationalProgramSearchResource::collection(
|
||||
EducationalProgram::query()
|
||||
->where('status', EducationalProgramStatus::PUBLISHED)
|
||||
->orderBy('name', 'desc')
|
||||
->get()
|
||||
);
|
||||
}
|
||||
);
|
||||
return $this->getEducationalProgramsAction->run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,38 +2,19 @@
|
||||
|
||||
namespace App\Containers\Widget\UI\API\Controllers;
|
||||
|
||||
use App\Containers\AppStructure\Models\Page;
|
||||
use App\Containers\Widget\UI\API\Transformers\PageNavigateResource;
|
||||
use App\Containers\Widget\Actions\GetPageAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ClientWidgetPageController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetPageAction $getPageAction
|
||||
) {}
|
||||
|
||||
public function single(int $id): \Illuminate\Http\JsonResponse
|
||||
{
|
||||
$cacheKey = 'page_' . md5($id);
|
||||
$data = $this->getPageAction->run($id);
|
||||
|
||||
// Пытаемся получить данные из кеша
|
||||
$page = Cache::remember($cacheKey, now()->addHours(1), function () use ($id) {
|
||||
return Page::where('id', '=', $id)
|
||||
->with('section.pages.section', 'section.mainSection')
|
||||
->firstOrFail();
|
||||
});
|
||||
|
||||
if (isset($page->section)) {
|
||||
$breadcrumbs = [
|
||||
'mainSection' => $page->section->mainSection->title,
|
||||
'subSection' => $page->section->title,
|
||||
'page' => $page->title,
|
||||
];
|
||||
} else {
|
||||
$breadcrumbs = null;
|
||||
}
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'page' => new PageNavigateResource($page),
|
||||
'breadcrumbs' => $breadcrumbs
|
||||
],
|
||||
], 200);
|
||||
return response()->json(['data' => $data], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,44 +2,28 @@
|
||||
|
||||
namespace App\Containers\Widget\UI\API\Controllers;
|
||||
|
||||
use App\Containers\Article\Enums\PostStatus;
|
||||
use App\Containers\Article\Models\Post;
|
||||
use App\Containers\Widget\UI\API\Transformers\PostThumbnailResource;
|
||||
use App\Containers\Widget\Actions\GetPostsAction;
|
||||
use App\Containers\Widget\Actions\GetSinglePostAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ClientWidgetPostController extends Controller
|
||||
{
|
||||
public function index()
|
||||
public function __construct(
|
||||
private readonly GetPostsAction $getPostsAction,
|
||||
private readonly GetSinglePostAction $getSinglePostAction
|
||||
) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$category_id = request()->input('category');
|
||||
$count = request()->input('count', 5);
|
||||
$category_id = $request->input('category');
|
||||
$count = $request->input('count', 5);
|
||||
|
||||
$cacheKey = 'posts_' . $category_id . '_' . $count;
|
||||
|
||||
return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($category_id, $count) {
|
||||
return PostThumbnailResource::collection(
|
||||
Post::query()
|
||||
->where('status', PostStatus::PUBLISHED)
|
||||
->when($category_id, function ($query, $category_id) {
|
||||
$query->where('category_id', $category_id);
|
||||
})
|
||||
->with('category')
|
||||
->orderBy('publish_at', 'desc')
|
||||
->take($count)
|
||||
->get()
|
||||
);
|
||||
});
|
||||
return $this->getPostsAction->run($category_id, $count);
|
||||
}
|
||||
|
||||
public function single(int $id)
|
||||
{
|
||||
$cacheKey = 'post_' . $id;
|
||||
|
||||
return Cache::remember($cacheKey, now()->addMinutes(10), function () use ($id) {
|
||||
return new PostThumbnailResource(
|
||||
Post::query()->with('category')->find($id)->firstOrFail()
|
||||
);
|
||||
});
|
||||
return $this->getSinglePostAction->run($id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,26 +2,17 @@
|
||||
|
||||
namespace App\Containers\Widget\UI\API\Controllers;
|
||||
|
||||
use App\Containers\Widget\Models\Slider;
|
||||
use App\Containers\Widget\Actions\GetSliderBySlugAction;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ClientWidgetSliderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GetSliderBySlugAction $getSliderBySlugAction
|
||||
) {}
|
||||
|
||||
public function show(string $slug): ?object
|
||||
{
|
||||
$cacheKey = 'slider_' . $slug;
|
||||
|
||||
return Cache::remember($cacheKey, now()->addHour(), function () use ($slug) {
|
||||
$slider = Slider::query()
|
||||
->where('slug', $slug)
|
||||
->where('is_active', true)
|
||||
->with(['slides' => function($query) {
|
||||
$query->where('is_active', true);
|
||||
}])
|
||||
->firstOrFail();
|
||||
|
||||
return $slider ?: null;
|
||||
});
|
||||
return $this->getSliderBySlugAction->run($slug);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
<template>
|
||||
|
||||
|
||||
<div v-if="loading" class="flex flex-col space-y-4">
|
||||
<div class="flex animate-pulse">
|
||||
<div class="ms-4 mt-2 w-full border px-4 py-4 rounded-xl shadow-sm">
|
||||
@@ -35,7 +33,7 @@
|
||||
</li>
|
||||
<li class="inline-flex items-center">
|
||||
<span class="flex items-center text-sm text-gray-500 hover:text-primary-hover focus:outline-none focus:text-primary" href="#">
|
||||
{{ breadcrumbs.mainSection }}
|
||||
{{ breadcrumbs.subSection }}
|
||||
</span>
|
||||
<svg class="shrink-0 mx-2 size-4 text-gray-400" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="m9 18 6-6-6-6"></path>
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
{{ post.data.title }}
|
||||
</h3>
|
||||
<p class="mt-3 text-gray-600">
|
||||
Produce professional, reliable streams easily leveraging Preline's innovative broadcast studio
|
||||
{{ textLimit(post.data.preview_text, 80) }}
|
||||
</p>
|
||||
<p class="mt-4 inline-flex items-center gap-x-1 text-sm text-primary decoration-2 group-hover:underline group-focus:underline font-medium">
|
||||
Читать далее
|
||||
@@ -55,7 +55,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import slugify from "slugify";
|
||||
import { helpers } from "@/mixins/Helpers";
|
||||
import axios from "axios";
|
||||
import {Link} from "@inertiajs/vue3";
|
||||
|
||||
@@ -84,7 +84,15 @@ export default {
|
||||
console.error('Ошибка:', error);
|
||||
this.loading = false; // Установить состояние загрузки в false даже при ошибке
|
||||
});
|
||||
}
|
||||
},
|
||||
textLimit(text, symbols) {
|
||||
if (text.length > symbols) {
|
||||
let LimitedText;
|
||||
LimitedText = text.substring(0, symbols);
|
||||
return LimitedText + "...";
|
||||
}
|
||||
return text;
|
||||
},
|
||||
},
|
||||
mounted() {
|
||||
this.getPost(this.block.data.post);
|
||||
|
||||
Reference in New Issue
Block a user