Skip to content

Commit

Permalink
feat(*): add backend comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Aug 4, 2023
1 parent fc28318 commit 90ebc5d
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 59 deletions.
48 changes: 1 addition & 47 deletions controllers/article.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
93 changes: 93 additions & 0 deletions controllers/comment.js
Original file line number Diff line number Diff line change
@@ -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
};
14 changes: 7 additions & 7 deletions models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 13 additions & 5 deletions routes/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

0 comments on commit 90ebc5d

Please sign in to comment.