Skip to content

Commit

Permalink
feat: comment delete and edit, view comments separately
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhradeepSS committed Dec 19, 2020
1 parent 6883eaa commit 1ea42ae
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 15 deletions.
12 changes: 9 additions & 3 deletions controllers/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { Movie } = require('../models/movie')

const getBlog = (req, res) => {
Blog.findOne({ _id: req.body.id }).then(blog => {
res.render('mdb/blog/blog', { blog, authUser: req.user })
Comment.find({ blog: blog._id }).then(comments => {
res.render('mdb/blog/blog', { blog, authUser: req.user, comments })
})
})
}

Expand Down Expand Up @@ -33,8 +35,12 @@ const editBlog = (req, res) => {
}

const deleteBlog = (req, res) => {
Blog.findByIdAndDelete(req.body.id).then(() => {
res.redirect('/mdb/movies')
Blog.findById(req.body.id).then(blog => {
Comment.deleteMany({ blog:blog._id }).then(() => {
blog.remove().then(() => {
res.redirect('/mdb/movies')
})
})
})
}

Expand Down
25 changes: 18 additions & 7 deletions controllers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@ const addComment = (req, res) => {
newComment.user = req.user
newComment.comment = req.body.comment
Blog.findById( req.body.id ).then(blog => {
newComment.blog = blog._id
newComment.save().then(() => {
blog.comments.push(newComment)
blog.save().then(() => {
res.redirect('/mdb/movies')
})
res.redirect('/mdb/movies')
})
})
}

const editComment = (req, res) => {

Comment.findById(req.body.id).then(comment => {
comment.comment = req.body.comment
comment.save().then(() => {
res.redirect('/mdb/')
})
})
}

const deleteComment = (req, res) => {

Comment.findByIdAndDelete(req.body.id).then(() => {
res.redirect('/mdb/')
})
}

const getYourComments = (req, res) => {
Comment.find({ user: req.user }).then(comments => {
res.render('mdb/comment/comments', { comments })
})
}


module.exports = { addComment, editComment, deleteComment }
module.exports = { addComment, editComment, deleteComment, getYourComments }
4 changes: 1 addition & 3 deletions models/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const Schema = mongoose.Schema

const { UserSchema } = require('./user')
const { MovieSchema } = require('./movie')
const { CommentSchema } = require('./comment')

const BlogSchema = new Schema({
title: {
Expand All @@ -21,8 +20,7 @@ const BlogSchema = new Schema({
movie: {
type: MovieSchema,
required: true
},
comments: [ CommentSchema ]
}
},
{
timestamps: true
Expand Down
5 changes: 5 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const CommentSchema = new Schema({
user: {
type: UserSchema,
required: true
},
blog: {
type: mongoose.Schema.Types.ObjectID,
required: true,
ref: 'Blog'
}
}, {
timestamps: true
Expand Down
3 changes: 2 additions & 1 deletion routes/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const router = express.Router()

const { ensureAuthenticated } = require("../conf/auth.js")

const { editComment, deleteComment } = require('../controllers/comment')
const { editComment, deleteComment, getYourComments } = require('../controllers/comment')

router.post('/edit', ensureAuthenticated, editComment)
router.post('/delete', ensureAuthenticated, deleteComment)
router.get('/', ensureAuthenticated, getYourComments)


module.exports = router
20 changes: 19 additions & 1 deletion views/mdb/blog/blog.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,29 @@
<h2>Comments: </h2>
<div>
<ul>
<% for(let comment of blog.comments) { %>
<% for(let comment of comments) { %>
<li>
<p><%= comment.comment %> </p>
<strong>by:</strong> <%= comment.user.username %> <br>
<strong>on:</strong> <%= comment.createdAt %> <br>
<% if(comment.user.username === authUser.username) { %>
<h3>Edit this comment</h3>
<form action="/mdb/comment/edit" method="post">
<input type="hidden" name="id" value="<%= comment._id %>">
<label for="comment">Comment:</label>
<input required type="text" name="comment">
<br>
<input type="submit" value="Edit">
</form>
<% } %>
<% if(comment.user.username === authUser.username || authUser.username === 'admin') { %>
<form action="/mdb/comment/delete" method="post">
<input type="hidden" name="id" value="<%= comment._id %>">
<input type="submit" value="Delete this comment">
</form>
<% } %>
<br>
</li>
<% } %>
Expand Down
15 changes: 15 additions & 0 deletions views/mdb/comment/comments.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>Your comments</h1>

<ul>
<% for(let comment of comments) { %>
<li>
<%= comment.comment %> <br>
<strong>on:</strong> <%= comment.createdAt %>
<br>
</li>
<% } %>
</ul>


<br>
<a href="/mdb/">Home</a>
3 changes: 3 additions & 0 deletions views/mdb/home.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<br>
<a href="/mdb/blog">Your blogs</a>

<br>
<a href="/mdb/comment">Your comments</a>

<br>
<a href="/mdb/ratings">Your ratings</a>

Expand Down

0 comments on commit 1ea42ae

Please sign in to comment.