Currently, users are only able to vote up or down once in general. I would like to allow users to vote up or down once per post.
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span class="glyphicon glyphicon-thumbs-up"
ng-click="incrementUpvotes(post)" ng-style="post.hadUpvoted ? {color: 'red'} : {}"></span>
{{post.upvotes}}
<span class="glyphicon glyphicon-thumbs-down" ng-click="downvote(post)"
ng-style="post.hadDownvoted ? {color:'red'} : {}"></span>
Controller:
var upvoted;
$scope.incrementUpvotes = function(post) {
if(!upvoted) {
posts.upvote(post);
upvoted = true;
post.hadUpvoted = true;
}
};
Service:
o.upvoteComment = function(post, comment) {
return $http.put('/posts/' + post._id + '/comments/' + comment._id + '/upvote', null, {
headers: {Authorization: 'Bearer '+auth.getToken()}
}).success(function(data) {
comment.upvotes += 1;
});
};