AllenYuan

comment endpoints

May 14th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //...
  2.  
  3. // comment on an article
  4. router.post('/:article/comments', auth.required, function(req, res, next) {
  5.   // our auth middleware validates the user and puts the data in req.payload
  6.   // find requesting user
  7.   User.findById(req.payload.id).then(function(user){
  8.     // DNE => 401 forbidden
  9.     if (!user) { return res.sendStatus(401); }
  10.    
  11.     // comment obj
  12.     var comment = new Comment(req.body.comment);
  13.     comment.article = req.article;
  14.     comment.author = user;
  15.  
  16.     // save comment to db and add to article comments list
  17.     return comment.save().then(function() {
  18.       req.article.comments = req.article.comments.concat([comment]);
  19.       // save article update and return comment
  20.       return req.article.save().then(function(article) {
  21.         res.json({comment: comment.toJSONFor(user)});
  22.       });
  23.     });
  24.   }).catch(next);
  25. });
  26.  
  27. // list comments on an article
  28. router.get('/:article/comments', auth.optional, function(req, res, next){
  29.   // grab user if logged in
  30.   Promise.resolve(req.payload ? User.findById(req.payload.id) : null).then(function(user){
  31.     // populate the comments and author of each comment (lookup data of an id) and return array
  32.     // sorted by newest to oldest comment.
  33.     return req.article.populate({
  34.       path: 'comments',
  35.       populate: {
  36.         path: 'author'
  37.       },
  38.       options: {
  39.         sort: {
  40.           createdAt: 'desc'
  41.         }
  42.       }
  43.     }).execPopulate().then(function(article) {
  44.       // return JSON of each comment
  45.       return res.json({comments: req.article.comments.map(function(comment){
  46.         return comment.toJSONFor(user);
  47.       })});
  48.     });
  49.   }).catch(next);
  50. });
  51.  
  52. // middleware to resolve :comment path variable
  53. router.param('comment', function(req, res, next, id) {
  54.   // grab comment by id and put it into the request
  55.   Comment.findById(id).then(function(comment){
  56.     if(!comment) { return res.sendStatus(404); }
  57.     req.comment = comment;
  58.  
  59.     return next();
  60.   }).catch(next);
  61. });
  62.  
  63. router.delete('/:article/comments/:comment', auth.required, function(req, res, next) {
  64.   // if requester=author remove comment from article, save schema, remove comment from db
  65.   if (req.comment.author.toString() === req.payload.id.toString()) {
  66.     req.article.comments.remove(req.comment._id);
  67.     req.article.save()
  68.       .then(Comment.find({_id: req.comment._id}).remove().exec())
  69.       .then(function(){
  70.         res.sendStatus(204);
  71.       });
  72.   } else {
  73.     res.sendStatus(403);
  74.   }
  75. });
  76.  
  77. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment