Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions controller/getComments.js
Original file line number Diff line number Diff line change
@@ -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 };
45 changes: 45 additions & 0 deletions controller/postComment.js
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 22 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -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);
23 changes: 13 additions & 10 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -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
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;