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

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

            if (ballot.BallotType == BallotType.Mayor || ballot.BallotType == BallotType.LocalCouncil)
            {
                var results = await _dbContext.CandidateResults
                              .Include(c => c.Party)
                              .Where(c => c.BallotId == ballotId && c.Division == ElectionDivision.Locality)
                              .ToListAsync();

                var list = new List <CandidateResult>();
                var resultsForElection = results.GroupBy(c => c.CountyId);
                foreach (var countyGroup in resultsForElection)
                {
                    var countyWinners = countyGroup
                                        .GroupBy(c => c.LocalityId)
                                        .Select(g => g.OrderByDescending(x => x.Votes).FirstOrDefault()).ToList();
                    var             candidateResults = RetrieveWinners(countyWinners, ballot.BallotType);
                    CandidateResult topResult;
                    if (ballot.BallotType == BallotType.Mayor)
                    {
                        topResult = candidateResults.OrderByDescending(c => c.Votes).FirstOrDefault();
                    }
                    else
                    {
                        topResult = candidateResults.OrderByDescending(c => c.TotalSeats).FirstOrDefault();
                    }
                    list.Add(topResult);
                }
                return(list
                       .Select(c =>
                {
                    var turnout = new Turnout {
                        ValidVotes = c.Votes
                    };
                    if (ballot.BallotType == BallotType.LocalCouncil)
                    {
                        turnout.ValidVotes = c.TotalSeats;
                    }
                    return CreateElectionMapWinner(c.CountyId, ballot, c, turnout);
                }).ToList());
            }
            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 = await AggregateCountyWinners(ballotId, parties);

            return(Result.Success(winners.Select(winner => WinnerToElectionMapWinner(winner, parties)).ToList()));
        }
Exemple #2
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));
        }
Exemple #3
0
        private async Task <List <Winner> > GetWinners(int ballotId, int?countyId, ElectionDivision division)
        {
            var query = CreateWinnersQuery()
                        .Where(w => w.BallotId == ballotId &&
                               w.Division == division &&
                               w.CountyId == countyId).ToListAsync();
            var winnersKey = MemoryCache.CreateWinnersKey(ballotId, countyId, division);
            var winners    = await _appCache.GetOrAddAsync(winnersKey,
                                                           () => query, DateTimeOffset.Now.AddMinutes(10));

            return(winners);
        }
Exemple #4
0
        public async Task <Result <List <Winner> > > GetLocalityCityHallWinnersByCounty(int ballotId, int countyId, bool takeOnlyWinner = true)
        {
            var dbWinners = await GetWinners(ballotId, countyId, ElectionDivision.Locality);

            if (dbWinners.Count > 0)
            {
                return(dbWinners);
            }
            _appCache.Remove(MemoryCache.CreateWinnersKey(ballotId, countyId, ElectionDivision.Locality));
            var localities = await _dbContext.Localities.Where(l => l.CountyId == countyId).ToListAsync();

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

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

            List <Winner> winningCandidates = new List <Winner>();

            foreach (var locality in localities)
            {
                var results = candidateResultsForCounty
                              .Where(c => c.LocalityId == locality.LocalityId)
                              .OrderByDescending(c => c.Votes).ToList();
                var localityWinner = results
                                     .FirstOrDefault();
                var turnoutForLocality = turnouts
                                         .FirstOrDefault(c => c.LocalityId == locality.LocalityId);
                if (localityWinner != null)
                {
                    if (takeOnlyWinner)
                    {
                        winningCandidates.Add(CreateWinner(ballotId, countyId, localityWinner, turnoutForLocality, ElectionDivision.Locality));
                    }
                    else
                    {
                        foreach (var candidateResult in results)
                        {
                            winningCandidates.Add(CreateWinner(ballotId, countyId, candidateResult, turnoutForLocality, ElectionDivision.Locality));
                        }
                    }
                }
            }

            await SaveWinners(winningCandidates);

            return(Result.Success(winningCandidates));
        }
Exemple #5
0
        public async Task <Result <List <CandidateResult> > > GetWinningCandidatesByCounty(int ballotId)
        {
            var parties = await _partiesRepository.GetAllParties();

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

            if (dbWinners.Count > 0)
            {
                return(dbWinners.Select(w => w.Candidate).ToList());
            }
            _appCache.Remove(MemoryCache.CreateWinnersKey(ballotId, null, ElectionDivision.Diaspora_Country));
            var winners = await AggregateCountyWinners(ballotId, parties);

            return(winners.Select(w => w.Candidate).ToList());
        }
Exemple #6
0
        public async Task <Result <List <CandidateResult> > > GetAllLocalityWinners(int ballotId)
        {
            var dbWinners = await GetWinners(ballotId, null, ElectionDivision.Locality);

            if (dbWinners.Count > 0)
            {
                return(dbWinners.Select(w => w.Candidate).ToList());
            }
            _appCache.Remove(MemoryCache.CreateWinnersKey(ballotId, null, ElectionDivision.Locality));
            var winners       = new List <CandidateResult>();
            var allLocalities = await _dbContext.Localities.ToListAsync();

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

            var localitiesForThisElection = allLocalities
                                            .Where(l => resultsForElection.Any(r => r.LocalityId == l.LocalityId)).ToList();
            var turnouts = await _dbContext.Turnouts
                           .Where(c => c.BallotId == ballotId &&
                                  c.Division == ElectionDivision.Locality)
                           .ToListAsync();

            List <Winner> winningCandidates = new List <Winner>();

            foreach (var locality in localitiesForThisElection)
            {
                var localityWinner = resultsForElection
                                     .Where(c => c.LocalityId == locality.LocalityId)
                                     .OrderByDescending(c => c.Votes)
                                     .FirstOrDefault();
                if (localityWinner == null)
                {
                    continue;
                }
                var turnoutForLocality = turnouts
                                         .FirstOrDefault(c => c.LocalityId == locality.LocalityId);
                winners.Add(localityWinner);
                winningCandidates.Add(CreateWinner(ballotId, null, localityWinner, turnoutForLocality, ElectionDivision.Locality));
            }

            await SaveWinners(winningCandidates);

            return(Result.Success(winners));
        }