private async Task <Turnout> GetDivisionTurnout(ElectionResultsQuery query, ApplicationDbContext dbContext, Ballot ballot) { if (ballot.Election.Category == ElectionCategory.Local && !ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live) { return(await RetrieveAggregatedTurnoutForCityHalls(query, ballot, dbContext)); } return(await dbContext.Turnouts .FirstOrDefaultAsync(t => t.BallotId == ballot.BallotId && t.CountyId == query.CountyId && t.CountryId == query.CountryId && t.LocalityId == query.LocalityId)); }
private async Task <LiveElectionInfo> GetCandidatesFromDb(ElectionResultsQuery query, Ballot ballot, ApplicationDbContext dbContext) { LiveElectionInfo liveElectionInfo = new LiveElectionInfo(); if (ballot.Election.Category == ElectionCategory.Local && query.CountyId.GetValueOrDefault().IsCapitalCity() == false) { if (!ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live) { var aggregatedVotes = await RetrieveAggregatedVotes(dbContext, query, ballot); liveElectionInfo.Candidates = aggregatedVotes; liveElectionInfo.Aggregated = true; return(liveElectionInfo); } } var results = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext); liveElectionInfo.Candidates = results; return(liveElectionInfo); }
public async Task <Turnout> GetDivisionTurnout(ElectionResultsQuery query, ApplicationDbContext dbContext, Ballot ballot) { if (ballot.Election.Category == ElectionCategory.Local && !ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live) { return(await RetrieveAggregatedTurnoutForCityHalls(query, ballot, dbContext)); } var turnouts = await dbContext.Turnouts .Where(t => t.BallotId == ballot.BallotId && t.CountyId == query.CountyId && t.CountryId == query.CountryId && t.Division == query.Division && t.LocalityId == query.LocalityId).ToListAsync(); if (turnouts.Count > 0 && query.Division == ElectionDivision.Diaspora_Country) { var turnout = AggregateTurnouts(turnouts); turnout.BallotId = ballot.BallotId; return(turnout); } if (turnouts.Count == 0 && query.Division == ElectionDivision.Diaspora) { turnouts = await dbContext.Turnouts .Where(t => t.BallotId == ballot.BallotId && t.CountyId == null && t.Division == ElectionDivision.Diaspora_Country && t.LocalityId == null).ToListAsync(); var turnout = AggregateTurnouts(turnouts); turnout.BallotId = ballot.BallotId; return(turnout); } return(turnouts.FirstOrDefault()); }
private async Task <LiveElectionInfo> GetCandidatesFromDb(ElectionResultsQuery query, Ballot ballot, ApplicationDbContext dbContext) { LiveElectionInfo liveElectionInfo = new LiveElectionInfo(); if (ballot.Election.Live) { if (query.Division == ElectionDivision.National) { return(await _appCache.GetOrAddAsync( $"{ballot.BallotType}-national", () => _resultsCrawler.AggregateNationalResults(query, ballot), DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes))); } if (query.Division == ElectionDivision.Diaspora) { return(await _appCache.GetOrAddAsync( $"{ballot.BallotType}-diaspora", () => _resultsCrawler.AggregateDiasporaResults(query, ballot), DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes))); } if (query.Division == ElectionDivision.Diaspora_Country) { return(await _appCache.GetOrAddAsync( $"{ballot.BallotType}-diaspora_country-{query.CountryId}", () => _resultsCrawler.ImportCountryResults(query, ballot), DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes))); } var county = await dbContext.Counties.FirstOrDefaultAsync(c => c.CountyId == query.CountyId); if (county.CountyId.IsCapitalCity()) { int?index = null; if (query.Division == ElectionDivision.Locality) { var sector = await dbContext.Localities.FirstOrDefaultAsync(l => l.LocalityId == query.LocalityId); index = int.Parse(sector.Name.Split(" ").Last()); } return(await _resultsCrawler.ImportCapitalCityResults(ballot, index)); } if (query.Division == ElectionDivision.Locality) { return(await _resultsCrawler.ImportLocalityResults(ballot, query)); } var url = _urlBuilder.GetFileUrl(ballot.BallotType, query.Division, county?.ShortName, query.LocalityId); if (url.IsFailure) { return(LiveElectionInfo.Default); } var result = await _appCache.GetOrAddAsync( $"{url}", () => _resultsCrawler.Import(url.Value), DateTimeOffset.Now.AddMinutes(_settings.CsvCacheInMinutes)); if (result.IsSuccess) { return(result.Value); } return(LiveElectionInfo.Default); } if (ballot.Election.Category == ElectionCategory.Local && query.CountyId.GetValueOrDefault().IsCapitalCity() == false) { if (!ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live) { var aggregatedVotes = await RetrieveAggregatedVotes(dbContext, query, ballot); liveElectionInfo.Candidates = aggregatedVotes; liveElectionInfo.Aggregated = true; return(liveElectionInfo); } } var results = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext); liveElectionInfo.Candidates = results; return(liveElectionInfo); }
private async Task <LiveElectionInfo> GetCandidatesFromDb(ElectionResultsQuery query, Ballot ballot, ApplicationDbContext dbContext) { LiveElectionInfo liveElectionInfo = new LiveElectionInfo(); if (ballot.Election.Live && ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault())) { try { int?siruta; var county = await _territoryRepository.GetCountyById(query.CountyId); if (query.LocalityId.GetValueOrDefault().IsCapitalCity() && query.Division == ElectionDivision.County) { siruta = null; } else { var locality = await _territoryRepository.GetLocalityById(query.LocalityId); if (locality.IsFailure) { return(LiveElectionInfo.Default); } siruta = locality.IsSuccess ? locality.Value?.Siruta : null; } if (county.IsFailure) { return(LiveElectionInfo.Default); } var countyShortName = county.IsSuccess ? county.Value.ShortName : null; var url = _liveElectionUrlBuilder.GetFileUrl(ballot.BallotType, query.Division, countyShortName, siruta); if (url.IsFailure) { return(LiveElectionInfo.Default); } liveElectionInfo = await _csvDownloaderJob.GetCandidatesFromUrl(url.Value); var candidates = liveElectionInfo.Candidates; var parties = await dbContext.Parties.ToListAsync(); var candidatesForThisElection = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext); var dbCandidates = new List <CandidateResult>(); if (candidates == null) { liveElectionInfo.Candidates = dbCandidates; return(liveElectionInfo); } foreach (var candidate in candidates) { dbCandidates.Add(PopulateCandidateData(candidatesForThisElection, candidate, parties, ballot)); } liveElectionInfo.Candidates = dbCandidates; return(liveElectionInfo); } catch (Exception e) { Console.WriteLine("Probably there are no votes "); Console.WriteLine(e); return(new LiveElectionInfo()); } } if (ballot.Election.Category == ElectionCategory.Local && query.CountyId.GetValueOrDefault().IsCapitalCity() == false) { if (!ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live) { var aggregatedVotes = await RetrieveAggregatedVotes(query, ballot); liveElectionInfo.Candidates = aggregatedVotes; return(liveElectionInfo); } } var results = await GetCandidateResultsFromQueryAndBallot(query, ballot, dbContext); liveElectionInfo.Candidates = results; return(liveElectionInfo); }