comments.js 743 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * @file
  3. *
  4. *
  5. * User: marand
  6. * Date: 05/09/15
  7. * Time: 21:47
  8. */
  9. Comments = new Mongo.Collection('comments');
  10. Meteor.methods({
  11. commentInsert: function (commentAttributes) {
  12. check(this.userId, String);
  13. check(commentAttributes, {
  14. postId: String,
  15. body: String
  16. });
  17. var user = Meteor.user();
  18. var post = Posts.findOne(commentAttributes.postId);
  19. if (!post) {
  20. throw new Meteor.Error('invalid-comment', 'You must comment on a post');
  21. }
  22. var comment = _.extend(commentAttributes, {
  23. userId: user._id,
  24. author: user.username,
  25. submitted: new Date()
  26. });
  27. Posts.update(comment.postId, {
  28. $inc: { commentsCount: 1 }
  29. });
  30. return Comments.insert(comment);
  31. }
  32. });