Permalink
Browse files

Better upvoting algorithm.

chapter13-4
  • Loading branch information...
tmeasday committed Apr 8, 2013
1 parent 1480289 commit de1c9103488ef8475bd274bd5c5992ade8b23f7a
Showing with 7 additions and 8 deletions.
  1. +7 −8 lib/collections/posts.js
View
@@ -72,16 +72,15 @@ Meteor.methods({
check(this.userId, String);
check(postId, String);
- var post = Posts.findOne(postId);
- if (!post)
- throw new Meteor.Error('invalid', 'Post not found');
-
- if (_.include(post.upvoters, this.userId))
- throw new Meteor.Error('invalid', 'Already upvoted this post');
-
- Posts.update(post._id, {
+ var affected = Posts.update({
+ _id: postId,
+ upvoters: {$ne: this.userId}
+ }, {
$addToSet: {upvoters: this.userId},
$inc: {votes: 1}
});
+
+ if (! affected)
+ throw new Meteor.Error('invalid', "You weren't able to upvote that post");
}
});

0 comments on commit de1c910

Please sign in to comment.