diff --git a/controller/getComments.js b/controller/getComments.js new file mode 100644 index 0000000..217edd4 --- /dev/null +++ b/controller/getComments.js @@ -0,0 +1,32 @@ +const Comment = require("../models/comment"); + +getComments = async (req, res) => { + const { id } = req.params; + + if (!Number(id)) { + return res.status(500).json({ + success: false, + error: "The comment id should be a number.", + }); + } + await Comment.find({ postId: id }, (err, comment) => { + if (err) { + return res.status(400).json({ + success: false, + error: err, + }); + } + if (!comment) { + return res.status(404).json({ + success: false, + error: `Comments not found`, + }); + } + return res.status(200).json({ + success: true, + data: comment, + }); + }).catch((err) => console.log(err)); +}; + +module.exports = { getComments }; diff --git a/controller/postComment.js b/controller/postComment.js new file mode 100644 index 0000000..fbd73c0 --- /dev/null +++ b/controller/postComment.js @@ -0,0 +1,45 @@ +const Comment = require("../models/comment"); + +/* +Also ripping off blog again, I somewhat feel I +am not demonstrating much in terms of knowledge +but also I do not want to reinvent the wheel +*/ + +postComment = (req, res) => { + const body = req.body; + + if (!body) { + return res.status(400).json({ + success: false, + error: "You must provide a comment", + }); + } + + const comment = new Comment(body); + + if (!comment) { + return res.status(400).json({ + success: false, + error: err, + }); + } + + comment + .save() + .then(() => { + return res.status(201).json({ + success: true, + id: comment._id, + message: "Comment created!", + }); + }) + .catch((error) => { + return res.status(400).json({ + error, + message: "Comment not created!", + }); + }); +}; + +module.exports = { postComment }; diff --git a/models/comment.js b/models/comment.js new file mode 100644 index 0000000..062e20a --- /dev/null +++ b/models/comment.js @@ -0,0 +1,22 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; + +/* +I could not find the resources folder with the example +json for comments so I used a modified Blog. Not very +creative on my part, but thinking from a user it already +contains all the information I would want from a comment +on my blog post. I left the id property because I imagine +in the real world there would be a need to access this specific +comment. +*/ + +const Comment = new Schema({ + commentId: { type: String, required: true }, + postId: { type: String, required: true }, + author: { type: String, required: true }, + title: { type: String, required: true }, + body: { type: String, required: true }, +}); + +module.exports = mongoose.model("comment", Comment); diff --git a/routes/index.js b/routes/index.js index f16015e..6b6021a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,12 +1,15 @@ -const express = require('express') -const { getBlogs } = require('../controller/getBlogs') -const { postBlog } = require('../controller/postBlog') -const { getBlog } = require('../controller/getBlog') +const express = require("express"); +const { getBlogs } = require("../controller/getBlogs"); +const { postBlog } = require("../controller/postBlog"); +const { getBlog } = require("../controller/getBlog"); +const { postComment } = require("../controller/postComment"); +const { getComments } = require("../controller/getComments"); -const router = express.Router() +const router = express.Router(); -router.get('/blog/:id', getBlog) -router.get('/blogs', getBlogs) -router.post('/blog/post', postBlog) - -module.exports = router \ No newline at end of file +router.get("/blog/:id", getBlog); +router.get("/blogs", getBlogs); +router.post("/blog/post", postBlog); +router.post("/blog/post/:id/comment", postComment); +router.get("/blog/post/:id/comment", getComments); +module.exports = router; \ No newline at end of file