Skip to content

Commit b5670dc

Browse files
committed
up/downVotes done
1 parent 73f6754 commit b5670dc

File tree

4 files changed

+44
-52
lines changed

4 files changed

+44
-52
lines changed

client/js/controllers/post-ctrl.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,29 @@ angular.module('RDash')
3232
Comments.addComment($scope.comment)
3333
.then(function(resp) {
3434
resp._source.created_at = new Date(resp._source.created_at).toString();
35+
resp.votes = resp.votes || 0;
3536
$scope.data.comments.push(resp);
3637
})
3738
.catch(function(error) {
3839
console.log(error);
3940
});
4041
};
4142

42-
$rootScope.$on('showComments', function(event){
43-
$scope.fetch()
44-
});
45-
4643
$scope.upVote = function(commentID) {
47-
console.log("commentID ===== ", commentID)
4844
//use commentID to send the user into the comment.upVotes array
49-
Comments.upVote(commentID)
45+
Comments.upVote(commentID).then(function() {
46+
$scope.fetch();
47+
});
48+
5049
};
5150

52-
// $scope.downVote = function() {
53-
// Comments.downVote($stateParams.postID, user.id, function(resp){
54-
// $scope.data.comment.votes = resp.upVotes.length - resp.downVotes.length;
55-
// })
56-
// };
51+
$scope.downVote = function(commentID) {
52+
Comments.downVote(commentID).then(function() {
53+
// debugger
54+
$scope.fetch();
55+
});
56+
57+
};
5758

5859
$scope.$on('$viewContentLoaded', function(){
5960
$scope.simplemde = new SimpleMDE({

client/js/services/services.js

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

82-
var upVote = function(commentID, cb){
82+
var upVote = function(commentID){
8383
return $http({
8484
method: 'POST',
8585
url: '/api/comments/'+commentID+'/upvote'
86-
}).then(function (resp) {
87-
cb(resp.data);
8886
});
8987
};
9088

91-
// var downVote = function(commentID, user, cb){
92-
// return $http({
93-
// method: 'POST',
94-
// url: '/api/comments'+commentID,
95-
// data: user
96-
// }).then(function (resp) {
97-
// cb(resp.data);
98-
// });
99-
// };
89+
var downVote = function(commentID){
90+
return $http({
91+
method: 'POST',
92+
url: '/api/comments/'+commentID+'/downvote'
93+
});
94+
};
10095

10196
return {
10297
upVote: upVote,
103-
// downVote: downVote,
98+
downVote: downVote,
10499
getComments: getComments,
105100
addComment: addComment
106101
};

client/templates/post.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@
6464
<div ng-click='upVote(comment._id)'>
6565
<img src="http://icons.iconarchive.com/icons/icons8/windows-8/32/Hands-Thumbs-Up-icon.png"/>
6666
</div>
67-
<div ng-click="downVote()">
67+
<div ng-click="downVote(comment._id)">
6868
<img src="http://icons.iconarchive.com/icons/icons8/windows-8/32/Hands-Thumbs-Down-icon.png"/>
6969
</div>
7070
</div>
7171
<div class="pull-right" ng-model="comment.votes">
72-
{{comment._source.votes}}
72+
{{comment.votes}}
7373
</div>
7474
</div>
7575
</rd-widget-body>

server/controllers/comments.controllers.server.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var path = require('path');
55
var client = require(path.resolve('./lib/elasticsearch'));
66

77
exports.renderComments = function(req, res) {
8-
console.log("I am rendering Comments")
98
var postID = req.query.postID;
109
var search;
1110

@@ -22,14 +21,13 @@ exports.renderComments = function(req, res) {
2221
search.body.sort = {"created_at" : {"order" : "desc"}};
2322

2423
client.search(search).then(function (results){
25-
console.log("in !postID")
2624
res.send(results.hits.hits);
2725
});
2826
} else {
2927
search = {};
3028
search.index = 'comments';
3129
search.type = 'comment';
32-
search.size = 10;
30+
search.size = 1000;
3331
search.body = {};
3432
search.body.query = {};
3533
search.body.query.match = {};
@@ -42,11 +40,9 @@ exports.renderComments = function(req, res) {
4240

4341
client.search(search).then(function (result) {
4442
var hits = result.hits.hits;
45-
console.log(hits);
4643
for(var i=0; i<hits.length; i++) {
4744
hits[i].votes = hits[i]._source.upvotes.length - hits[i]._source.downvotes.length;
4845
}
49-
console.log("hits === ", hits);
5046
res.json(hits);
5147
});
5248
}
@@ -88,55 +84,55 @@ exports.storeComment = function(req, res) {
8884
};
8985

9086
exports.upvoteComment = function(req, res) {
91-
console.log("req.comment ==== ", req.comment)
87+
9288
var comment = req.comment;
93-
if(!comment.upvotes) {
94-
comment.upvotes = [];
89+
if(!comment._source.upvotes) {
90+
comment._source.upvotes = [];
9591
}
96-
if(!comment.downvotes) {
97-
comment.downvotes = [];
92+
if(!comment._source.downvotes) {
93+
comment._source.downvotes = [];
9894
}
99-
if(comment.downvotes.indexOf(req.session.user.id) > -1) {
100-
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);
10197
}
102-
if(comment.upvotes.indexOf(req.session.user.id) === -1) {
103-
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);
104100
}
105101
var update = {};
106102
update.index = 'comments';
107103
update.type = 'comment';
108104
update.id = comment._id;
109105
update.body = {};
110106
update.body.doc = {};
111-
update.body.upvotes = comment.upvotes;
112-
update.body.downvotes = comment.downvotes;
107+
update.body.doc.upvotes = comment._source.upvotes;
108+
update.body.doc.downvotes = comment._source.downvotes;
113109
client.update(update).then(function (result) {
114110
res.send(result);
115111
});
116112
};
117113

118114
exports.downvoteComment = function(req, res) {
119115
var comment = req.comment;
120-
if(!comment.downvotes) {
121-
comment.downvotes = [];
116+
if(!comment._source.downvotes) {
117+
comment._source.downvotes = [];
122118
}
123-
if(!comment.upvotes) {
124-
comment.upvotes = [];
119+
if(!comment._source.upvotes) {
120+
comment._source.upvotes = [];
125121
}
126-
if(comment.upvotes.indexOf(req.session.user.id) > -1) {
127-
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);
128124
}
129-
if(comment.downvotes.indexOf(req.session.user.id) === -1) {
130-
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);
131127
}
132128
var update = {};
133129
update.index = 'comments';
134130
update.type = 'comment';
135131
update.id = comment._id;
136132
update.body = {};
137133
update.body.doc = {};
138-
update.body.upvotes = comment.upvotes;
139-
update.body.downvotes = comment.downvotes;
134+
update.body.doc.upvotes = comment._source.upvotes;
135+
update.body.doc.downvotes = comment._source.downvotes;
140136
client.update(update).then(function (result) {
141137
res.send(result);
142138
});

0 commit comments

Comments
 (0)