aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-05-03 17:44:38 +0200
committerMateja <mail@matejamaric.com>2021-05-03 17:44:38 +0200
commit34b3fefaddd165b649c53e140f6b749c18943d26 (patch)
tree3c53c59d29df1fc1d3165eddd7c83ed3aab871f1
parent9eeca255cfe41ca4a962422c3703274caa530021 (diff)
downloadnode-playground-34b3fefaddd165b649c53e140f6b749c18943d26.tar.gz
node-playground-34b3fefaddd165b649c53e140f6b749c18943d26.zip
Add delete option on every post.
-rw-r--r--controllers/post.js4
-rw-r--r--public/css/style.css3
-rw-r--r--routes/web.js1
-rw-r--r--views/home.handlebars3
4 files changed, 10 insertions, 1 deletions
diff --git a/controllers/post.js b/controllers/post.js
index 7c66dec..d879fcc 100644
--- a/controllers/post.js
+++ b/controllers/post.js
@@ -25,6 +25,10 @@ module.exports = {
'text': req.body.text
});
newPost.save().then(() => res.redirect('/'));
+ },
+
+ destroy(req, res) {
+ Post.findByIdAndRemove(req.params.id).then(() => res.redirect('/'));
}
};
diff --git a/public/css/style.css b/public/css/style.css
index d3a017d..d8fb0d5 100644
--- a/public/css/style.css
+++ b/public/css/style.css
@@ -20,6 +20,9 @@ footer, nav {
padding: 1rem;
border-radius: 0 35%; }
+a.badge { text-decoration: none; }
+a.badge:hover { color: white; }
+
.btn {
background: #002147;
color: white; }
diff --git a/routes/web.js b/routes/web.js
index 732c87c..ee07d85 100644
--- a/routes/web.js
+++ b/routes/web.js
@@ -5,5 +5,6 @@ const postController = require('../controllers/post');
router.get('/', postController.index);
router.get('/new-post', postController.create);
router.post('/new-post', postController.store);
+router.get('/remove-post/:id', postController.destroy);
module.exports = router;
diff --git a/views/home.handlebars b/views/home.handlebars
index 768b62d..6061e4e 100644
--- a/views/home.handlebars
+++ b/views/home.handlebars
@@ -2,7 +2,7 @@
<h1 class="mt-2">Posts</h1>
{{#each posts}}
- <div class="card mt-3">
+ <div class="card mb-3">
<div class="card-header">
{{this.title}}
</div>
@@ -10,6 +10,7 @@
<div class="card-text">
{{this.text}}
</div>
+ <a href="/remove-post/{{this._id}}" class="badge float-end">Remove</a>
</div>
</div>
{{/each}}