@RoleHolderPresent
public static Result voteDown(String moduleKey)
{
Result result;
final Module module = Module.findByModuleKey(moduleKey);
if (module == null)
{
result = notFound("Module not found: " + moduleKey);
}
else
{
User user = currentUser();
Vote vote = CollectionUtils.filterFirst(user.votes,
new Filter<Vote>()
{
@Override
public boolean isAcceptable(Vote vote)
{
return vote.playModule.id.equals(module.id);
}
});
if (vote != null)
{
if (vote.voteType == Vote.VoteType.UP)
{
--module.upVoteCount;
++module.downVoteCount;
vote.voteType = Vote.VoteType.DOWN;
vote.save();
module.save();
}
else
{
Logger.info(String.format("User [%s] tried to downvote module [%s] but vote already existed",
user.userName,
module.key));
}
}
else
{
vote = new Vote();
vote.playModule = module;
vote.voteType = Vote.VoteType.DOWN;
// support for public voting comes later
vote.publicVote = false;
user.votes.add(vote);
user.save();
++module.downVoteCount;
module.save();
}
VoteResponseForm responseForm = new VoteResponseForm();
responseForm.upVotes = module.upVoteCount;
responseForm.downVotes = module.downVoteCount;