From ba9026349ac13e98edab9dcef837ba5cb6536c43 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Thu, 29 Feb 2024 16:07:43 +0100 Subject: [PATCH] feat(Contexts): API endpoint to update basic information of a Context Signed-off-by: Arthur Schiwon --- appinfo/routes.php | 1 + lib/Controller/ContextController.php | 14 ++++++++++++++ lib/Service/ContextService.php | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/appinfo/routes.php b/appinfo/routes.php index 77e0bd992..1a5e1798e 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -131,6 +131,7 @@ ['name' => 'Context#index', 'url' => '/api/2/contexts', 'verb' => 'GET'], ['name' => 'Context#show', 'url' => '/api/2/contexts/{contextId}', 'verb' => 'GET'], ['name' => 'Context#create', 'url' => '/api/2/contexts', 'verb' => 'POST'], + ['name' => 'Context#update', 'url' => '/api/2/contexts/{contextId}', 'verb' => 'PUT'], ['name' => 'Context#addNode', 'url' => '/api/2/contexts/{contextId}/nodes', 'verb' => 'POST'], ['name' => 'Context#removeNode', 'url' => '/api/2/contexts/{contextId}/nodes/{nodeRelId}', 'verb' => 'DELETE'], diff --git a/lib/Controller/ContextController.php b/lib/Controller/ContextController.php index b380c3474..08e00d32e 100644 --- a/lib/Controller/ContextController.php +++ b/lib/Controller/ContextController.php @@ -99,6 +99,20 @@ public function create(string $name, string $iconName, string $description = '', } } + /** + * @NoAdminRequired + * @CanManageContext + */ + public function update(int $contextId, ?string $name, ?string $iconName, ?string $description): DataResponse { + try { + return new DataResponse($this->contextService->update($contextId, $name, $iconName, $description)->jsonSerialize()); + } catch (Exception|MultipleObjectsReturnedException $e) { + return $this->handleError($e); + } catch (DoesNotExistException $e) { + return $this->handleNotFoundError(new NotFoundError($e->getMessage(), $e->getCode(), $e)); + } + } + /** * @NoAdminRequired * @CanManageNode diff --git a/lib/Service/ContextService.php b/lib/Service/ContextService.php index 4f4d091f9..7d09e0f3f 100644 --- a/lib/Service/ContextService.php +++ b/lib/Service/ContextService.php @@ -104,6 +104,27 @@ public function create(string $name, string $iconName, string $description, arra return $context; } + /** + * @throws Exception + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + */ + public function update(int $contextId, ?string $name, ?string $iconName, ?string $description): Context { + $context = $this->contextMapper->findById($contextId); + + if ($name !== null) { + $context->setName(trim($name)); + } + if ($iconName !== null) { + $context->setIcon(trim($iconName)); + } + if ($description !== null) { + $context->setDescription(trim($description)); + } + + return $this->contextMapper->update($context); + } + /** * @throws MultipleObjectsReturnedException * @throws DoesNotExistException