controller.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const Comment = require("./models/comment.js");
  2. module.exports = {
  3. /*
  4. GET: return all comments for the specific article
  5. req.params.article = String
  6. response = [Comment]
  7. */
  8. getComments: function(req, res){
  9. Comment.find({article: req.params.article})
  10. .then((comments)=>{
  11. return res.json(comments);
  12. })
  13. .catch((err)=>{
  14. return res.json("Couldn't retrieve the comments");
  15. });
  16. },
  17. /*
  18. POST: create a new comment
  19. req.body = {
  20. article: String,
  21. name: String,
  22. content: String,
  23. date: Date
  24. }
  25. response = Comment
  26. */
  27. createComment: function(req, res){
  28. let comment = new Comment({
  29. article: req.body.article,
  30. name: req.body.name,
  31. content: req.body.content,
  32. date: new Date(req.body.date)
  33. });
  34. comment.save()
  35. .then((comment)=>{
  36. return res.json(comment);
  37. })
  38. .catch((err)=>{
  39. return res.json("Couldn't create your comment");
  40. });
  41. }
  42. }