Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add backend commit #431

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
57 changes: 57 additions & 0 deletions server/controllers/post.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,63 @@ export function addPost(req, res) {
res.json({ post: saved });
});
}
export function addComment(req,res){
if (!req.params.cuid || !req.body.userName ||!req.body.text) {
res.status(403).end();
}
const {cuid } = req.body


const comment = {
_id:new Date(),
userName:req.body.userName,
text:req.body.text,
}
bulkUpdateOps.push({
updateOne: {
filter: { cuid },
update: { $set: { comment } },
}
})


Post.bulkWrite(bulkUpdateOps).exec((err, post)=>{
if(err){
res.status(500).send(err);
}
res.status(200).end();
})
}
export function editComment(req,res){
if (!req.params.cuid || !req.body.userName ||!req.body.text||!req.body._id) {
res.status(403).end();
}
const {cuid,_id} = req.body
const comment = {
_id:_id,
userName:req.body.userName,
text:req.body.text,
}
Post.findOneAndUpdate({cuid,comment:{_id}},{comment},{upsert:true}).exec((err)=>{
if(err){
res.status(500).send(err);
}
res.status(200).end();
})
}
export function deleteComment(req,res){
if(!req.params.cuid||!req.body._id){
res.status(403).end();
}
const {cuid,_id} = req.body
Post.deleteOne({cuid,comment:{_id}}).exec((err)=>{
if(err){
res.status(500).send(err);
}
res.status(200).end();
})

}

/**
* Get a single post
Expand Down
1 change: 1 addition & 0 deletions server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const postSchema = new Schema({
content: { type: 'String', required: true },
slug: { type: 'String', required: true },
cuid: { type: 'String', required: true },
commit :{},
dateAdded: { type: 'Date', default: Date.now, required: true },
});

Expand Down
12 changes: 12 additions & 0 deletions server/routes/post.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ router.route('/posts').post(PostController.addPost);
// Delete a post by cuid
router.route('/posts/:cuid').delete(PostController.deletePost);

//add Comment a post by cuid
router.route('/posts/:cuid').put(PostController.addComment);

//edit Comment
router.route('/posts/edit/:cuid').put(PostController.editComment);

//delete Comment
router.route('/posts/delete/:cuid').delete(PostController.editComment);




export default router;