diff --git a/appinfo/routes.php b/appinfo/routes.php index 1a5e1798e..521b39c26 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -134,6 +134,6 @@ ['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'], - + ['name' => 'Context#updateContentOrder', 'url' => '/api/2/contexts/{contextId}/pages/{pageId}', 'verb' => 'PUT'], ] ]; diff --git a/lib/Controller/ContextController.php b/lib/Controller/ContextController.php index 08e00d32e..8f749ffdd 100644 --- a/lib/Controller/ContextController.php +++ b/lib/Controller/ContextController.php @@ -155,6 +155,19 @@ public function removeNode(int $contextId, int $nodeRelId): DataResponse { return new DataResponse(); } + /** + * @NoAdminRequired + * @CanManageContext + */ + public function updateContentOrder(int $contextId, int $pageId, array $content): DataResponse { + $context = $this->contextService->findById($contextId, $this->userId); + if (!isset($context->getPages()[$pageId])) { + return $this->handleBadRequestError(new BadRequestError('Page not found in given Context')); + } + + return new DataResponse($this->contextService->updateContentOrder($pageId, $content)); + } + /** * @param Context[] $contexts * @return array diff --git a/lib/Db/PageContentMapper.php b/lib/Db/PageContentMapper.php index 5a6810aab..dcd88fc01 100644 --- a/lib/Db/PageContentMapper.php +++ b/lib/Db/PageContentMapper.php @@ -2,6 +2,8 @@ namespace OCA\Tables\Db; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\Exception; use OCP\IDBConnection; @@ -14,6 +16,20 @@ public function __construct(IDBConnection $db) { parent::__construct($db, $this->table, PageContent::class); } + /** + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + * @throws Exception + */ + public function findById(int $pageContentId): PageContent { + $qb = $this->db->getQueryBuilder(); + $qb->select('*') + ->from($this->tableName) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($pageContentId))); + + return $this->mapRowToEntity($this->findOneQuery($qb)); + } + /** * @throws Exception */ diff --git a/lib/Service/ContextService.php b/lib/Service/ContextService.php index 7d09e0f3f..43dbdc6f1 100644 --- a/lib/Service/ContextService.php +++ b/lib/Service/ContextService.php @@ -4,6 +4,7 @@ namespace OCA\Tables\Service; +use InvalidArgumentException; use OCA\Tables\AppInfo\Application; use OCA\Tables\Db\Context; use OCA\Tables\Db\ContextMapper; @@ -207,6 +208,36 @@ public function removeNodeRelFromAllPages(ContextNodeRelation $nodeRelation): ar return $contents; } + public function updateContentOrder(int $pageId, array $contents): array { + $updated = []; + foreach ($contents as $content) { + try { + $updated[] = $this->updatePageContent($pageId, $content['id'], $content['order']); + } catch (DoesNotExistException|MultipleObjectsReturnedException|Exception|InvalidArgumentException $e) { + $this->logger->info('Could not updated order of content with ID {cID}', [ + 'cID' => $content['id'], + 'exception' => $e, + ]); + } + } + return $updated; + } + + /** + * @throws MultipleObjectsReturnedException + * @throws DoesNotExistException + * @throws Exception + * @throws InvalidArgumentException + */ + protected function updatePageContent(int $pageId, int $contentId, int $order): PageContent { + $pageContent = $this->pageContentMapper->findById($contentId); + if ($pageContent->getPageId() !== $pageId) { + throw new InvalidArgumentException('Content does not belong to given page'); + } + $pageContent->setOrder($order); + return $this->pageContentMapper->update($pageContent); + } + protected function insertPage(Context $context): void { $page = new Page(); $page->setContextId($context->getId());