refactor multiple widget controllers; replace caching logic with action classes for data retrieval, enhancing code clarity and maintainability
This commit is contained in:
+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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user