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 { $userId = (int) (auth()->id() ?? 0); if ($userId <= 0) { return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED); } $payload = $this->queryService->getForUser($userId); 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 { $userId = (int) (auth()->id() ?? 0); if ($userId <= 0) { return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED); } try { $saved = $this->commandService->upsert($userId, $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($userId); return $this->success([ 'preferences' => new PreferencesResource($payload['preferences']), ], 'Preferences saved.', $saved->wasRecentlyCreated ? Response::HTTP_CREATED : Response::HTTP_OK); } 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.', $saved->wasRecentlyCreated ? Response::HTTP_CREATED : Response::HTTP_OK); } public function destroy(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('delete', $row); 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.'); } }