AllenYuan

articles enddpoints

May 10th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var router = require('express').Router();
  2. var passport = require('passport');
  3. var mongoose = require('mongoose');
  4. var Article = mongoose.model('Article');
  5. var User = mongoose.model('User');
  6. var auth = require('../auth');
  7.  
  8. // create articles endpoint
  9. router.post('/', auth.required, function(req, res, next) {
  10.   User.findById(req.payload.id).then(function(user){
  11.     if (!user) { return res.sendStatus(401); }
  12.     // create Article from request data
  13.     var article = new Article(req.body.article);
  14.     // mark article author
  15.     article.author = user;
  16.     // save article in db and return article object
  17.     return article.save().then(function(){
  18.       console.log(article.author);
  19.       return res.json({article: article.toJSONFor(user)});
  20.     });
  21.   }).catch(next);
  22. });
  23.  
  24. // router.param is an Express method to apply middleware to, in this case,
  25. // requests with an :article path parameter. Our middleware prepopulates
  26. // req.article with the article data before passing it to our endpoints
  27. router.param('article', function(req, res, next, slug) {
  28.   // read the slug path param and look up the article
  29.   Article.findOne({ slug: slug })
  30.     // look up the author id and replace it with the author object
  31.     .populate('author')
  32.     .then(function (article) {
  33.       // set the article and pass it to the next middleware/endpoint
  34.       if (!article) { return res.sendStatus(404); }
  35.       req.article = article;
  36.       return next();
  37.     }).catch(next);
  38. });
  39.  
  40. // GET /:article endpoint reads an article
  41. router.get('/:article', auth.optional, function(req, res, next) {
  42.   // grab the current user and article author
  43.   Promise.all([
  44.     req.payload ? User.findById(req.payload.id) : null,
  45.     req.article.populate('author').execPopulate()
  46.   ]).then(function(results){
  47.     // return article JSON
  48.     var user = results[0];
  49.     return res.json({article: req.article.toJSONFor(user)});
  50.   }).catch(next);
  51. });
  52.  
  53. // PUT /:article endpoint to update articles
  54. router.put('/:article', auth.required, function(req, res, next) {
  55.   // look up the user
  56.   User.findById(req.payload.id).then(function(user){
  57.     // if requester = article author, reset the article fields with data from request
  58.     if (req.article.author._id.toString() === req.payload.id.toString()){
  59.       if(typeof req.body.article.title !== 'undefined'){
  60.         req.article.title = req.body.article.title;
  61.       }
  62.  
  63.       if(typeof req.body.article.description !== 'undefined'){
  64.         req.article.description = req.body.article.description;
  65.       }
  66.  
  67.       if(typeof req.body.article.body !== 'undefined'){
  68.         req.article.body = req.body.article.body;
  69.       }
  70.  
  71.       req.article.save().then(function(article){
  72.         return res.json({article: article.toJSONFor(user)});
  73.       }).catch(next);
  74.     } else {
  75.       return res.sendStatus(403);
  76.     }
  77.   });
  78. });
  79.  
  80. // DELETE /:article to delete an article
  81. router.delete('/:article', auth.required, function(req, res, next) {
  82.   // look up requesting user, if it matches author, delete the article from db
  83.   User.findById(req.payload.id).then(function(){
  84.     if(req.article.author._id.toString() === req.payload.id.toString()){
  85.       return req.article.remove().then(function(){
  86.         return res.sendStatus(204);
  87.       });
  88.     } else {
  89.       return res.sendStatus(403);
  90.     }
  91.   });
  92. });
  93.  
  94. module.exports = router;
Add Comment
Please, Sign In to add comment