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