Skip to content

Commit ee969bc

Browse files
committedOct 19, 2015
Added basic upvoting algorithm.
chapter13-1
1 parent cda6c6e commit ee969bc

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed
 

‎client/templates/posts/post_item.html

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<template name="postItem">
22
<div class="post">
3+
<a href="#" class="upvote btn btn-default"></a>
34
<div class="post-content">
45
<h3><a href="{{url}}">{{title}}</a><span>{{domain}}</span></h3>
56
<p>
7+
{{votes}} Votes,
68
submitted by {{author}},
79
<a href="{{pathFor 'postPage'}}">{{commentsCount}} comments</a>
810
{{#if ownPost}}<a href="{{pathFor 'postEdit'}}">Edit</a>{{/if}}

‎client/templates/posts/post_item.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ Template.postItem.helpers({
77
a.href = this.url;
88
return a.hostname;
99
}
10+
});
11+
12+
Template.postItem.events({
13+
'click .upvote': function(e) {
14+
e.preventDefault();
15+
Meteor.call('upvote', this._id);
16+
}
1017
});

‎lib/collections/posts.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,32 @@ Meteor.methods({
5656
userId: user._id,
5757
author: user.username,
5858
submitted: new Date(),
59-
commentsCount: 0
59+
commentsCount: 0,
60+
upvoters: [],
61+
votes: 0
6062
});
6163

6264
var postId = Posts.insert(post);
6365

6466
return {
6567
_id: postId
6668
};
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+
});
Has comments. Original line has comments.
6786
}
Has a comment. Original line has a comment.
6887
});

‎server/fixtures.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ if (Posts.find().count() === 0) {
1818
author: sacha.profile.name,
Has a comment. Original line has a comment.
1919
url: 'http://sachagreif.com/introducing-telescope/',
2020
submitted: new Date(now - 7 * 3600 * 1000),
21-
commentsCount: 2
21+
commentsCount: 2,
22+
upvoters: [], votes: 0
2223
});
2324

2425
Comments.insert({
@@ -43,7 +44,8 @@ if (Posts.find().count() === 0) {
4344
author: tom.profile.name,
4445
url: 'http://meteor.com',
4546
submitted: new Date(now - 10 * 3600 * 1000),
46-
commentsCount: 0
47+
commentsCount: 0,
48+
upvoters: [], votes: 0
4749
});
4850

4951
Posts.insert({
@@ -52,7 +54,8 @@ if (Posts.find().count() === 0) {
5254
author: tom.profile.name,
5355
url: 'http://themeteorbook.com',
5456
submitted: new Date(now - 12 * 3600 * 1000),
55-
commentsCount: 0
57+
commentsCount: 0,
58+
upvoters: [], votes: 0
5659
});
5760

5861
for (var i = 0; i < 10; i++) {
@@ -62,7 +65,8 @@ if (Posts.find().count() === 0) {
6265
userId: sacha._id,
6366
url: 'http://google.com/?q=test-' + i,
6467
submitted: new Date(now - i * 3600 * 1000 + 1),
65-
commentsCount: 0
68+
commentsCount: 0,
69+
upvoters: [], votes: 0
6670
});
6771
}
6872
}

0 commit comments

Comments
 (0)
Please sign in to comment.