Skip to content

Commit

Permalink
feat(Contexts): API to create a new 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 Feb 26, 2024
1 parent a9086d2 commit 0b3bc71
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 9 deletions.
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,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'],

]
];
21 changes: 21 additions & 0 deletions lib/Controller/ContextController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ public function show(int $contextId): DataResponse {
}
}

/**
* [api v2] Create a new context and return it
*
* @NoAdminRequired
*
* @param string $title Title of the table
* @param string|null $emoji Emoji for the table
* @param string $template Template to use if wanted
*
* @return DataResponse<Http::STATUS_OK, TablesTable, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedDocblockClass

lib/Controller/ContextController.php:85:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCA\Tables\Controller\TablesTable does not exist (see https://psalm.dev/200)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedDocblockClass

lib/Controller/ContextController.php:85:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCA\Tables\Controller\TablesTable does not exist (see https://psalm.dev/200)
*
* 200: Tables returned
*/
public function create(string $name, string $iconName, string $description = '', array $nodes = []): DataResponse {
try {
return new DataResponse($this->contextService->create($name, $iconName, $description, $nodes, $this->userId, 0)->jsonSerialize());
} catch (InternalError|Exception $e) {
return $this->handleError($e);
}
}

/**
* @param Context[] $contexts
* @return array
Expand Down
40 changes: 40 additions & 0 deletions lib/Db/ContextNodeRelation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\Entity;

/**
* @method getContextId(): int
* @method setContextId(int $value): void
* @method getNodeId(): int
* @method setNodeId(int $value): void
* @method getNodeType(): string
* @method setNodeType(string $value): void
* @method getPermissions(): int
* @method setPermissions(int $value): void
*/

class ContextNodeRelation extends Entity implements \JsonSerializable {
protected ?int $contextId = null;
protected ?int $nodeId = null;
protected ?string $nodeType = null;
protected ?int $permissions = null;

public function __construct() {
$this->addType('id', 'integer');
}

public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'contextId' => $this->getContextId(),
'nodeId' => $this->getNodeId(),
'nodeType' => $this->getNodeType(),
'permissions' => $this->getPermissions()
];
}
}

17 changes: 17 additions & 0 deletions lib/Db/ContextNodeRelationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;

class ContextNodeRelationMapper extends QBMapper {

Check failure on line 10 in lib/Db/ContextNodeRelationMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MissingTemplateParam

lib/Db/ContextNodeRelationMapper.php:10:7: MissingTemplateParam: OCA\Tables\Db\ContextNodeRelationMapper has missing template params when extending OCP\AppFramework\Db\QBMapper, expecting 1 (see https://psalm.dev/182)

Check failure on line 10 in lib/Db/ContextNodeRelationMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

MissingTemplateParam

lib/Db/ContextNodeRelationMapper.php:10:7: MissingTemplateParam: OCA\Tables\Db\ContextNodeRelationMapper has missing template params when extending OCP\AppFramework\Db\QBMapper, expecting 1 (see https://psalm.dev/182)
protected string $table = 'tables_contexts_rel_context_node';

public function __construct(IDBConnection $db) {
parent::__construct($db, $this->table, ContextNodeRelation::class);
}

}
32 changes: 32 additions & 0 deletions lib/Db/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\Entity;

/**
* @method getContextId(): int
* @method setContextId(int $value): void
* @method getPageType(): string
* @method setPageType(string $value): void
*/
class Page extends Entity implements \JsonSerializable {
public const TYPE_STARTPAGE = 'startpage';

protected ?int $contextId = null;
protected ?string $pageType = null;

public function __construct() {
$this->addType('id', 'integer');
}

public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'contextId' => $this->getContextId(),
'pageType' => $this->getPageType(),
];
}
}
34 changes: 34 additions & 0 deletions lib/Db/PageContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\Entity;

/**
* @method getPageId(): int
* @method setPageId(int $value): void
* @method getNodeRelId(): int
* @method setNodeRelId(int $value): void
* @method getOrder(): int
* @method setOrder(int $value): void
*/
class PageContent extends Entity implements \JsonSerializable {
protected ?int $pageId = null;
protected ?int $nodeRelId = null;
protected ?int $order = null;

public function __construct() {
$this->addType('id', 'integer');
}

public function jsonSerialize(): array {
return [
'id' => $this->getId(),
'pageId' => $this->getPageId(),
'nodeRelId' => $this->getNodeRelId(),
'order' => $this->getOrder(),
];
}
}
14 changes: 14 additions & 0 deletions lib/Db/PageContentMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;

