2018-06-01 21:41:56 +00:00
|
|
|
<?php
|
2020-09-21 12:25:50 +00:00
|
|
|
/*
|
2024-02-02 18:27:50 +00:00
|
|
|
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
|
2018-06-01 21:41:56 +00:00
|
|
|
*
|
2020-09-21 12:25:50 +00:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
|
2018-06-01 21:41:56 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-23 17:29:19 +00:00
|
|
|
namespace OCA\Bookmarks\Controller;
|
2018-06-01 21:41:56 +00:00
|
|
|
|
2024-01-28 11:25:40 +00:00
|
|
|
use OCA\Bookmarks\Service\UserSettingsService;
|
2019-12-28 16:41:31 +00:00
|
|
|
use OCP\AppFramework\ApiController;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IRequest;
|
2018-06-01 21:41:56 +00:00
|
|
|
|
|
|
|
class SettingsController extends ApiController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2024-01-28 11:25:40 +00:00
|
|
|
* @param UserSettingsService $userSettingsService
|
2018-06-01 21:41:56 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2024-01-28 11:25:40 +00:00
|
|
|
$appName, $request,
|
|
|
|
private UserSettingsService $userSettingsService,
|
2018-06-01 21:41:56 +00:00
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:44:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return JSONResponse
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2024-01-28 11:25:40 +00:00
|
|
|
public function getSetting(string $key): JSONResponse {
|
2018-06-01 21:41:56 +00:00
|
|
|
try {
|
2024-01-28 11:25:40 +00:00
|
|
|
$value = $this->userSettingsService->get($key);
|
|
|
|
} catch (\UnexpectedValueException) {
|
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
|
2018-06-01 21:41:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 11:25:40 +00:00
|
|
|
return new JSONResponse([$key => $value], Http::STATUS_OK);
|
2020-08-21 15:18:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 15:44:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @return JSONResponse
|
|
|
|
* @NoAdminRequired
|
|
|
|
*/
|
2024-01-28 11:25:40 +00:00
|
|
|
public function setSetting(string $key, string $value): JSONResponse {
|
2020-08-21 15:18:30 +00:00
|
|
|
try {
|
2024-01-28 11:25:40 +00:00
|
|
|
$this->userSettingsService->set($key, $value);
|
|
|
|
} catch (\ValueError) {
|
2018-06-01 23:28:58 +00:00
|
|
|
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
|
2024-01-28 11:25:40 +00:00
|
|
|
} catch (\UnexpectedValueException) {
|
2018-12-30 10:19:58 +00:00
|
|
|
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
2022-04-12 11:27:47 +00:00
|
|
|
|
2024-01-28 11:25:40 +00:00
|
|
|
return new JSONResponse(['status' => 'success'], Http::STATUS_OK);
|
2023-08-17 11:14:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 21:41:56 +00:00
|
|
|
}
|