Permalink
Browse files

Added basic upvoting algorithm.

chapter13-1
  • Loading branch information...
tmeasday committed Apr 8, 2013
1 parent cda6c6e commit ee969bcca51dcd6c07270d4a97de86d340c95edd
Showing with 37 additions and 5 deletions.
  1. +2 −0 client/templates/posts/post_item.html
  2. +7 −0 client/templates/posts/post_item.js
  3. +20 −1 lib/collections/posts.js
  4. +8 −4 server/fixtures.js
@@ -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}}
@@ -7,4 +7,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);
}
});
View
@@ -56,13 +56,32 @@ Meteor.methods({
userId: user._id,
author: user.username,
submitted: new Date(),
commentsCount: 0
commentsCount: 0,
upvoters: [],
votes: 0
});
var postId = Posts.insert(post);
return {
_id: postId
};
},
upvote: function(postId) {
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, {
$addToSet: {upvoters: this.userId},
$inc: {votes: 1}
});

This comment has been minimized.

Show comment
Hide comment
@lovetokill69

lovetokill69 Jan 13, 2016

return {
65 67  id: postId
66 68  };
69  + },
70  +
71  + upvote: function(postId) {
72  + check(this.userId, String);
73  + check(postId, String);
74  +
75  + var post = Posts.findOne(postId);
76  + if (!post)
77  + throw new Meteor.Error('invalid', 'Post not found');
78  +
79  + if (
.include(post.upvoters, this.userId))
80  + throw new Meteor.Error('invalid', 'Already upvoted this post');
81  +
82  + Posts.update(post._id, {
83  + $addToSet: {upvoters: this.userId},
84  + $inc: {votes: 1}
85  + });
67 86  }
68 87  });

@lovetokill69

lovetokill69 Jan 13, 2016

return {
65 67  id: postId
66 68  };
69  + },
70  +
71  + upvote: function(postId) {
72  + check(this.userId, String);
73  + check(postId, String);
74  +
75  + var post = Posts.findOne(postId);
76  + if (!post)
77  + throw new Meteor.Error('invalid', 'Post not found');
78  +
79  + if (
.include(post.upvoters, this.userId))
80  + throw new Meteor.Error('invalid', 'Already upvoted this post');
81  +
82  + Posts.update(post._id, {
83  + $addToSet: {upvoters: this.userId},
84  + $inc: {votes: 1}
85  + });
67 86  }
68 87  });

This comment has been minimized.

Show comment
Hide comment
@lovetokill69

lovetokill69 Jan 13, 2016

งง ทำไรกัน

@lovetokill69

lovetokill69 Jan 13, 2016

งง ทำไรกัน

}

This comment has been minimized.

Show comment
Hide comment
@lovetokill69

lovetokill69 Jan 13, 2016

79  + if (_.include(post.upvoters, this.userId))
80  + throw new Meteor.Error('invalid', 'Already upvoted this post');

@lovetokill69

lovetokill69 Jan 13, 2016

79  + if (_.include(post.upvoters, this.userId))
80  + throw new Meteor.Error('invalid', 'Already upvoted this post');

});
View
@@ -18,7 +18,8 @@ if (Posts.find().count() === 0) {
author: sacha.profile.name,

This comment has been minimized.

Show comment
Hide comment
@lovetokill69

lovetokill69 Jan 13, 2016

เอาไวทำไร
This text will be italic
This will also be italic

This text will be bold
This will also be bold

You can combine them

@lovetokill69

lovetokill69 Jan 13, 2016

เอาไวทำไร
This text will be italic
This will also be italic

This text will be bold
This will also be bold

You can combine them

url: 'http://sachagreif.com/introducing-telescope/',
submitted: new Date(now - 7 * 3600 * 1000),
commentsCount: 2
commentsCount: 2,
upvoters: [], votes: 0
});
Comments.insert({
@@ -43,7 +44,8 @@ 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({
@@ -52,7 +54,8 @@ 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++) {
@@ -62,7 +65,8 @@ if (Posts.find().count() === 0) {
userId: sacha._id,
url: 'http://google.com/?q=test-' + i,
submitted: new Date(now - i * 3600 * 1000 + 1),
commentsCount: 0
commentsCount: 0,
upvoters: [], votes: 0
});
}
}

0 comments on commit ee969bc

Please sign in to comment.