Skip to content

Commit

Permalink
feat(OpenAPI): add information on Contexts endpoints
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 11, 2024
1 parent 765a05f commit f45d956
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7,573 deletions.
92 changes: 83 additions & 9 deletions lib/Controller/ContextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public function index(): DataResponse {
}

/**
* [api v3] Get information about the requests context
* [api v2] Get information about the requests context
*
* @param int $contextId ID of the context
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>

Check failure on line 65 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidReturnType

lib/Controller/ContextController.php:65:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::show is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)

Check failure on line 65 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/Controller/ContextController.php:65:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::show is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)
*
* 200: returning the full context information
Expand All @@ -87,11 +88,11 @@ public function show(int $contextId): DataResponse {
* @param string $name Name of the context
* @param string $iconName Material design icon name of the context
* @param string $description Descriptive text of the context
* @param array $nodes optional nodes to be connected to this context
* @param array{id: int, type: int, permissions: int} $nodes optional nodes to be connected to this context

Check failure on line 91 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidParamDefault

lib/Controller/ContextController.php:91:12: InvalidParamDefault: Default value type array<never, never> for argument 4 of method OCA\Tables\Controller\ContextController::create does not match the given type array{id: int, permissions: int, type: int} (see https://psalm.dev/062)

Check failure on line 91 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidParamDefault

lib/Controller/ContextController.php:91:12: InvalidParamDefault: Default value type array<never, never> for argument 4 of method OCA\Tables\Controller\ContextController::create does not match the given type array{id: int, permissions: int, type: int} (see https://psalm.dev/062)
*
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Tables returned
* 200: returning the full context information
*/
public function create(string $name, string $iconName, string $description = '', array $nodes = []): DataResponse {
try {
Expand All @@ -102,6 +103,20 @@ public function create(string $name, string $iconName, string $description = '',
}

/**
* [api v2] Update an existing context and return it
*
* @param int $contextId ID of the context
* @param ?string $name provide this parameter to set a new name
* @param ?string $iconName provide this parameter to set a new icon
* @param ?string $description provide this parameter to set a new description
* @param ?array{id: int, type: int, permissions: int, order: int} $nodes provide this parameter to set a new list of nodes.
*
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>

Check failure on line 114 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidReturnType

lib/Controller/ContextController.php:114:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::update is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)

Check failure on line 114 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/Controller/ContextController.php:114:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::update is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)
*
* 200: returning the full context information
* 403: No permissions
* 404: Not found
*
* @NoAdminRequired
* @CanManageContext
*/
Expand All @@ -123,6 +138,19 @@ public function update(int $contextId, ?string $name, ?string $iconName, ?string
}

/**
* [api v2] Transfer the ownership of a context and return it
*
* @param int $contextId ID of the context
* @param string $newOwnerId ID of the new owner
* @param int $newOwnerType any Application::OWNER_TYPE_* constant
*
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>

Check failure on line 147 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidReturnType

lib/Controller/ContextController.php:147:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::transfer is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|400|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)

Check failure on line 147 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/Controller/ContextController.php:147:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::transfer is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|400|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)
*
* 200: Ownership transferred
* 400: Invalid request
* 403: No permissions
* 404: Not found
*
* @NoAdminRequired
* @CanManageContext
*
Expand All @@ -142,6 +170,20 @@ public function transfer(int $contextId, string $newOwnerId, int $newOwnerType =
}

/**
* [api v2] Add a node to a Context
*
* @param int $contextId ID of the context
* @param int $nodeId ID of the node
* @param int $nodeType any Application::NODE_TYPE_* constant
* @param int $permissions bitmask of the permissions for context recipients
* @param ?int $order in which order the node should appear within the context
*
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>

Check failure on line 181 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidReturnType

lib/Controller/ContextController.php:181:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::addNode is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)

Check failure on line 181 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/Controller/ContextController.php:181:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::addNode is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|404|500, array<array-key, mixed>, array<never, never>>' (see https://psalm.dev/011)
*
* 200: Node added successfully
* 403: No permissions
* 404: Not found
*
* @NoAdminRequired
* @CanManageNode
*/
Expand All @@ -151,6 +193,8 @@ public function addNode(int $contextId, int $nodeId, int $nodeType, int $permiss
$this->contextService->addNodeRelToPage($rel, $order);
$context = $this->contextService->findById($rel->getContextId(), $this->userId);
return new DataResponse($context->jsonSerialize());
} catch (NotFoundError $e) {
return $this->handleNotFoundError($e);

Check failure on line 197 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

InvalidReturnStatement

lib/Controller/ContextController.php:197:11: InvalidReturnStatement: The inferred type 'OCP\AppFramework\Http\DataResponse<404, array{message: string}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::addNode (see https://psalm.dev/128)

Check failure on line 197 in lib/Controller/ContextController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnStatement

lib/Controller/ContextController.php:197:11: InvalidReturnStatement: The inferred type 'OCP\AppFramework\Http\DataResponse<404, array{message: string}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|500, array{description?: string, iconName?: string, id?: int, message?: string, name?: string, owner?: string, ownerType?: int}, array<never, never>>' for OCA\Tables\Controller\ContextController::addNode (see https://psalm.dev/128)
} catch (DoesNotExistException $e) {
return $this->handleNotFoundError(new NotFoundError($e->getMessage(), $e->getCode(), $e));
} catch (MultipleObjectsReturnedException|Exception|InternalError $e) {
Expand All @@ -159,6 +203,17 @@ public function addNode(int $contextId, int $nodeId, int $nodeType, int $permiss
}

/**
* [api v2] Remove a node from a Context
*
* @param int $contextId ID of the context
* @param int $nodeRelId ID of the node-in-context relation
*
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Node removed successfully
* 403: No permissions
* 404: Not found
*
* @NoAdminRequired
* @CanManageContext
*/
Expand All @@ -174,21 +229,40 @@ public function removeNode(int $contextId, int $nodeRelId): DataResponse {
$this->contextService->removeNodeRelFromAllPages($nodeRelation);
$context = $this->contextService->findById($contextId, $this->userId);
return new DataResponse($context->jsonSerialize());
} catch (NotFoundError $e) {
return $this->handleNotFoundError($e);
} catch (DoesNotExistException $e) {
$this->handleNotFoundError(new NotFoundError($e->getMessage(), $e->getCode(), $e));
} catch (MultipleObjectsReturnedException|Exception $e) {
$this->handleError($e);
return $this->handleNotFoundError(new NotFoundError($e->getMessage(), $e->getCode(), $e));
} catch (MultipleObjectsReturnedException|Exception|InternalError $e) {
return $this->handleError($e);
}

return new DataResponse();
}

/**
* [api v2] Update the order on a page of a context
*
* @param int $contextId ID of the context
* @param int $pageId ID of the page
* @param array{id: int, order: int} $content content items with it and order values
*
* @return DataResponse<Http::STATUS_OK, TablesContext, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* @NoAdminRequired
* @CanManageContext
*
* 200: content updated successfully
* 400: Invalid request
* 403: No permissions
* 404: Not found
*/
public function updateContentOrder(int $contextId, int $pageId, array $content): DataResponse {
$context = $this->contextService->findById($contextId, $this->userId);
try {
$context = $this->contextService->findById($contextId, $this->userId);
} catch (Exception|InternalError $e) {
return $this->handleError($e);
} catch (NotFoundError $e) {
return $this->handleNotFoundError($e);
}
if (!isset($context->getPages()[$pageId])) {
return $this->handleBadRequestError(new BadRequestError('Page not found in given Context'));
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Db/ContextMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ public function findAll(?string $userId = null): array {
}

/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws Exception
* @throws NotFoundError
*/
public function findById(int $contextId, ?string $userId = null): Context {
$qb = $this->getFindContextBaseQuery($userId);
Expand Down
4 changes: 3 additions & 1 deletion lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\Tables\Db\PageMapper;
use OCA\Tables\Errors\BadRequestError;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand Down Expand Up @@ -77,8 +78,9 @@ public function findAll(?string $userId): array {
}

/**
* @return Context
* @throws Exception
* @throws InternalError
* @throws NotFoundError
*/
public function findById(int $id, ?string $userId): Context {
if ($userId !== null && trim($userId) === '') {
Expand Down
Loading

0 comments on commit f45d956

Please sign in to comment.