use of org.mamute.model.Vote in project mamute by caelum.
the class VoteDAO method previousVoteFor.
public Vote previousVoteFor(Serializable id, User currentUser, Class type) {
String hql = "select v from " + type.getSimpleName() + " q join q.votes as v where v.author = :author and q.id = :votable";
Query query = session.createQuery(hql);
return (Vote) query.setParameter("author", currentUser).setParameter("votable", id).setMaxResults(1).uniqueResult();
}
use of org.mamute.model.Vote in project mamute by caelum.
the class VotingMachine method register.
public void register(Votable votable, Vote current, Class<?> votableType) {
User voter = current.getAuthor();
User votableAuthor = votable.getAuthor();
ReputationEventContext eventContext = votes.contextOf(votable);
if (votable.getAuthor().getId().equals(voter.getId())) {
throw new IllegalArgumentException("an author can't vote its own votable");
}
Vote previous = votes.previousVoteFor(votable.getId(), voter, votableType);
boolean shouldCountKarma = voteChecker.shouldCountKarma(voter, votableAuthor, current);
if (previous != null) {
ReputationEvent receivedVote = new ReceivedVoteEvent(previous.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
votableAuthor.descreaseKarma(karmaCalculator.karmaFor(receivedVote));
ReputationEvent votedAtSomething = new VotedAtSomethingEvent(previous, eventContext).reputationEvent();
voter.descreaseKarma(karmaCalculator.karmaFor(votedAtSomething));
reputationEvents.delete(receivedVote);
reputationEvents.delete(votedAtSomething);
}
votable.substitute(previous, current);
ReputationEvent receivedVote = new ReceivedVoteEvent(current.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
votableAuthor.increaseKarma(karmaCalculator.karmaFor(receivedVote));
ReputationEvent votedAtSomething = new VotedAtSomethingEvent(current, eventContext).reputationEvent();
voter.increaseKarma(karmaCalculator.karmaFor(votedAtSomething));
reputationEvents.save(receivedVote);
reputationEvents.save(votedAtSomething);
if (votable.getVoteCount() <= -5) {
votable.getQuestion().remove();
retrieveDownvote.retrieveKarma(votable.getVotes());
}
}
use of org.mamute.model.Vote in project mamute by caelum.
the class VoteDAOTest method upvote.
private Vote upvote(Votable votable, User user) {
Vote vote = new Vote(user, VoteType.UP);
session.save(votable);
session.save(vote);
votingMachine.register(votable, vote, Comment.class);
return vote;
}
use of org.mamute.model.Vote in project mamute by caelum.
the class QuestionTest method should_remove_vote_values_and_update_vote_count.
@Test
public void should_remove_vote_values_and_update_vote_count() {
Question myQuestion = question.build();
assertEquals(0l, myQuestion.getVoteCount());
Vote firstVote = new Vote(null, VoteType.UP);
myQuestion.substitute(null, firstVote);
assertEquals(1l, myQuestion.getVoteCount());
myQuestion.substitute(firstVote, new Vote(null, VoteType.DOWN));
assertEquals(-1l, myQuestion.getVoteCount());
myQuestion.substitute(null, new Vote(null, VoteType.DOWN));
assertEquals(-2l, myQuestion.getVoteCount());
}
use of org.mamute.model.Vote in project mamute by caelum.
the class VotingMachineTest method should_decrese_karma_of_downvoter.
@Test
public void should_decrese_karma_of_downvoter() throws Exception {
Vote newVote = new Vote(voter, VoteType.DOWN);
votingMachine.register(votable, newVote, Question.class);
assertEquals(-2l, voter.getKarma());
}
Aggregations