-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc28318
commit 90ebc5d
Showing
5 changed files
with
141 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters