update controllers logic
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Settings;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Settings\SettingsUpdateRequest;
|
||||
use App\Http\Resources\Settings\SettingsResource;
|
||||
use App\Models\Setting;
|
||||
@@ -32,13 +32,13 @@ class SettingsController extends BaseApiController
|
||||
$settings = Setting::singleton();
|
||||
$this->authorize('update', $settings);
|
||||
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
try {
|
||||
$updated = $this->settingsService->update($request->validated(), $userId);
|
||||
$updated = $this->settingsService->update($request->validated(), $guard);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Settings update failed: ' . $e->getMessage());
|
||||
return $this->error('Unable to update settings.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
@@ -48,4 +48,17 @@ class SettingsController extends BaseApiController
|
||||
'settings' => new SettingsResource($updated),
|
||||
], 'Settings updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|JsonResponse
|
||||
*/
|
||||
private function authenticatedUserIdOrUnauthorized(): int|JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return $userId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user