Skip to content

Commit

Permalink
feat(Contexts): API to modify content order on a page
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 Feb 29, 2024
1 parent 03bb2ba commit 54b7c70
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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'],
]
];
13 changes: 13 additions & 0 deletions lib/Controller/ContextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/Db/PageContentMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down
31 changes: 31 additions & 0 deletions lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 54b7c70

Please sign in to comment.