Skip to content

Commit bdf1ee9

Browse files
committed
Merge pull request #109 from Kevbyte/upvotes3
Upvotes3
2 parents cae7185 + 219e48a commit bdf1ee9

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

client/js/controllers/post-ctrl.js

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
angular.module('RDash')
8+
89
.controller('PostCtrl', function PostCtrl($scope, $stateParams, $location, Posts, Comments) {
910
$scope.data = {};
1011

@@ -31,6 +32,7 @@ angular.module('RDash')
3132
Comments.addComment($scope.comment)
3233
.then(function(resp) {
3334
resp._source.created_at = new Date(resp._source.created_at).toString();
35+
resp.votes = resp.votes || 0;
3436
$scope.data.comments.push(resp);
3537
$scope.simplemde.value('');
3638
})
@@ -39,6 +41,22 @@ angular.module('RDash')
3941
});
4042
};
4143

44+
$scope.upVote = function(commentID) {
45+
//use commentID to send the user into the comment.upVotes array
46+
Comments.upVote(commentID).then(function() {
47+
$scope.fetch();
48+
});
49+
50+
};
51+
52+
$scope.downVote = function(commentID) {
53+
Comments.downVote(commentID).then(function() {
54+
// debugger
55+
$scope.fetch();
56+
});
57+
58+
};
59+
4260
$scope.$on('$viewContentLoaded', function(){
4361
$scope.simplemde = new SimpleMDE({
4462
tabSize: 2

client/js/services/services.js

+16
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,23 @@ angular.module('RDash.services', [])
7979
});
8080
};
8181

82+
var upVote = function(commentID){
83+
return $http({
84+
method: 'POST',
85+
url: '/api/comments/'+commentID+'/upvote'
86+
});
87+
};
88+
89+
var downVote = function(commentID){
90+
return $http({
91+
method: 'POST',
92+
url: '/api/comments/'+commentID+'/downvote'
93+
});
94+
};
95+
8296
return {
97+
upVote: upVote,
98+
downVote: downVote,
8399
getComments: getComments,
84100
addComment: addComment
85101
};

client/templates/post.html

+14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@
5858
</div>
5959
</div>
6060
</div>
61+
<div class="votes pull-right">
62+
63+
<div class="pull-left">
64+
<div ng-click='upVote(comment._id)'>
65+
<img src="http://icons.iconarchive.com/icons/icons8/windows-8/32/Hands-Thumbs-Up-icon.png"/>
66+
</div>
67+
<div ng-click="downVote(comment._id)">
68+
<img src="http://icons.iconarchive.com/icons/icons8/windows-8/32/Hands-Thumbs-Down-icon.png"/>
69+
</div>
70+
</div>
71+
<div class="pull-right" ng-model="comment.votes">
72+
<h2>{{comment.votes}}</h2>
73+
</div>
74+
</div>
6175
</rd-widget-body>
6276
</rd-widget>
6377
<br/>

server/controllers/comments.controllers.server.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exports.renderComments = function(req, res) {
2727
search = {};
2828
search.index = 'comments';
2929
search.type = 'comment';
30-
search.size = 10;
30+
search.size = 1000;
3131
search.body = {};
3232
search.body.query = {};
3333
search.body.query.match = {};
@@ -40,7 +40,6 @@ exports.renderComments = function(req, res) {
4040

4141
client.search(search).then(function (result) {
4242
var hits = result.hits.hits;
43-
console.log(hits);
4443
for(var i=0; i<hits.length; i++) {
4544
hits[i].votes = hits[i]._source.upvotes.length - hits[i]._source.downvotes.length;
4645
}
@@ -85,54 +84,55 @@ exports.storeComment = function(req, res) {
8584
};
8685

8786
exports.upvoteComment = function(req, res) {
87+
8888
var comment = req.comment;
89-
if(!comment.upvotes) {
90-
comment.upvotes = [];
89+
if(!comment._source.upvotes) {
90+
comment._source.upvotes = [];
9191
}
92-
if(!comment.downvotes) {
93-
comment.downvotes = [];
92+
if(!comment._source.downvotes) {
93+
comment._source.downvotes = [];
9494
}
95-
if(comment.downvotes.indexOf(req.session.user.id) > -1) {
96-
comment.downvotes.splice(comment.downvotes.indexOf(req.session.user.id), 1);
95+
if(comment._source.downvotes.indexOf(req.session.user.id) > -1) {
96+
comment._source.downvotes.splice(comment._source.downvotes.indexOf(req.session.user.id), 1);
9797
}
98-
if(comment.upvotes.indexOf(req.session.user.id) === -1) {
99-
comment.upvotes.push(req.session.user.id);
98+
if(comment._source.upvotes.indexOf(req.session.user.id) === -1) {
99+
comment._source.upvotes.push(req.session.user.id);
100100
}
101101
var update = {};
102102
update.index = 'comments';
103103
update.type = 'comment';
104104
update.id = comment._id;
105105
update.body = {};
106106
update.body.doc = {};
107-
update.body.upvotes = comment.upvotes;
108-
update.body.downvotes = comment.downvotes;
107+
update.body.doc.upvotes = comment._source.upvotes;
108+
update.body.doc.downvotes = comment._source.downvotes;
109109
client.update(update).then(function (result) {
110110
res.send(result);
111111
});
112112
};
113113

114114
exports.downvoteComment = function(req, res) {
115115
var comment = req.comment;
116-
if(!comment.downvotes) {
117-
comment.downvotes = [];
116+
if(!comment._source.downvotes) {
117+
comment._source.downvotes = [];
118118
}
119-
if(!comment.upvotes) {
120-
comment.upvotes = [];
119+
if(!comment._source.upvotes) {
120+
comment._source.upvotes = [];
121121
}
122-
if(comment.upvotes.indexOf(req.session.user.id) > -1) {
123-
comment.upvotes.splice(comment.upvotes.indexOf(req.session.user.id), 1);
122+
if(comment._source.upvotes.indexOf(req.session.user.id) > -1) {
123+
comment._source.upvotes.splice(comment._source.upvotes.indexOf(req.session.user.id), 1);
124124
}
125-
if(comment.downvotes.indexOf(req.session.user.id) === -1) {
126-
comment.downvotes.push(req.session.user.id);
125+
if(comment._source.downvotes.indexOf(req.session.user.id) === -1) {
126+
comment._source.downvotes.push(req.session.user.id);
127127
}
128128
var update = {};
129129
update.index = 'comments';
130130
update.type = 'comment';
131131
update.id = comment._id;
132132
update.body = {};
133133
update.body.doc = {};
134-
update.body.upvotes = comment.upvotes;
135-
update.body.downvotes = comment.downvotes;
134+
update.body.doc.upvotes = comment._source.upvotes;
135+
update.body.doc.downvotes = comment._source.downvotes;
136136
client.update(update).then(function (result) {
137137
res.send(result);
138138
});

0 commit comments

Comments
 (0)