bookmarks/lib/Controller/SettingsController.php

64 lines
1.6 KiB
PHP
Raw Normal View History

2018-06-01 21:41:56 +00:00
<?php
2020-09-21 12:25: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
use OCA\Bookmarks\Service\UserSettingsService;
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
* @param UserSettingsService $userSettingsService
2018-06-01 21:41:56 +00:00
*/
public function __construct(
$appName, $request,
private UserSettingsService $userSettingsService,
2018-06-01 21:41:56 +00:00
) {
parent::__construct($appName, $request);
}
/**
* @param string $key
* @return JSONResponse
* @NoAdminRequired
*/
public function getSetting(string $key): JSONResponse {
2018-06-01 21:41:56 +00:00
try {
$value = $this->userSettingsService->get($key);
} catch (\UnexpectedValueException) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
2018-06-01 21:41:56 +00:00
}
return new JSONResponse([$key => $value], Http::STATUS_OK);
}
/**
* @param string $key
* @param string $value
* @return JSONResponse
* @NoAdminRequired
*/
public function setSetting(string $key, string $value): JSONResponse {
try {
$this->userSettingsService->set($key, $value);
} catch (\ValueError) {
2018-06-01 23:28:58 +00:00
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
} catch (\UnexpectedValueException) {
2018-12-30 10:19:58 +00:00
return new JSONResponse(['status' => 'error'], Http::STATUS_BAD_REQUEST);
}
return new JSONResponse(['status' => 'success'], Http::STATUS_OK);
}
2018-06-01 21:41:56 +00:00
}