diff options
| author | Bobby <[email protected]> | 2022-03-21 01:32:51 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-03-21 01:32:51 -0400 |
| commit | d760ad4ae6d33daf121b1a310fd10764afc326c8 (patch) | |
| tree | 8dbf1b7cc68cfe78d36b9b1bec7f6361700b3769 | |
| parent | 6a43cd05abf6be7d30e405f2e797738ffeaf659a (diff) | |
| download | luciferreeves.github.io-d760ad4ae6d33daf121b1a310fd10764afc326c8.tar.xz luciferreeves.github.io-d760ad4ae6d33daf121b1a310fd10764afc326c8.zip | |
Delete post function
| -rw-r--r-- | public/views/dashboard.html | 11 | ||||
| -rw-r--r-- | routes/blog.js | 20 |
2 files changed, 31 insertions, 0 deletions
diff --git a/public/views/dashboard.html b/public/views/dashboard.html index 03dd653..65db495 100644 --- a/public/views/dashboard.html +++ b/public/views/dashboard.html @@ -91,6 +91,17 @@ const deleteButton = document.createElement('a'); deleteButton.className = 'btn btn-danger'; deleteButton.innerHTML = 'Delete Post'; + deleteButton.addEventListener('click', () => { + if (confirm('Are you sure you want to delete this post?')) { + $.ajax({ + url: `/api/blog/delete/${post.slug}`, + type: 'DELETE', + success: () => { + window.location.reload(); + } + }); + } + }); const editButtonContainer = document.createElement('div'); editButtonContainer.className = 'btn-group'; editButtonContainer.appendChild(editButton); diff --git a/routes/blog.js b/routes/blog.js index 8a8cb19..29dbb65 100644 --- a/routes/blog.js +++ b/routes/blog.js @@ -53,6 +53,26 @@ router.put("/update/:slug", (req, res) => { }); }); +router.delete("/delete/:slug", (req, res) => { + const store = firebase.firestore(); + let query = store.collection("posts"); + query = query.where("slug", "==", req.params.slug); + query + .get() + .then(function (querySnapshot) { + querySnapshot.forEach(function (doc) { + doc.ref.delete(); + }); + }) + .then(() => { + res.json({ success: true }); + }) + .catch((err) => { + res.json({ success: false, err }); + }); +}); + + router.post("/new", (req, res) => { const { title, content, tags, publishDate, shortText, slug } = req.body; const store = firebase.firestore(); |
