示例#1
0
        public async Task <Result <List <ElectionMapWinner> > > GetCountyWinners(int ballotId)
        {
            var parties = await _partiesRepository.GetAllParties();

            var dbWinners = await GetWinners(ballotId, null, ElectionDivision.County);

            if (dbWinners.Count > 0)
            {
                return(dbWinners.Select(winner => WinnerToElectionMapWinner(winner, parties)).ToList());
            }
            _appCache.Remove(MemoryCache.CreateWinnersKey(ballotId, null, ElectionDivision.Diaspora_Country));
            var winners  = new List <ElectionMapWinner>();
            var counties = await _territoryRepository.GetCounties();

            var ballot = await _dbContext.Ballots
                         .AsNoTracking()
                         .Include(b => b.Election)
                         .FirstOrDefaultAsync(b => b.BallotId == ballotId);

            var candidateResultsByCounties = await _dbContext.CandidateResults
                                             .Include(c => c.Party)
                                             .Where(c => c.BallotId == ballotId &&
                                                    c.Division == ElectionDivision.County)
                                             .ToListAsync();

            var turnouts = await _dbContext.Turnouts
                           .Where(c => c.BallotId == ballotId &&
                                  c.Division == ElectionDivision.County)
                           .ToListAsync();

            var winningCandidates = new List <Winner>();

            foreach (var county in counties.Value)
            {
                var countyWinner = candidateResultsByCounties
                                   .Where(c => c.CountyId == county.CountyId)
                                   .OrderByDescending(c => c.Votes)
                                   .FirstOrDefault();

                var turnoutForCounty = turnouts
                                       .FirstOrDefault(c => c.CountyId == county.CountyId);

                if (countyWinner == null || turnoutForCounty == null)
                {
                    continue;
                }
                var electionMapWinner = CreateElectionMapWinner(county.CountyId, ballot, countyWinner, turnoutForCounty);
                if (electionMapWinner.Winner.PartyColor.IsEmpty())
                {
                    electionMapWinner.Winner.PartyColor = parties.ToList().GetMatchingParty(countyWinner.ShortName)?.Color;
                }
                winners.Add(electionMapWinner);
                winningCandidates.Add(CreateWinner(ballotId, countyWinner, electionMapWinner, turnoutForCounty.Id, county.CountyId, ElectionDivision.County));
            }
            await SaveWinners(winningCandidates);

            return(Result.Success(winners));
        }
示例#2
0
        private async Task <List <Winner> > AggregateCountyWinners(int ballotId, IEnumerable <Party> parties)
        {
            var counties = await _territoryRepository.GetCounties();

            var ballot = await _dbContext.Ballots
                         .Include(b => b.Election)
                         .FirstOrDefaultAsync(b => b.BallotId == ballotId);

            var candidateResultsByCounties = await _dbContext.CandidateResults
                                             .Include(c => c.Party)
                                             .Where(c => c.BallotId == ballotId &&
                                                    c.Division == ElectionDivision.County)
                                             .ToListAsync();

            var turnouts = await _dbContext.Turnouts
                           .Where(c => c.BallotId == ballotId &&
                                  c.Division == ElectionDivision.County)
                           .ToListAsync();

            var winningCandidates = new List <Winner>();

            foreach (var county in counties.Value)
            {
                var countyWinner = candidateResultsByCounties
                                   .Where(c => c.CountyId == county.CountyId)
                                   .OrderByDescending(c => c.Votes)
                                   .FirstOrDefault();

                var turnoutForCounty = turnouts
                                       .FirstOrDefault(c => c.CountyId == county.CountyId);

                if (countyWinner == null || turnoutForCounty == null)
                {
                    continue;
                }
                var electionMapWinner = CreateElectionMapWinner(county.CountyId, ballot, countyWinner, turnoutForCounty);
                if (electionMapWinner.Winner.PartyColor.IsEmpty())
                {
                    electionMapWinner.Winner.PartyColor = parties.ToList().GetMatchingParty(countyWinner.ShortName)?.Color ?? Consts.IndependentCandidateColor;
                }
                winningCandidates.Add(CreateWinner(ballot, countyWinner, electionMapWinner, turnoutForCounty.Id,
                                                   county.CountyId, ElectionDivision.County));
            }

            await SaveWinners(winningCandidates);

            return(winningCandidates);
        }
示例#3
0
        public async Task <ActionResult <List <LocationData> > > GetCounties()
        {
            try
            {
                var countiesResult = await _territoryRepository.GetCounties();

                if (countiesResult.IsSuccess)
                {
                    return(countiesResult.Value.Select(c => new LocationData
                    {
                        Id = c.CountyId,
                        Name = c.Name
                    }).ToList());
                }

                return(StatusCode(500, countiesResult.Error));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }