Added functuality to the edit button. Added router to /commets/:id/edit as well as commentEdit method in story js.
This commit is contained in:
35
controllers/story.js
Normal file → Executable file
35
controllers/story.js
Normal file → Executable file
@@ -423,6 +423,41 @@ exports.commentOnCommentSubmit = function(req, res, next) {
|
||||
commentSave(comment, Comment, res, next);
|
||||
};
|
||||
|
||||
exports.commentEdit = function(req, res, next){
|
||||
if (req.user._id.toString() !== req.body.data.author.userId.toString()) {
|
||||
return next(new Error('Not authorized'));
|
||||
}
|
||||
|
||||
var data = req.params.id;
|
||||
var sanitizedBody = sanitizeHtml(req.body.data.body,{
|
||||
allowedTags: [],
|
||||
allowedAttributes: []
|
||||
}).replace(/"/g, '"');
|
||||
if (req.body.data.body !== sanitizedBody) {
|
||||
req.flash('errors', {
|
||||
msg: 'HTML is not allowed'
|
||||
});
|
||||
return res.send(true);
|
||||
}
|
||||
|
||||
Comment.find({'_id': data}, function(err, cmt) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
cmt = cmt.pop();
|
||||
cmt.body = sanitizedBody;
|
||||
cmt.commentOn = Date.now();
|
||||
cmt.save(function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.send(true);
|
||||
});
|
||||
//commentSave(comment, Comment, res, next);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
function commentSave(comment, Context, res, next) {
|
||||
comment.save(function(err, data) {
|
||||
if (err) {
|
||||
|
Reference in New Issue
Block a user