浏览代码

Commit 13-1: Added basic upvoting algorithm.

Frederic G. MARAND 9 年之前
父节点
当前提交
18bfe5fe49
共有 4 个文件被更改,包括 44 次插入7 次删除
  1. 3 1
      client/templates/posts/post_item.html
  2. 8 1
      client/templates/posts/post_item.js
  3. 21 1
      lib/collections/posts.js
  4. 12 4
      server/fixtures.js

+ 3 - 1
client/templates/posts/post_item.html

@@ -1,8 +1,10 @@
 <template name="postItem">
   <div class="post">
+    <a href="#" class="upvote btn btn-default">⬆</a>
     <div class="post-content">
       <h3><a href="{{url}}">{{title}}</a><span>{{domain}}</span></h3>
       <p>
+        {{votes}} Votes,
         submitted by {{author}},
         <a href="{{pathFor 'postPage' }}">{{commentsCount}} comments</a>
         {{#if ownPost}}<a href="{{pathFor 'postEdit' }}">Edit</a>{{/if}}
@@ -16,4 +18,4 @@
     }}
     <a href="{{pathFor 'postPage'}}" class="discuss btn btn-default">Discuss</a>
   </div>
-</template>
+</template>

+ 8 - 1
client/templates/posts/post_item.js

@@ -9,4 +9,11 @@ Template.postItem.helpers({
     a.href = this.url;
     return a.hostname;
   }
-});
+});
+
+Template.postItem.events({
+  'click .upvote': function (e) {
+    e.preventDefault();
+    Meteor.call('upvote', this._id);
+  }
+});

+ 21 - 1
lib/collections/posts.js

@@ -70,7 +70,9 @@ Meteor.methods({
       userId: user._id,
       author: user.username,
       submitted: new Date(),
-      commentsCount: 0
+      commentsCount: 0,
+      upvoters: [],
+      votes: 0
     });
 
     var postId =  Posts.insert(post);
@@ -78,5 +80,23 @@ Meteor.methods({
     return {
       _id: postId
     };
+  },
+  upvote: function (postId) {
+    check(this.userId, Match.OneOf(null, String));
+    check(postId, String);
+    if (this.userId === null) {
+      throw new Meteor.Error('invalid', 'Not logged in');
+    }
+    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, {
+      $addToSet: { upvoters: this.userId },
+      $inc: { votes: 1 }
+    });
   }
 });

+ 12 - 4
server/fixtures.js

@@ -19,7 +19,9 @@ if (Posts.find().count() === 0) {
     author: sacha.profile.name,
     url: 'http://sachagreif.com/introducing-telescope/',
     submitted: new Date(now - 7 * 3600 * 1000),
-    commentsCount: 2
+    commentsCount: 2,
+    upvoters: [],
+    votes: 0
   });
 
   Comments.insert({
@@ -44,7 +46,9 @@ if (Posts.find().count() === 0) {
     author: tom.profile.name,
     url: 'http://meteor.com',
     submitted: new Date(now - 10 * 3600 * 1000),
-    commentsCount: 0
+    commentsCount: 0,
+    upvoters: [],
+    votes: 0
   });
 
   Posts.insert({
@@ -53,7 +57,9 @@ if (Posts.find().count() === 0) {
     author: tom.profile.name,
     url: 'http://themeteorbook.com',
     submitted: new Date(now - 12 * 3600 * 1000),
-    commentsCount: 0
+    commentsCount: 0,
+    upvoters: [],
+    votes: 0
   });
 
   for (var i = 0; i < 10; i++) {
@@ -63,7 +69,9 @@ if (Posts.find().count() === 0) {
       userId: sacha._id,
       url: 'http://google.com/?q=test-' + i,
       submitted: new Date(now - i * 3600 * 1000),
-      commentsCount: 0
+      commentsCount: 0,
+      upvoters: [],
+      votes: 0
     });
   }}
 else {