public async Task <ActionResult <LiveResultsResponse> > GetResults([FromQuery] string electionId = Consts.FirstElectionRound, string source = null, string county = null, FileType fileType = FileType.Results) { try { var resultsQuery = new ResultsQuery(fileType, source, county, electionId); var key = resultsQuery.ToString(); var result = await _appCache.GetOrAddAsync( key, () => _resultsAggregator.GetElectionResults(resultsQuery), DateTimeOffset.Now.AddSeconds(_config.Value.IntervalInSeconds)); if (result.IsFailure) { _appCache.Remove(key); Log.LogWarning(result.Error); return(BadRequest(result.Error)); } var voteCountStatisticsResult = await _appCache.GetOrAddAsync( Consts.ResultsCountKey + electionId, () => _resultsAggregator.GetVoteCountStatistics(electionId), DateTimeOffset.Now.AddSeconds(_config.Value.IntervalInSeconds)); if (voteCountStatisticsResult.IsSuccess) { result.Value.TotalCountedVotes = voteCountStatisticsResult.Value.TotalCountedVotes; result.Value.PercentageCounted = voteCountStatisticsResult.Value.Percentage; } if (electionId == Consts.SecondElectionRound && string.IsNullOrWhiteSpace(source) && string.IsNullOrWhiteSpace(county)) { result.Value.PercentageCounted = 100; result.Value.CanceledVotes = 182648; if (voteCountStatisticsResult.IsSuccess) { result.Value.TotalCountedVotes = voteCountStatisticsResult.Value.TotalCountedVotes - result.Value.CanceledVotes; } result.Value.Candidates[0].Votes = 6509135; result.Value.Candidates[0].Percentage = (decimal)66.09; result.Value.Candidates[1].Votes = 3339922; result.Value.Candidates[1].Percentage = (decimal)33.91; } return(result.Value); } catch (Exception e) { Log.LogError(e, "Exception encountered while retrieving results"); throw; } }
public async Task <ActionResult <LiveResultsResponse> > GetResults([FromQuery] string electionId, string source = null, string county = null, FileType fileType = FileType.Results) { try { var resultsQuery = new ResultsQuery(fileType, source, county, electionId); var key = resultsQuery.ToString(); var result = await _appCache.GetOrAddAsync( key, () => _resultsAggregator.GetElectionResults(resultsQuery), DateTimeOffset.Now.AddSeconds(_config.Value.IntervalInSeconds)); if (result.IsFailure) { _appCache.Remove(key); _logger.LogError(result.Error); return(BadRequest(result.Error)); } return(result.Value); } catch (Exception e) { _logger.LogError(e, "Exception encountered while retrieving results"); throw; } }