Files
alrahma_sunday_school_api/app/Http/Controllers/Api/Settings/PreferencesController.php
T
root e13df69885
API CI/CD / Validate (composer + pint) (push) Successful in 3m6s
API CI/CD / Test (PHPUnit) (push) Failing after 4m53s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 59s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix unittests issues
2026-07-07 20:56:32 -04:00

153 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Settings;
use App\Http\Controllers\Api\Core\BaseApiController;
use App\Http\Requests\Preferences\PreferencesIndexRequest;
use App\Http\Requests\Preferences\PreferencesUpsertRequest;
use App\Http\Resources\Preferences\PreferencesCollection;
use App\Http\Resources\Preferences\PreferencesResource;
use App\Models\Preferences;
use App\Services\Preferences\PreferencesCommandService;
use App\Services\Preferences\PreferencesQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
class PreferencesController extends BaseApiController
{
public function __construct(
private PreferencesQueryService $queryService,
private PreferencesCommandService $commandService
) {
parent::__construct();
}
public function index(PreferencesIndexRequest $request): JsonResponse
{
$this->authorize('viewAny', Preferences::class);
$filters = $request->validated();
$page = (int) ($filters['page'] ?? 1);
$perPage = (int) ($filters['per_page'] ?? 20);
$rows = $this->queryService->paginate($filters, $page, $perPage);
$collection = new PreferencesCollection($rows);
return $this->success([
'preferences' => $collection->toArray($request),
'meta' => $collection->with($request)['meta'],
]);
}
public function show(): JsonResponse
{
$guard = $this->authenticatedUserIdOrUnauthorized();
if ($guard instanceof JsonResponse) {
return $guard;
}
$payload = $this->queryService->getForUser($guard);
return $this->success([
'preferences' => new PreferencesResource($payload['preferences']),
'options' => $payload['options'],
]);
}
public function showForUser(int $userId): JsonResponse
{
$row = Preferences::query()->where('user_id', $userId)->first();
if (! $row) {
return $this->error('Preferences not found.', Response::HTTP_NOT_FOUND);
}
$this->authorize('view', $row);
$payload = $this->queryService->getForUser($userId);
return $this->success([
'preferences' => new PreferencesResource($payload['preferences']),
'options' => $payload['options'],
]);
}
public function store(PreferencesUpsertRequest $request): JsonResponse
{
$guard = $this->authenticatedUserIdOrUnauthorized();
if ($guard instanceof JsonResponse) {
return $guard;
}
try {
$saved = $this->commandService->upsert($guard, $request->validated());
} catch (\Throwable $e) {
Log::error('Preferences save failed: '.$e->getMessage());
return $this->error('Unable to save preferences.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
$payload = $this->queryService->getForUser($guard);
return $this->success([
'preferences' => new PreferencesResource($payload['preferences']),
], 'Preferences saved.');
}
public function updateForUser(PreferencesUpsertRequest $request, int $userId): JsonResponse
{
$row = Preferences::query()->where('user_id', $userId)->first();
if ($row) {
$this->authorize('update', $row);
}
try {
$saved = $this->commandService->upsert($userId, $request->validated());
} catch (\Throwable $e) {
Log::error('Preferences update failed: '.$e->getMessage());
return $this->error('Unable to save preferences.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
$payload = $this->queryService->getForUser($userId);
return $this->success([
'preferences' => new PreferencesResource($payload['preferences']),
], 'Preferences saved.');
}
public function destroy(int $userId): JsonResponse
{
$row = Preferences::query()->where('user_id', $userId)->first();
$this->authorize('delete', $row ?? new Preferences(['user_id' => $userId]));
if (! $row) {
return $this->error('Preferences not found.', Response::HTTP_NOT_FOUND);
}
try {
$deleted = $this->commandService->delete($row);
} catch (\Throwable $e) {
Log::error('Preferences delete failed: '.$e->getMessage());
return $this->error('Unable to delete preferences.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
if (! $deleted) {
return $this->error('Unable to delete preferences.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success(null, 'Preferences deleted.');
}
private function authenticatedUserIdOrUnauthorized(): int|JsonResponse
{
$userId = (int) (auth()->id() ?? 0);
if ($userId <= 0) {
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
}
return $userId;
}
}