class PageContentMapper extends QBMapper {

Check failure on line 8 in lib/Db/PageContentMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MissingTemplateParam

lib/Db/PageContentMapper.php:8:7: MissingTemplateParam: OCA\Tables\Db\PageContentMapper has missing template params when extending OCP\AppFramework\Db\QBMapper, expecting 1 (see https://psalm.dev/182)

Check failure on line 8 in lib/Db/PageContentMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

MissingTemplateParam

lib/Db/PageContentMapper.php:8:7: MissingTemplateParam: OCA\Tables\Db\PageContentMapper has missing template params when extending OCP\AppFramework\Db\QBMapper, expecting 1 (see https://psalm.dev/182)
protected string $table = 'tables_contexts_page_content';

public function __construct(IDBConnection $db) {
parent::__construct($db, $this->table, ContextNodeRelation::class);
}
}
14 changes: 14 additions & 0 deletions lib/Db/PageMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;

class PageMapper extends QBMapper {

Check failure on line 8 in lib/Db/PageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MissingTemplateParam

lib/Db/PageMapper.php:8:7: MissingTemplateParam: OCA\Tables\Db\PageMapper has missing template params when extending OCP\AppFramework\Db\QBMapper, expecting 1 (see https://psalm.dev/182)

Check failure on line 8 in lib/Db/PageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

MissingTemplateParam

lib/Db/PageMapper.php:8:7: MissingTemplateParam: OCA\Tables\Db\PageMapper has missing template params when extending OCP\AppFramework\Db\QBMapper, expecting 1 (see https://psalm.dev/182)
protected string $table = 'tables_contexts_page';

public function __construct(IDBConnection $db) {
parent::__construct($db, $this->table, ContextNodeRelation::class);
}
}
105 changes: 96 additions & 9 deletions lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,49 @@

namespace OCA\Tables\Service;

use OCA\Tables\AppInfo\Application;
use OCA\Tables\Db\Context;
use OCA\Tables\Db\ContextMapper;
use OCA\Tables\Db\ContextNodeRelation;
use OCA\Tables\Db\ContextNodeRelationMapper;
use OCA\Tables\Db\Page;
use OCA\Tables\Db\PageContent;
use OCA\Tables\Db\PageContentMapper;
use OCA\Tables\Db\PageMapper;
use OCA\Tables\Errors\InternalError;
use OCP\DB\Exception;
use Psr\Log\LoggerInterface;
use function OCP\Log\logger;

class ContextService {

private ContextMapper $mapper;
private ContextMapper $contextMapper;
private bool $isCLI;
private LoggerInterface $logger;
private ContextNodeRelationMapper $contextNodeRelMapper;
private PageMapper $pageMapper;
private PageContentMapper $pageContentMapper;

public function __construct(
ContextMapper $mapper,
LoggerInterface $logger,
bool $isCLI,
ContextMapper $contextMapper,
ContextNodeRelationMapper $contextNodeRelationMapper,
PageMapper $pageMapper,
PageContentMapper $pageContentMapper,
LoggerInterface $logger,
bool $isCLI,
) {
$this->mapper = $mapper;
$this->contextMapper = $contextMapper;
$this->isCLI = $isCLI;
$this->logger = $logger;
$this->contextNodeRelMapper = $contextNodeRelationMapper;
$this->pageMapper = $pageMapper;
$this->pageContentMapper = $pageContentMapper;
}

/**
* @throws InternalError
* @throws Exception
* @return Context[]
* @throws Exception
* @throws InternalError
*/
public function findAll(?string $userId): array {
if ($userId !== null && trim($userId) === '') {
Expand All @@ -40,7 +57,7 @@ public function findAll(?string $userId): array {
$this->logger->warning($error);
throw new InternalError($error);
}
return $this->mapper->findAll($userId);
return $this->contextMapper->findAll($userId);
}

/**
Expand All @@ -57,6 +74,76 @@ public function findById(int $id, ?string $userId): Context {
throw new InternalError($error);
}

return $this->mapper->findById($id, $userId);
return $this->contextMapper->findById($id, $userId);
}

public function create(string $name, string $iconName, string $description, array $nodes, string $ownerId, int $ownerType): Context {
$context = new Context();
$context->setName($name);
$context->setIcon($iconName);
$context->setDescription($description);
$context->setOwnerId($ownerId);
$context->setOwnerType($ownerType);

$this->contextMapper->insert($context);

if (!empty($nodes)) {
$context->resetUpdatedFields();
$this->insertNodesFromArray($context, $nodes);
$this->insertPage($context);
}

return $context;
}


protected function insertPage(Context $context) {
$page = new Page();
$page->setContextId($context->getId());
$page->setPageType(Page::TYPE_STARTPAGE);
$this->pageMapper->insert($page);

$addedPage = $page->jsonSerialize();

$i = 1;
foreach ($context->getNodes() as $node) {
$pageContent = new PageContent();
$pageContent->setPageId($page->getId());
$pageContent->setNodeRelId($node['id']);
$pageContent->setOrder(10 * $i++);

$this->pageContentMapper->insert($pageContent);

$addedPage['content'][$pageContent->getId()] = $pageContent->jsonSerialize();
// the content is already embedded in the page
unset($addedPage['content'][$pageContent->getId()]['pageId']);
}

$context->setPages($addedPage);
}

protected function insertNodesFromArray(Context $context, array $nodes) {
$addedNodes = [];

foreach ($nodes as $node) {
$contextNodeRel = new ContextNodeRelation();
$contextNodeRel->setContextId($context->getId());
$contextNodeRel->setNodeId($node['id']);
$contextNodeRel->setNodeType($node['type']);
$contextNodeRel->setPermissions($node['permissions'] ?? 660);

try {
$this->contextNodeRelMapper->insert($contextNodeRel);
$addedNodes[] = $contextNodeRel->jsonSerialize();
} catch (Exception $e) {
logger(Application::APP_ID)->warning('Could not add node {ntype}/{nid} to context {cid}', [

Check failure on line 139 in lib/Service/ContextService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedFunction

lib/Service/ContextService.php:139:5: UndefinedFunction: Function OCP\Log\logger does not exist (see https://psalm.dev/021)

Check failure on line 139 in lib/Service/ContextService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

UndefinedFunction

lib/Service/ContextService.php:139:5: UndefinedFunction: Function OCP\Log\logger does not exist (see https://psalm.dev/021)
'ntype' => $node['type'],
'nid' => $node['id'],
'cid' => $context['id'],
'exception' => $e,
]);
}
}
$context->setNodes($addedNodes);
}
}

0 comments on commit 0b3bc71

Please sign in to comment.