72 lines
1.9 KiB
PHP
72 lines
1.9 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'],
|
|
permissions: ['user.settings.read']
|
|
)]
|
|
public function read(): JsonResponse
|
|
{
|
|
// Fetch all settings (no filter)
|
|
$settings = $this->userService->fetchSettings();
|
|
|
|
return new JsonResponse($settings, JsonResponse::HTTP_OK);
|
|
}
|
|
|
|
/**
|
|
* Update user settings
|
|
*
|
|
* @param array $data Key-value pairs of settings to update
|
|
*
|
|
* @example request body:
|
|
* {
|
|
* "data": {
|
|
* "theme": "dark",
|
|
* "language": "en",
|
|
* "notifications": true
|
|
* }
|
|
* }
|
|
*
|
|
* @return JsonResponse Updated settings data
|
|
*/
|
|
#[AuthenticatedRoute(
|
|
'/user/settings',
|
|
name: 'user.settings.update',
|
|
methods: ['PUT', 'PATCH'],
|
|
permissions: ['user.settings.update']
|
|
)]
|
|
public function update(array $data): JsonResponse
|
|
{
|
|
$this->userService->storeSettings($data);
|
|
|
|
// Return updated settings
|
|
$updatedSettings = $this->userService->fetchSettings(array_keys($data));
|
|
|
|
return new JsonResponse($updatedSettings, JsonResponse::HTTP_OK);
|
|
}
|
|
}
|