60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace KTXC\Controllers;
|
|
|
|
use KTXC\Http\Response\JsonResponse;
|
|
use KTXC\Service\UserService;
|
|
use KTXC\SessionIdentity;
|
|
use KTXC\SessionTenant;
|
|
use KTXF\Controller\ControllerAbstract;
|
|
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
|
|
class UserSettingsController extends ControllerAbstract
|
|
{
|
|
public function __construct(
|
|
private readonly SessionTenant $tenantIdentity,
|
|
private readonly SessionIdentity $userIdentity,
|
|
private readonly UserService $userService
|
|
) {}
|
|
|
|
/**
|
|
* Retrieve user settings
|
|
* If no specific settings are requested, all settings are returned
|
|
*
|
|
* @return JsonResponse Settings data as key-value pairs
|
|
*/
|
|
#[AuthenticatedRoute('/user/settings', name: 'user.settings.read', methods: ['GET'])]
|
|
public function read(): JsonResponse
|
|
{
|
|
// Fetch all settings
|
|
$settings = $this->userService->fetchSettings([]);
|
|
|
|
return new JsonResponse($settings, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Update user settings
|
|
*
|
|
* @param array $settings Key-value pairs of settings to update
|
|
*
|
|
* @example request body:
|
|
* {
|
|
* "theme": "dark",
|
|
* "language": "en",
|
|
* "notifications": true
|
|
* }
|
|
*
|
|
* @return JsonResponse Updated settings data
|
|
*/
|
|
#[AuthenticatedRoute('/user/settings', name: 'user.settings.update', methods: ['PUT', 'PATCH'])]
|
|
public function update(array $settings = []): JsonResponse
|
|
{
|
|
$this->userService->storeSettings($settings);
|
|
|
|
// Return updated settings
|
|
$updatedSettings = $this->userService->fetchSettings(array_keys($settings));
|
|
|
|
return new JsonResponse($updatedSettings, JsonResponse::HTTP_OK);
|
|
}
|
|
}
|