Files
alrahma_sunday_school_api/app/Http/Controllers/Api/Settings/SettingsController.php
T
2026-03-11 17:53:15 -04:00

52 lines
1.5 KiB
PHP

<?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.');
}
}