From 98028e4422fe6f2c1af297271d0c5bcd01af70d4 Mon Sep 17 00:00:00 2001 From: Paul Tirk Date: Mon, 11 Apr 2022 10:11:33 +0200 Subject: add routes for starring/unstarring items by id Signed-off-by: Paul Tirk --- appinfo/routes.php | 5 ++++ lib/Controller/ApiController.php | 2 +- lib/Controller/ItemApiController.php | 51 ++++++++++++++++++++++++++++++++++++ lib/Service/ItemServiceV2.php | 26 ++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 09348bc7b..739d8deaf 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -66,6 +66,11 @@ return ['routes' => [ ['name' => 'folder_api_v2#update', 'url' => '/api/v2/folders/{folderId}', 'verb' => 'PATCH'], ['name' => 'folder_api_v2#delete', 'url' => '/api/v2/folders/{folderId}', 'verb' => 'DELETE'], +// API 1.3 + +['name' => 'item_api#star_by_item_id', 'url' => '/api/v1-3/items/{itemId}/star', 'verb' => 'POST'], +['name' => 'item_api#unstar_by_item_id', 'url' => '/api/v1-3/items/{itemId}/unstar', 'verb' => 'POST'], + // API 1.2 ['name' => 'utility_api#version', 'url' => '/api/v1-2/version', 'verb' => 'GET'], ['name' => 'utility_api#status', 'url' => '/api/v1-2/status', 'verb' => 'GET'], diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 35924c261..c965dead5 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -82,7 +82,7 @@ class ApiController extends BaseApiController public function index(): array { return [ - 'apiLevels' => ['v1-2', 'v2'] + 'apiLevels' => ['v1-2', 'v1-3', 'v2'] ]; } } diff --git a/lib/Controller/ItemApiController.php b/lib/Controller/ItemApiController.php index 2e59d5b3a..dec04bbe6 100644 --- a/lib/Controller/ItemApiController.php +++ b/lib/Controller/ItemApiController.php @@ -221,6 +221,25 @@ class ItemApiController extends ApiController } + /** + * @param int $itemId + * @param bool $isStarred + * + * @return array|JSONResponse + * @throws ServiceConflictException + */ + private function setStarredByItemId(int $itemId, bool $isStarred) + { + try { + $this->itemService->starByItemId($this->getUserId(), $itemId, $isStarred); + } catch (ServiceNotFoundException $ex) { + return $this->error($ex, Http::STATUS_NOT_FOUND); + } + + return []; + } + + /** * @NoAdminRequired * @NoCSRFRequired @@ -255,6 +274,38 @@ class ItemApiController extends ApiController } + /** + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * + * @param int $itemId + * + * @return array|JSONResponse + * @throws ServiceConflictException + */ + public function starByItemId(int $itemId) + { + return $this->setStarredByItemId($itemId, true); + } + + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @CORS + * + * @param int $itemId + * + * @return array|JSONResponse + * @throws ServiceConflictException + */ + public function unstarByItemId(int $itemId) + { + return $this->setStarredByItemId($itemId, false); + } + + /** * @NoAdminRequired * diff --git a/lib/Service/ItemServiceV2.php b/lib/Service/ItemServiceV2.php index 57804a8c3..4f314105b 100644 --- a/lib/Service/ItemServiceV2.php +++ b/lib/Service/ItemServiceV2.php @@ -211,6 +211,32 @@ class ItemServiceV2 extends Service return $this->mapper->update($item); } + /** + * Mark an item as starred by id + * + * @param string $userId Item owner + * @param int $itemId + * @param bool $starred + * + * @return Item + * @throws ServiceConflictException + * @throws ServiceNotFoundException + */ + public function starByItemId(string $userId, int $itemId, bool $starred): Entity + { + try { + $item = $this->mapper->findFromUser($userId, $itemId); + } catch (DoesNotExistException $ex) { + throw ServiceNotFoundException::from($ex); + } catch (MultipleObjectsReturnedException $ex) { + throw ServiceConflictException::from($ex); + } + + $item->setStarred($starred); + + return $this->mapper->update($item); + } + /** * Mark all items as read * -- cgit v1.2.3