add all controllers logic

This commit is contained in:
root
2026-03-11 17:53:15 -04:00
parent 3e6c577085
commit 2ef71cc92b
421 changed files with 12009 additions and 5211 deletions
@@ -0,0 +1,43 @@
<?php
namespace App\Http\Controllers\Api\Settings;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Settings\PolicyShowRequest;
use App\Http\Resources\Settings\PolicyResource;
use App\Services\Policy\PolicyContentService;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class PolicyController extends BaseApiController
{
public function __construct(private PolicyContentService $service)
{
parent::__construct();
}
public function school(PolicyShowRequest $request): JsonResponse
{
return $this->respondPolicy('school');
}
public function picture(PolicyShowRequest $request): JsonResponse
{
return $this->respondPolicy('picture');
}
private function respondPolicy(string $type): JsonResponse
{
try {
$policy = $this->service->getPolicy($type);
} catch (\InvalidArgumentException $e) {
return $this->error('Policy not found.', Response::HTTP_NOT_FOUND);
} catch (\Throwable $e) {
return $this->error('Unable to load policy.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success([
'policy' => new PolicyResource($policy),
]);
}
}
@@ -0,0 +1,139 @@
<?php
namespace App\Http\Controllers\Api\Settings;
use App\Http\Controllers\Api\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
{
$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.');
}
}
@@ -0,0 +1,51 @@
<?php
namespace App\Http\Controllers\Api\Settings;
use App\Http\Controllers\Api\BaseApiController;
use App\Http\Requests\Settings\SettingsUpdateRequest;
use App\Http\Resources\Settings\SettingsResource;
use App\Models\Setting;
use App\Services\Settings\SettingsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
class SettingsController extends BaseApiController
{
public function __construct(private SettingsService $settingsService)
{
parent::__construct();
}
public function index(): JsonResponse
{
$this->authorize('viewAny', Setting::class);
return $this->success([
'settings' => new SettingsResource($this->settingsService->get()),
]);
}
public function update(SettingsUpdateRequest $request): JsonResponse
{
$settings = Setting::singleton();
$this->authorize('update', $settings);
$userId = (int) (auth()->id() ?? 0);
if ($userId <= 0) {
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
}
try {
$updated = $this->settingsService->update($request->validated(), $userId);
} catch (\Throwable $e) {
Log::error('Settings update failed: ' . $e->getMessage());
return $this->error('Unable to update settings.', Response::HTTP_INTERNAL_SERVER_ERROR);
}
return $this->success([
'settings' => new SettingsResource($updated),
], 'Settings updated.');
}
}