refactoring the InstituteStructure module
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Actions;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Division;
|
||||
use App\Containers\InstituteStructure\Tasks\GetDivisionBySlugTask;
|
||||
|
||||
class FindDivisionBySlugAction
|
||||
{
|
||||
public function __construct(private readonly GetDivisionBySlugTask $getDivisionBySlugTask)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(string $slug): Division
|
||||
{
|
||||
return $this->getDivisionBySlugTask->run($slug);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Actions;
|
||||
|
||||
use App\Containers\InstituteStructure\Tasks\GetActiveDivisionsTask;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
|
||||
class ListDivisionsAction
|
||||
{
|
||||
public function __construct(private readonly GetActiveDivisionsTask $getActiveDivisionsTask)
|
||||
{
|
||||
}
|
||||
|
||||
public function run(): AnonymousResourceCollection
|
||||
{
|
||||
return $this->getActiveDivisionsTask->run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Division;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\DivisionResource;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class GetActiveDivisionsTask
|
||||
{
|
||||
public function run(): AnonymousResourceCollection
|
||||
{
|
||||
return Cache::remember(CacheKeys::DIVISIONS_PREFIX->value . 'list', now()->addDay(), function () {
|
||||
return DivisionResource::collection(
|
||||
Division::query()->where('is_active', true)->get()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Containers\InstituteStructure\Tasks;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Division;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class GetDivisionBySlugTask
|
||||
{
|
||||
public function run(string $slug): Division
|
||||
{
|
||||
return Cache::remember(CacheKeys::DIVISION_PREFIX->value . $slug, now()->addDay(), function () use ($slug) {
|
||||
return Division::query()
|
||||
->with([
|
||||
'workers' => fn ($query) => $query->orderBy('sort', 'asc'),
|
||||
'workers.userDetail',
|
||||
'seo'
|
||||
])
|
||||
->where('is_active', true)
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,52 +2,36 @@
|
||||
|
||||
namespace App\Containers\InstituteStructure\UI\WEB\Controllers;
|
||||
|
||||
use App\Containers\InstituteStructure\Models\Division;
|
||||
use App\Containers\InstituteStructure\Actions\FindDivisionBySlugAction;
|
||||
use App\Containers\InstituteStructure\Actions\ListDivisionsAction;
|
||||
use App\Containers\InstituteStructure\UI\WEB\Transformers\DivisionResource;
|
||||
use App\Ship\Contracts\SeoServiceInterface;
|
||||
use App\Ship\Controllers\Controller;
|
||||
use App\Ship\Enums\CacheKeys;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class ClientDivisionController extends Controller
|
||||
{
|
||||
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
||||
public function __construct(
|
||||
readonly SeoServiceInterface $seoPageProvider,
|
||||
private readonly ListDivisionsAction $listDivisionsAction,
|
||||
private readonly FindDivisionBySlugAction $findDivisionBySlugAction
|
||||
) {
|
||||
}
|
||||
|
||||
public function index()
|
||||
public function index(): \Inertia\Response
|
||||
{
|
||||
$divisions = Cache::remember(CacheKeys::DIVISIONS_PREFIX->value . 'list', now()->addDay(), function () {
|
||||
return DivisionResource::collection(
|
||||
Division::query()->where('is_active', true)->get()
|
||||
);
|
||||
});
|
||||
$divisions = $this->listDivisionsAction->run();
|
||||
|
||||
$seo = Cache::remember(CacheKeys::DIVISIONS_PREFIX->value . 'seo', now()->addDay(), function () {
|
||||
return $this->seoPageProvider->getSeoForCurrentPage();
|
||||
});
|
||||
$seo = $this->seoPageProvider->getSeoForCurrentPage();
|
||||
|
||||
return Inertia::render('Client/Divisions/Index', compact('divisions', 'seo'));
|
||||
}
|
||||
|
||||
public function show(string $slug): \Inertia\Response
|
||||
{
|
||||
$divisions = Cache::remember(CacheKeys::DIVISIONS_PREFIX->value . 'list', now()->addDay(), function () {
|
||||
return DivisionResource::collection(
|
||||
Division::query()->where('is_active', true)->get()
|
||||
);
|
||||
});
|
||||
$divisions = $this->listDivisionsAction->run();
|
||||
|
||||
$divisionData = Cache::remember(CacheKeys::DIVISION_PREFIX->value . $slug, now()->addDay(), function () use ($slug) {
|
||||
return Division::query()
|
||||
->with([
|
||||
'workers' => fn ($query) => $query->orderBy('sort', 'asc'),
|
||||
'workers.userDetail',
|
||||
'seo'
|
||||
])
|
||||
->where('is_active', true)
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
});
|
||||
$divisionData = $this->findDivisionBySlugAction->run($slug);
|
||||
|
||||
$seo = $this->seoPageProvider->getSeoForModel($divisionData);
|
||||
|
||||
@@ -55,5 +39,4 @@ class ClientDivisionController extends Controller
|
||||
|
||||
return Inertia::render('Client/Divisions/Show', compact('divisions', 'division', 'seo'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user