Skip to content

Instantly share code, notes, and snippets.

@LateButEarly
Created March 17, 2015 16:50
Show Gist options
  • Save LateButEarly/5fc3938c38e4720745ef to your computer and use it in GitHub Desktop.
Save LateButEarly/5fc3938c38e4720745ef to your computer and use it in GitHub Desktop.
working on comment creation
/**
* Create a new Comment
*/
exports.create = function(req, res) {
var comment = new Comment(req.body);
comment.poll = req.poll._id;
// if user
if (req.profile){ comment.user = req.profile;}
// if anonymous
else {comment.user_fingerprint = req.query.user_fingerprint;}
comment.save(function(err,comment){
if (err) return res.status(400).send({ message: errorHandler.getErrorMessage(err) });
// push comment id to polls comments array
Poll.findByIdAndUpdate(
comment.poll,
{'$push': {comments : {'_id':comment._id}}},
function(err,poll){
// calculate new poll weight
poll.weight = polls.calculatePollWeight(poll);
poll.save();
}
);
// if user
if (req.profile){
User.findByIdAndUpdate(
comment.user,
{'$push': {comments: {'_id':comment._id}}},
{safe:true,upsert:true}
);
}
// if comment is on poption, update poption's comments array
if (req.body.poption){
Poption.findByIdAndUpdate(
vote.poption,
{'$push': {votes: {'_id':vote._id}}},
{safe:true,upsert:true}
);
}
res.json(comment);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment