Search in sources :

Example 11 with Vote

use of com.faforever.api.data.domain.Vote in project faf-java-api by FAForever.

the class VotingService method saveVote.

@Transactional
public void saveVote(Vote vote, Player player) {
    vote.setPlayer(player);
    Assert.notNull(vote.getVotingSubject(), "You must specify a subject");
    List<Error> errors = ableToVote(player, vote.getVotingSubject().getId());
    if (vote.getVotingAnswers() == null) {
        vote.setVotingAnswers(Collections.emptySet());
    }
    VotingSubject subject = votingSubjectRepository.findById(vote.getVotingSubject().getId()).orElseThrow(() -> new IllegalArgumentException("Subject of vote not found"));
    vote.getVotingAnswers().forEach(votingAnswer -> {
        VotingChoice votingChoice = votingAnswer.getVotingChoice();
        VotingChoice one = votingChoiceRepository.findById(votingChoice.getId()).orElseThrow(() -> new ApiException(new Error(ErrorCode.VOTING_CHOICE_DOES_NOT_EXIST, votingChoice.getId())));
        votingAnswer.setVotingChoice(one);
        votingAnswer.setVote(vote);
    });
    subject.getVotingQuestions().forEach(votingQuestion -> {
        List<VotingAnswer> votingAnswers = vote.getVotingAnswers().stream().filter(votingAnswer -> votingAnswer.getVotingChoice().getVotingQuestion().equals(votingQuestion)).collect(Collectors.toList());
        long countOfAnswers = votingAnswers.size();
        int maxAnswers = votingQuestion.getMaxAnswers();
        if (maxAnswers < countOfAnswers) {
            errors.add(new Error(ErrorCode.TOO_MANY_ANSWERS, countOfAnswers, maxAnswers));
        }
        if (votingQuestion.isAlternativeQuestion()) {
            for (int i = 0; i < countOfAnswers; i++) {
                int finalI = i;
                long answersWithOrdinal = votingAnswers.stream().filter(votingAnswer -> Objects.equals(votingAnswer.getAlternativeOrdinal(), finalI)).count();
                if (answersWithOrdinal == 1) {
                    continue;
                }
                errors.add(new Error(ErrorCode.MALFORMATTED_ALTERNATIVE_ORDINALS));
            }
        }
    });
    if (!errors.isEmpty()) {
        throw new ApiException(errors.toArray(new Error[0]));
    }
    voteRepository.save(vote);
}
Also used : Error(com.faforever.api.error.Error) ErrorCode(com.faforever.api.error.ErrorCode) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) Transactional(javax.transaction.Transactional) ApiException(com.faforever.api.error.ApiException) GamePlayerStatsRepository(com.faforever.api.game.GamePlayerStatsRepository) Vote(com.faforever.api.data.domain.Vote) Player(com.faforever.api.data.domain.Player) Collectors(java.util.stream.Collectors) VotingChoice(com.faforever.api.data.domain.VotingChoice) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Validity(com.faforever.api.data.domain.Validity) Service(org.springframework.stereotype.Service) Optional(java.util.Optional) VotingSubject(com.faforever.api.data.domain.VotingSubject) Collections(java.util.Collections) Assert(org.springframework.util.Assert) VotingAnswer(com.faforever.api.data.domain.VotingAnswer) Error(com.faforever.api.error.Error) VotingSubject(com.faforever.api.data.domain.VotingSubject) VotingChoice(com.faforever.api.data.domain.VotingChoice) ApiException(com.faforever.api.error.ApiException) Transactional(javax.transaction.Transactional)

Aggregations

Vote (com.faforever.api.data.domain.Vote)11 VotingSubject (com.faforever.api.data.domain.VotingSubject)11 Player (com.faforever.api.data.domain.Player)10 Test (org.junit.Test)9 VotingQuestion (com.faforever.api.data.domain.VotingQuestion)8 VotingChoice (com.faforever.api.data.domain.VotingChoice)7 VotingAnswer (com.faforever.api.data.domain.VotingAnswer)5 ApiException (com.faforever.api.error.ApiException)5 Error (com.faforever.api.error.Error)2 ArrayList (java.util.ArrayList)2 Validity (com.faforever.api.data.domain.Validity)1 ErrorCode (com.faforever.api.error.ErrorCode)1 GamePlayerStatsRepository (com.faforever.api.game.GamePlayerStatsRepository)1 OffsetDateTime (java.time.OffsetDateTime)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Transactional (javax.transaction.Transactional)1