From 90ebc5dc151beeb4b3a6c0e3a08a26e0d17055e2 Mon Sep 17 00:00:00 2001 From: hoseacodes Date: Thu, 3 Aug 2023 22:15:24 -0500 Subject: [PATCH] feat(*): add backend comments --- controllers/article.js | 48 +--------------------- controllers/comment.js | 93 ++++++++++++++++++++++++++++++++++++++++++ models/article.js | 14 +++---- models/comment.js | 27 ++++++++++++ routes/articles.js | 18 +++++--- 5 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 controllers/comment.js create mode 100644 models/comment.js diff --git a/controllers/article.js b/controllers/article.js index a19c6bcb..dbaeb89a 100755 --- a/controllers/article.js +++ b/controllers/article.js @@ -1,5 +1,5 @@ import Articles from '../models/article.js'; -import Comments from '../models/article.js'; +import Comments from '../models/comment.js'; import Logger from '../utils/logger.js'; import {cache} from '../utils/cache.js'; import axios from 'axios' @@ -133,24 +133,6 @@ async function deleteArticle(req, res) { return res.status(500).json({ msg: err.message }) } } - - -async function deletePostcomment(req, res) { - try { - const post_id = req.body.post_id - - logger.info(`Deleted comment ${req.params.id} has been deleted`); - - await Articles.findByIdAndDelete(req.params.id) - res.clearCookie('comments-cache'); - res.json({ msg: "Deleted a article" }) - } catch (err) { - - logger.error(err) - - return res.status(500).json({ msg: err.message }) - } - } async function updateLikes(req, res) { try { @@ -252,40 +234,12 @@ async function archiveArticle(req, res) { } } -async function createArticleComment(req, res) { - try { - - const { article_id, post_id, comment, markdown, user_id, date_created } = req.body; - - const article = await Articles.findOne({ article_id }); - - const newComment = new Comments({ - article_id, post_id, comment, user_id, markdown, date_created - }) - - await newComment.save() - - logger.info(`New comment has been created`); - res.clearCookie('comments-cache'); - - res.json({ msg: "Created a new comment" }); - } catch (err) { - - logger.error(err) - - return res.status(500).json({ msg: err.message }) - } -} - - export { getArticle, createArticle, - createArticleComment, archiveArticle, deleteArticle, - deletePostcomment, updateArticle, updateArticleComment, updateLikes, diff --git a/controllers/comment.js b/controllers/comment.js new file mode 100644 index 00000000..55fe7272 --- /dev/null +++ b/controllers/comment.js @@ -0,0 +1,93 @@ +import Comments from '../models/comment.js'; +import Logger from '../utils/logger.js'; +import {cache} from '../utils/cache.js'; + +const logger = new Logger('comments') + +async function getComment(req, res) { + try { + const comments = await Comments.find() + + logger.info("Returning the list of comments"); + + res.cookie('comments-cache', comments.length + "comments", { + maxAge: 1000 * 60 * 60, // would expire after an hour + httpOnly: true, // The cookie only accessible by the web server + }) + + cache.set( comments.length + "comments", { + status: 'success', + comments: comments, + result: comments.length, + location: 'cache', + }); + + res.json({ + status: 'success', + comments: comments, + result: comments.length, + location: 'main', + }) + + } catch (err) { + + logger.error(err); + + return res.status(500).json({ msg: err.message }) + } +} + +async function createComment(req, res) { + try { + + const { comment_id, name, email, comment, markdown, user_id } = req.body; + console.log(req.body) + const com = await Comments.findOne({ comment_id }); + + if (com) { + logger.error("Comment already exist."); + return res.status(400).json({ msg: "This comment already exists." }) + } + + const newComment = new Comments({ + comment_id, name, email, + comment, markdown, user_id, + blog: req.params.id + }) + + await newComment.save() + + logger.info(`New comment has been created`); + res.clearCookie('comments-cache'); + + res.json({ msg: "Created a new comment" }); + } catch (err) { + + logger.error(err) + + return res.status(500).json({ msg: err.message }) + } +} + +async function deleteComment(req, res) { + try { + const post_id = req.params.id + + logger.info(`Deleted comment ${post_id} has been deleted`); + + await Comments.findByIdAndDelete(post_id) + res.clearCookie('comments-cache'); + res.json({ msg: "Deleted a article" }) + } catch (err) { + + logger.error(err) + + return res.status(500).json({ msg: err.message }) + } +} + +export { + createComment, + getComment, + deleteComment +}; diff --git a/models/article.js b/models/article.js index 8ad05486..88c9e37d 100755 --- a/models/article.js +++ b/models/article.js @@ -62,13 +62,13 @@ const articleSchema = new mongoose.Schema({ // required: true, // unique: true // }, - // comments: [{ - // text: String, - // postedBy: { - // type: mongoose.Schema.Types.ObjectId, - // ref: 'Users' - // } - // }], + comments: [{ + text: String, + postedBy: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Comments' + } + }], createdAt: { type: Date, default: Date.now diff --git a/models/comment.js b/models/comment.js new file mode 100644 index 00000000..d6300d4c --- /dev/null +++ b/models/comment.js @@ -0,0 +1,27 @@ +import mongoose from 'mongoose'; + +const commentSchema = new mongoose.Schema({ + name:{ + type:String, + required: "this field is required" + }, + email:{ + type:String, + required: "this field is required" + }, + comment:{ + type:String, + required:"this filed is required" + }, + blog:{ + type:mongoose.Schema.Types.ObjectId, + ref: 'Articles' + } + +},{ + timestamps: true +}) + +const Comments = mongoose.model('Comments', commentSchema); + +export default Comments; diff --git a/routes/articles.js b/routes/articles.js index 1c6263ac..e15772f8 100755 --- a/routes/articles.js +++ b/routes/articles.js @@ -2,17 +2,22 @@ import express from 'express'; import { getArticle, createArticle, - createArticleComment, archiveArticle, deleteArticle, - deletePostcomment, updateArticle, updateArticleComment, updateLikes } from '../controllers/article.js'; +import { + getComment, + createComment, + deleteComment +} from '../controllers/comment.js'; import {nodecache} from '../utils/cache.js'; const router = express.Router(); +const commentRouter = express.Router({mergeParams: true}); +router.use('/articles/:id/comments', commentRouter); router.route('/articles') .get(nodecache, getArticle) @@ -26,9 +31,12 @@ router.route('/articles/:id') router.route('/articles/:id/likes') .put(updateLikes) -router.route('/articles/:id/postcomments') - .post(createArticleComment) - .delete(deletePostcomment) +router.route('/articles/:id/comments') + .get(getComment) + .post(createComment) .put(updateArticleComment) +commentRouter.route('/:id') + .delete(deleteComment) + export default router;