From e54c2a0570b4141ea2a0c74422a65920a4d6e210 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 9 Feb 2020 21:13:46 -0500 Subject: [PATCH] Add document deletion --- MongoView/Node.swift | 8 ++- .../QueryViewController.swift | 57 +++++++++++++++++-- .../View Controllers/QueryViewController.xib | 53 ++++++++++------- 3 files changed, 90 insertions(+), 28 deletions(-) diff --git a/MongoView/Node.swift b/MongoView/Node.swift index b53cf72..5e106ce 100644 --- a/MongoView/Node.swift +++ b/MongoView/Node.swift @@ -12,16 +12,17 @@ import MongoSwift class Node { let key: Key? let value: BSON + weak var parent: Node? lazy private(set) var children: [Node] = { switch value { case let .array(array): return array.enumerated().map { (index, val) in - Node(key: .index(index), value: val) + Node(key: .index(index), value: val, parent: self) } case let .document(doc): return doc.map { (key, val) in - Node(key: .name(key), value: val) + Node(key: .name(key), value: val, parent: self) } default: return [] @@ -36,8 +37,9 @@ class Node { numberOfChildren > 0 } - init(key: Key? = nil, value: BSON) { + init(key: Key? = nil, value: BSON, parent: Node? = nil) { self.value = value + self.parent = parent if key == nil, case let .document(doc) = value, diff --git a/MongoView/View Controllers/QueryViewController.swift b/MongoView/View Controllers/QueryViewController.swift index 1966a8e..0578afd 100644 --- a/MongoView/View Controllers/QueryViewController.swift +++ b/MongoView/View Controllers/QueryViewController.swift @@ -102,11 +102,51 @@ class QueryViewController: NSViewController { mostRecentQuery = queryTextView.string refresh() } - - rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map { - Node(value: $0) + + func deleteRootNode(_ node: Node) { + guard case let .document(doc) = node.value else { return } + let alert = NSAlert() + alert.alertStyle = .warning + alert.messageText = "Confirm deletion" + alert.informativeText = "Are you sure you want to delete the document" + let id: ObjectId? + if case let .objectId(docId) = doc["_id"] { + id = docId + alert.informativeText += " with id \(docId)" + } else { + id = nil + } + + alert.addButton(withTitle: "Delete") + alert.addButton(withTitle: "Cancel") + + alert.beginSheetModal(for: view.window!) { (response) in + guard response == .alertFirstButtonReturn else { return } + + self.mongoController.collection(self.collection).deleteOne(doc).whenComplete { (result) in + DispatchQueue.main.async { + switch result { + case let .success(result): + guard let result = result, result.deletedCount == 1 else { + let alert = NSAlert() + alert.alertStyle = .critical + alert.messageText = "Error deleting document" + if let id = id { + alert.informativeText = "The document with id \(id) could not be deleted." + } else { + alert.informativeText = "The document could not be deleted." + } + alert.beginSheetModal(for: self.view.window!, completionHandler: nil) + return + } + self.refresh() + case let .failure(error): + let alert = NSAlert(error: error) + alert.beginSheetModal(for: self.view.window!, completionHandler: nil) + } + } + } } - outlineView.reloadData() } @objc func outlineCellDoubleClicked() { @@ -118,6 +158,15 @@ class QueryViewController: NSViewController { } } } + + @IBAction func deleteNode(_ sender: Any) { + guard let node = outlineView.item(atRow: outlineView.clickedRow) as? Node else { + return + } + if node.parent == nil { + deleteRootNode(node) + } + } } diff --git a/MongoView/View Controllers/QueryViewController.xib b/MongoView/View Controllers/QueryViewController.xib index 9f23ad6..f53470d 100644 --- a/MongoView/View Controllers/QueryViewController.xib +++ b/MongoView/View Controllers/QueryViewController.xib @@ -1,7 +1,7 @@ - + - + @@ -994,19 +994,18 @@ - + - + - + - @@ -1018,11 +1017,11 @@ - + - + @@ -1037,9 +1036,8 @@ - + - @@ -1051,11 +1049,11 @@ - + - + @@ -1070,9 +1068,8 @@ - + - @@ -1084,11 +1081,11 @@ - + - + @@ -1107,16 +1104,16 @@ -