From 021e30f6382ab88afea3dd101529979ebc2ca8c3 Mon Sep 17 00:00:00 2001 From: Cleopatra Enjeck M Date: Wed, 19 Jun 2024 07:05:47 +0100 Subject: [PATCH] fix: Remove deleted resource from associated applications Signed-off-by: Cleopatra Enjeck M --- lib/Db/ContextNodeRelationMapper.php | 20 +++++++++++ lib/Db/PageContentMapper.php | 7 ++++ lib/Service/ContextService.php | 13 ++++++++ lib/Service/TableService.php | 3 ++ lib/Service/ViewService.php | 6 ++++ src/pages/Context.vue | 50 ++++++++++++++++------------ 6 files changed, 77 insertions(+), 22 deletions(-) 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 @@

-   {{ activeContext.name }} +   {{ + activeContext.name }}

@@ -149,29 +150,34 @@ export default { const nodeType = parseInt(node.node_type) if (nodeType === NODE_TYPE_TABLE) { const table = this.tables.find(table => table.id === node.node_id) - await this.$store.dispatch('loadColumnsFromBE', { - view: null, - tableId: table.id, - }) - await this.$store.dispatch('loadRowsFromBE', { - viewId: null, - tableId: table.id, - }) - table.key = (table.id).toString() - table.isView = false - this.contextResources.push(table) + if (table) { + await this.$store.dispatch('loadColumnsFromBE', { + view: null, + tableId: table.id, + }) + await this.$store.dispatch('loadRowsFromBE', { + viewId: null, + tableId: table.id, + }) + table.key = (table.id).toString() + table.isView = false + this.contextResources.push(table) + } + } else if (nodeType === NODE_TYPE_VIEW) { const view = this.views.find(view => view.id === node.node_id) - await this.$store.dispatch('loadColumnsFromBE', { - view, - }) - await this.$store.dispatch('loadRowsFromBE', { - viewId: view.id, - tableId: view.tableId, - }) - view.key = 'view-' + (view.id).toString() - view.isView = true - this.contextResources.push(view) + if (view) { + await this.$store.dispatch('loadColumnsFromBE', { + view, + }) + await this.$store.dispatch('loadRowsFromBE', { + viewId: view.id, + tableId: view.tableId, + }) + view.key = 'view-' + (view.id).toString() + view.isView = true + this.contextResources.push(view) + } } } }