| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- const Comment = require("./models/comment.js");
- module.exports = {
- /*
- GET: return all comments for the specific article
- req.params.article = String
- response = [Comment]
- */
- getComments: function(req, res){
- Comment.aggregate([
- {$match: {
- article: req.params.article
- }},
- {$sort: {
- date: -1
- }}
- ])
- .then((comments)=>{
- return res.json(comments);
- })
- .catch((err)=>{
- return res.json("Couldn't retrieve the comments");
- });
- },
- /*
- POST: create a new comment
- req.body = {
- article: String,
- name: String,
- content: String,
- date: Date
- }
- response = Comment
- */
- createComment: function(req, res){
- let comment = new Comment({
- article: req.body.article,
- name: req.body.name,
- content: req.body.content,
- date: new Date(req.body.date)
- });
- comment.save()
- .then((comment)=>{
- return res.json(comment);
- })
- .catch((err)=>{
- return res.json("Couldn't create your comment");
- });
- }
- }
|