diff --git a/lib/Db/ContextNodeRelationMapper.php b/lib/Db/ContextNodeRelationMapper.php index b445b2b56..e13b22ef9 100644 --- a/lib/Db/ContextNodeRelationMapper.php +++ b/lib/Db/ContextNodeRelationMapper.php @@ -28,6 +28,26 @@ public function deleteAllByContextId(int $contextId): void { $qb->executeStatement(); } + public function getRelIdsForNode(int $nodeId, int $nodeType): array { + $qb = $this->db->getQueryBuilder(); + $qb->select('id')->from($this->table)->where($qb->expr()->eq('node_id', $qb->createNamedParameter($nodeId))) + ->andWhere($qb->expr()->eq('node_type', $qb->createNamedParameter($nodeType))); + $result = $qb->executeQuery(); + $nodeRelIds = []; + while ($row = $result->fetch()) { + $nodeRelIds[] = (int)$row['id']; + } + return $nodeRelIds; + } + + public function deleteByNodeRelId(int $nodeRelId): int { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->table) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($nodeRelId, IQueryBuilder::PARAM_INT))); + return $qb->executeStatement(); + } + + /** * @throws MultipleObjectsReturnedException * @throws DoesNotExistException diff --git a/lib/Db/PageContentMapper.php b/lib/Db/PageContentMapper.php index a71251e2a..9a2529270 100644 --- a/lib/Db/PageContentMapper.php +++ b/lib/Db/PageContentMapper.php @@ -67,4 +67,11 @@ public function deleteByPageId(int $pageId): int { return $qb->executeStatement(); } + + public function deleteByNodeRelId(int $nodeRelId): int { + $qb = $this->db->getQueryBuilder(); + $qb->delete($this->table) + ->where($qb->expr()->eq('node_rel_id', $qb->createNamedParameter($nodeRelId, IQueryBuilder::PARAM_INT))); + return $qb->executeStatement(); + } } diff --git a/lib/Service/ContextService.php b/lib/Service/ContextService.php index 2e0e209f6..42d5e01f8 100644 --- a/lib/Service/ContextService.php +++ b/lib/Service/ContextService.php @@ -258,6 +258,19 @@ public function delete(int $contextId, string $userId): Context { return $context; } + public function deleteNodeRel(int $nodeId, int $nodeType): void { + try { + $nodeRelIds = $this->contextNodeRelMapper->getRelIdsForNode($nodeId, $nodeType); + foreach ($nodeRelIds as $nodeRelId) { + $this->pageContentMapper->deleteByNodeRelId($nodeRelId); + $this->contextNodeRelMapper->deleteByNodeRelId($nodeRelId); + } + + } catch (Exception $e) { + $this->logger->error('something went wrong while deleting node relation for node id: '.(string)$nodeId. ' and node type '.(string)$nodeType); + } + } + /** * @throws MultipleObjectsReturnedException * @throws DoesNotExistException diff --git a/lib/Service/TableService.php b/lib/Service/TableService.php index 892f7debc..a7d21584d 100644 --- a/lib/Service/TableService.php +++ b/lib/Service/TableService.php @@ -443,6 +443,9 @@ public function delete(int $id, ?string $userId = null): Table { // delete all shares for that table $this->shareService->deleteAllForTable($item); + // delete node relations if view is in any context + $this->contextService->deleteNodeRel($id, Application::NODE_TYPE_TABLE); + // delete table try { $this->mapper->delete($item); diff --git a/lib/Service/ViewService.php b/lib/Service/ViewService.php index 2c545886c..daa71ab85 100644 --- a/lib/Service/ViewService.php +++ b/lib/Service/ViewService.php @@ -302,6 +302,9 @@ public function delete(int $id, ?string $userId = null): View { } $this->shareService->deleteAllForView($view); + // delete node relations if view is in any context + $this->contextService->deleteNodeRel($id, Application::NODE_TYPE_VIEW); + try { $deletedView = $this->mapper->delete($view); @@ -335,6 +338,9 @@ public function deleteByObject(View $view, ?string $userId = null): View { // delete all shares for that table $this->shareService->deleteAllForView($view); + // delete node relations if view is in any context + $this->contextService->deleteNodeRel($view->getId(), Application::NODE_TYPE_VIEW); + $this->mapper->delete($view); $event = new ViewDeletedEvent(view: $view); diff --git a/src/pages/Context.vue b/src/pages/Context.vue index e165b6952..70286a306 100644 --- a/src/pages/Context.vue +++ b/src/pages/Context.vue @@ -5,7 +5,8 @@