Esempio n. 1
0
        private async Task <List <CandidateResult> > RetrieveAggregatedVotes(ElectionResultsQuery query, Ballot ballot)
        {
            switch (query.Division)
            {
            case ElectionDivision.County:
            {
                var result = await _winnersAggregator.GetLocalityCityHallWinnersByCounty(ballot.BallotId, query.CountyId.GetValueOrDefault());

                if (result.IsSuccess)
                {
                    var candidateResults = _winnersAggregator.RetrieveFirst10Winners(result.Value.Select(w => w.Candidate).ToList(), ballot.BallotType);

                    return(candidateResults);
                }
                throw new Exception(result.Error);
            }

            case ElectionDivision.National:
            {
                var result = await _winnersAggregator.GetAllLocalityWinners(ballot.BallotId);

                if (result.IsSuccess)
                {
                    var candidateResults = _winnersAggregator.RetrieveFirst10Winners(result.Value, ballot.BallotType);
                    return(candidateResults);
                }
                throw new Exception(result.Error);
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(query));
            }
        }
Esempio n. 2
0
        private async Task <List <CandidateResult> > RetrieveAggregatedVotes(ApplicationDbContext dbContext,
                                                                             ElectionResultsQuery query, Ballot ballot)
        {
            switch (query.Division)
            {
            case ElectionDivision.County:
            {
                var takeOnlyWinner = ballot.BallotType != BallotType.LocalCouncil;
                var result         = await _winnersAggregator.GetLocalityCityHallWinnersByCounty(ballot.BallotId, query.CountyId.GetValueOrDefault(), takeOnlyWinner);

                if (result.IsSuccess)
                {
                    var candidateResults = _winnersAggregator.RetrieveWinners(result.Value.Select(w => w.Candidate).ToList(), ballot.BallotType);

                    return(candidateResults);
                }
                throw new Exception(result.Error);
            }

            case ElectionDivision.National:
            {
                Result <List <CandidateResult> > result;
                if (ballot.BallotType == BallotType.CountyCouncil ||
                    ballot.BallotType == BallotType.CountyCouncilPresident)
                {
                    result = await _winnersAggregator.GetWinningCandidatesByCounty(ballot.BallotId);

                    if (result.IsSuccess)
                    {
                        return(_winnersAggregator.RetrieveWinners(result.Value, ballot.BallotType));
                    }
                }
                else
                {
                    var resultsForElection = await dbContext.CandidateResults
                                             .Include(c => c.Party)
                                             .Where(c => c.BallotId == query.BallotId && c.Division == ElectionDivision.Locality)
                                             .ToListAsync();

                    return(_winnersAggregator.RetrieveWinners(resultsForElection, ballot.BallotType));
                }
                throw new Exception(result.Error);
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(query));
            }
        }