refactor PersonController; replace caching logic with GetPersonDataAction for improved maintainability and clarity
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Containers\User\Actions;
|
||||||
|
|
||||||
|
use App\Containers\User\Tasks\FindPersonBySlugTask;
|
||||||
|
use App\Containers\User\Tasks\GetSeoForPersonTask;
|
||||||
|
|
||||||
|
class GetPersonDataAction
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly FindPersonBySlugTask $findPersonBySlugTask,
|
||||||
|
private readonly GetSeoForPersonTask $getSeoForPersonTask,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function run(string $slug): array
|
||||||
|
{
|
||||||
|
$personData = $this->findPersonBySlugTask->run($slug);
|
||||||
|
$seo = $this->getSeoForPersonTask->run($personData);
|
||||||
|
|
||||||
|
return compact('personData', 'seo');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Containers\User\Tasks;
|
||||||
|
|
||||||
|
use App\Containers\User\Models\User;
|
||||||
|
use App\Ship\Enums\CacheKeys;
|
||||||
|
use App\Ship\Parents\Tasks\Task;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
class FindPersonBySlugTask
|
||||||
|
{
|
||||||
|
public function run(string $slug): User
|
||||||
|
{
|
||||||
|
return Cache::remember(
|
||||||
|
CacheKeys::USER_PREFIX->value . $slug,
|
||||||
|
now()->addHours(24),
|
||||||
|
fn() => User::with(['userDetail', 'departments_work.faculty', 'departments_teach.faculty', 'divisions', 'faculties'])
|
||||||
|
->where('slug', $slug)
|
||||||
|
->whereHas('userDetail')
|
||||||
|
->firstOrFail()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Containers\User\Tasks;
|
||||||
|
|
||||||
|
use App\Containers\User\Models\User;
|
||||||
|
use App\Ship\Contracts\SeoServiceInterface;
|
||||||
|
use App\Ship\Enums\CacheKeys;
|
||||||
|
use App\Ship\Models\Seo;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
class GetSeoForPersonTask
|
||||||
|
{
|
||||||
|
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
||||||
|
|
||||||
|
public function run(User $personData): array|null
|
||||||
|
{
|
||||||
|
return Cache::remember(
|
||||||
|
CacheKeys::USER_PREFIX->value . 'seo_' . $personData->slug,
|
||||||
|
now()->addHours(24),
|
||||||
|
fn() => $this->seoPageProvider->getSeoForModel($personData)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,38 +2,22 @@
|
|||||||
|
|
||||||
namespace App\Containers\User\UI\WEB\Controllers;
|
namespace App\Containers\User\UI\WEB\Controllers;
|
||||||
|
|
||||||
|
use App\Containers\User\Actions\GetPersonDataAction;
|
||||||
use App\Containers\User\Models\User;
|
use App\Containers\User\Models\User;
|
||||||
use App\Containers\User\UI\WEB\Transformers\FullInfoPersonResource;
|
use App\Containers\User\UI\WEB\Transformers\FullInfoPersonResource;
|
||||||
use App\Ship\Contracts\SeoServiceInterface;
|
|
||||||
use App\Ship\Controllers\Controller;
|
use App\Ship\Controllers\Controller;
|
||||||
use App\Ship\Enums\CacheKeys;
|
|
||||||
use Illuminate\Support\Facades\Cache;
|
|
||||||
use Inertia\Inertia;
|
use Inertia\Inertia;
|
||||||
|
|
||||||
class PersonController extends Controller
|
class PersonController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(readonly SeoServiceInterface $seoPageProvider){}
|
public function __construct(private readonly GetPersonDataAction $getPersonDataAction){}
|
||||||
|
|
||||||
public function show(string $slug)
|
public function show(string $slug)
|
||||||
{
|
{
|
||||||
$personData = Cache::remember(
|
$data = $this->getPersonDataAction->run($slug);
|
||||||
CacheKeys::USER_PREFIX->value . $slug,
|
|
||||||
now()->addHours(24),
|
|
||||||
fn() => User::with(['userDetail', 'departments_work.faculty', 'departments_teach.faculty', 'divisions', 'faculties'])
|
|
||||||
->where('slug', $slug)
|
|
||||||
->whereHas('userDetail')
|
|
||||||
->firstOrFail()
|
|
||||||
);
|
|
||||||
|
|
||||||
$seo = Cache::remember(
|
|
||||||
CacheKeys::USER_PREFIX->value . 'seo_' . $slug,
|
|
||||||
now()->addHours(24),
|
|
||||||
fn() => $this->seoPageProvider->getSeoForModel($personData)
|
|
||||||
);
|
|
||||||
|
|
||||||
$person = new FullInfoPersonResource($personData);
|
|
||||||
|
|
||||||
|
|
||||||
|
$person = new FullInfoPersonResource($data['personData']);
|
||||||
|
$seo = $data['seo'];
|
||||||
|
|
||||||
return Inertia::render('Client/Persons/Show', compact('person', 'seo'));
|
return Inertia::render('Client/Persons/Show', compact('person', 'seo'));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers\Auth;
|
|
||||||
|
|
||||||
use App\Containers\User\Models\User;
|
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use Illuminate\Auth\Events\Registered;
|
|
||||||
use Illuminate\Http\RedirectResponse;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Support\Facades\Auth;
|
|
||||||
use Illuminate\Support\Facades\Hash;
|
|
||||||
use Illuminate\Validation\Rules;
|
|
||||||
use Inertia\Inertia;
|
|
||||||
use Inertia\Response;
|
|
||||||
|
|
||||||
class RegisteredUserController extends Controller
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Display the registration view.
|
|
||||||
*/
|
|
||||||
public function create(): Response
|
|
||||||
{
|
|
||||||
return Inertia::render('Auth/Register');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle an incoming registration request.
|
|
||||||
*
|
|
||||||
* @throws \Illuminate\Validation\ValidationException
|
|
||||||
*/
|
|
||||||
public function store(Request $request): RedirectResponse
|
|
||||||
{
|
|
||||||
$request->validate([
|
|
||||||
'name' => 'required|string|max:255',
|
|
||||||
'email' => 'required|string|email|max:255|unique:'.User::class,
|
|
||||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$user = User::create([
|
|
||||||
'name' => $request->name,
|
|
||||||
'email' => $request->email,
|
|
||||||
'password' => Hash::make($request->password),
|
|
||||||
]);
|
|
||||||
|
|
||||||
event(new Registered($user));
|
|
||||||
|
|
||||||
Auth::login($user);
|
|
||||||
|
|
||||||
return redirect('/');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user