File tree 4 files changed +37
-5
lines changed
4 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 1
1
< template name ="postItem ">
2
2
< div class ="post ">
3
+ < a href ="# " class ="upvote btn btn-default "> ⬆</ a >
3
4
< div class ="post-content ">
4
5
< h3 > < a href ="{{url}} "> {{title}}</ a > < span > {{domain}}</ span > </ h3 >
5
6
< p >
7
+ {{votes}} Votes,
6
8
submitted by {{author}},
7
9
< a href ="{{pathFor 'postPage'}} "> {{commentsCount}} comments</ a >
8
10
{{#if ownPost}}< a href ="{{pathFor 'postEdit'}} "> Edit</ a > {{/if}}
Original file line number Diff line number Diff line change @@ -7,4 +7,11 @@ Template.postItem.helpers({
7
7
a . href = this . url ;
8
8
return a . hostname ;
9
9
}
10
+ } ) ;
11
+
12
+ Template . postItem . events ( {
13
+ 'click .upvote' : function ( e ) {
14
+ e . preventDefault ( ) ;
15
+ Meteor . call ( 'upvote' , this . _id ) ;
16
+ }
10
17
} ) ;
Original file line number Diff line number Diff line change @@ -56,13 +56,32 @@ Meteor.methods({
56
56
userId : user . _id ,
57
57
author : user . username ,
58
58
submitted : new Date ( ) ,
59
- commentsCount : 0
59
+ commentsCount : 0 ,
60
+ upvoters : [ ] ,
61
+ votes : 0
60
62
} ) ;
61
63
62
64
var postId = Posts . insert ( post ) ;
63
65
64
66
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
+ } ) ;
Has comments. Original line has comments. 67
86
}
Has a comment. Original line has a comment. 68
87
} ) ;
Original file line number Diff line number Diff line change @@ -18,7 +18,8 @@ if (Posts.find().count() === 0) {
18
18
author : sacha . profile . name ,
Has a comment. Original line has a comment. 19
19
url : 'http://sachagreif.com/introducing-telescope/' ,
20
20
submitted : new Date ( now - 7 * 3600 * 1000 ) ,
21
- commentsCount : 2
21
+ commentsCount : 2 ,
22
+ upvoters : [ ] , votes : 0
22
23
} ) ;
23
24
24
25
Comments . insert ( {
@@ -43,7 +44,8 @@ if (Posts.find().count() === 0) {
43
44
author : tom . profile . name ,
44
45
url : 'http://meteor.com' ,
45
46
submitted : new Date ( now - 10 * 3600 * 1000 ) ,
46
- commentsCount : 0
47
+ commentsCount : 0 ,
48
+ upvoters : [ ] , votes : 0
47
49
} ) ;
48
50
49
51
Posts . insert ( {
@@ -52,7 +54,8 @@ if (Posts.find().count() === 0) {
52
54
author : tom . profile . name ,
53
55
url : 'http://themeteorbook.com' ,
54
56
submitted : new Date ( now - 12 * 3600 * 1000 ) ,
55
- commentsCount : 0
57
+ commentsCount : 0 ,
58
+ upvoters : [ ] , votes : 0
56
59
} ) ;
57
60
58
61
for ( var i = 0 ; i < 10 ; i ++ ) {
@@ -62,7 +65,8 @@ if (Posts.find().count() === 0) {
62
65
userId : sacha . _id ,
63
66
url : 'http://google.com/?q=test-' + i ,
64
67
submitted : new Date ( now - i * 3600 * 1000 + 1 ) ,
65
- commentsCount : 0
68
+ commentsCount : 0 ,
69
+ upvoters : [ ] , votes : 0
66
70
} ) ;
67
71
}
68
72
}
You can’t perform that action at this time.