Skip to content

Commit

Permalink
feat(Contexts): API endpoint to delete a context
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Mar 13, 2024
1 parent b9e9e42 commit 5c6489c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
['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#destroy', 'url' => '/api/2/contexts/{contextId}', 'verb' => 'DELETE'],
['name' => 'Context#transfer', 'url' => '/api/2/contexts/{contextId}/transfer', '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'],
Expand Down
23 changes: 23 additions & 0 deletions lib/Controller/ContextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ public function update(int $contextId, ?string $name, ?string $iconName, ?string
}
}

/**
* [api v2] Delete an existing context and return it
*
* @param int $contextId ID of the context
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
*
* 200: returning the full context information
* 403: No permissions
* 404: Not found
*
* @NoAdminRequired
* @CanManageContext
*/
public function destroy(int $contextId): DataResponse {
try {
return new DataResponse($this->contextService->delete($contextId, $this->userId)->jsonSerialize());
} catch (Exception $e) {
return $this->handleError($e);
} catch (NotFoundError $e) {
return $this->handleNotFoundError($e);
}
}

/**
* [api v2] Transfer the ownership of a context and return it
*
Expand Down
9 changes: 9 additions & 0 deletions lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ public function update(int $contextId, string $userId, ?string $name, ?string $i
return $context;
}

/**
* @throws NotFoundError
* @throws Exception
*/
public function delete(int $contextId, string $userId): Context {
$context = $this->contextMapper->findById($contextId, $userId);
return $this->contextMapper->delete($context);
}

/**
* @throws MultipleObjectsReturnedException
* @throws DoesNotExistException
Expand Down

0 comments on commit 5c6489c

Please sign in to comment.