Ejemplo n.º 1
0
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent)
        {
            try
            {
                if (electionResultsData == null)
                {
                    electionResultsData = new ElectionResultsData();
                }
                var electionsConfig = DeserializeElectionsConfig();

                var pollingStations = await CalculateVotesByCounty(csvContent, electionsConfig);

                var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);
                electionResultsData.Candidates = StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData.Candidates, sumOfVotes);
                var counties = pollingStations.GroupBy(g => g.County).Select(c => c.Key).OrderBy(c => c).ToList();
                foreach (var candidate in electionResultsData.Candidates)
                {
                    foreach (var county in counties)
                    {
                        var votesForCandidate = pollingStations.Where(p => p.County == county)
                                                .Sum(p => p.Candidates.FirstOrDefault(c => c.Id == candidate.Id)?.Votes ?? 0);
                        candidate.Counties[county] = votesForCandidate;
                    }
                }

                return(Result.Ok(electionResultsData));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Result.Failure <ElectionResultsData>(e.Message));
            }
        }
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent, ElectionResultsFile file)
        {
            if (electionResultsData == null)
            {
                electionResultsData = new ElectionResultsData();
            }
            var electionsConfigResponse = _electionConfigurationSource.GetElectionById(file.ElectionId);

            if (electionsConfigResponse.IsFailure)
            {
                return(Result.Failure <ElectionResultsData>(electionsConfigResponse.Error));
            }

            var electionsConfig = electionsConfigResponse.Value;

            electionResultsData.Candidates = electionsConfig.Candidates.Select(c => new CandidateConfig
            {
                Id       = c.CsvId,
                ImageUrl = c.ImageUrl,
                Name     = c.Name
            }).ToList();

            await PopulateCandidatesListWithVotes(csvContent, electionResultsData, file);

            var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);

            electionResultsData.Candidates = StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData.Candidates, sumOfVotes);

            return(Result.Ok(electionResultsData));
        }
Ejemplo n.º 3
0
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent)
        {
            if (electionResultsData == null)
            {
                electionResultsData = new ElectionResultsData();
            }
            Candidates = Config.Candidates;

            await PopulateCandidatesListWithVotes(csvContent);

            electionResultsData.Candidates = Candidates;
            var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);

            StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData, sumOfVotes);

            return(Result.Ok(electionResultsData));
        }
Ejemplo n.º 4
0
        public async Task <Result <ElectionResultsData> > Parse(ElectionResultsData electionResultsData, string csvContent)
        {
            if (electionResultsData == null)
            {
                electionResultsData = new ElectionResultsData();
            }
            var electionsConfig = DeserializeElectionsConfig();

            electionResultsData.Candidates = electionsConfig.Candidates.Select(c => new CandidateStatistics
            {
                Id       = c.CsvId,
                ImageUrl = c.ImageUrl,
                Name     = c.Name
            }).ToList();
            await PopulateCandidatesListWithVotes(csvContent, electionResultsData.Candidates);

            var sumOfVotes = electionResultsData.Candidates.Sum(c => c.Votes);

            StatisticsAggregator.CalculatePercentagesForCandidates(electionResultsData, sumOfVotes);

            return(Result.Ok(electionResultsData));
        }