Ejemplo n.º 1
0
        public async Task <Result <LiveElectionInfo> > Import(string url, CsvIndexes csvIndexes)
        {
            var electionInfo = await GetCandidatesFromUrl(url, csvIndexes);

            if (electionInfo.Candidates == null)
            {
                return(Result.Failure <LiveElectionInfo>("File doesn't exist"));
            }
            return(electionInfo);
        }
Ejemplo n.º 2
0
        public async Task <LiveElectionInfo> GetCandidatesFromUrl(string url, CsvIndexes csvIndexes)
        {
            try
            {
                var stream = await _fileDownloader.Download(url);

                var liveElectionInfo = await ExtractCandidatesFromCsv(stream, csvIndexes);

                return(liveElectionInfo);
            }
            catch
            {
                return(LiveElectionInfo.Default);
            }
        }
Ejemplo n.º 3
0
        private async Task <List <CandidateResult> > GetCandidates(CsvReader csvParser, CsvIndexes csvIndexes)
        {
            await csvParser.ReadAsync();

            var candidates = new List <CandidateResult>();
            var index      = csvIndexes.CandidatesIndex;
            await csvIndexes.Map(csvParser);

            while (true)
            {
                try
                {
                    var field = csvParser.GetField(index++);
                    if (field == null)
                    {
                        return(candidates);
                    }
                    field = field.Replace("-voturi", "");
                    candidates.Add(new CandidateResult
                    {
                        Name = field
                    });
                }
                catch (Exception)
                {
                    return(candidates);
                }
            }
        }
Ejemplo n.º 4
0
        private async Task <List <PollingSection> > ExtractCandidateResultsFromCsv(Stream csvStream, CsvIndexes csvIndexes)
        {
            List <CandidateResult> candidates;
            List <PollingSection>  pollingSections = new List <PollingSection>();
            var csvContent = await ReadCsvContent(csvStream);

            TextReader sr        = new StringReader(csvContent);
            var        csvParser = new CsvReader(sr, CultureInfo.CurrentCulture);

            csvParser.Configuration.HeaderValidated   = null;
            csvParser.Configuration.MissingFieldFound = null;
            candidates = await GetCandidates(csvParser, csvIndexes);

            while (true)
            {
                var result = await csvParser.ReadAsync();

                if (!result)
                {
                    return(pollingSections);
                }
                var index          = 0;
                var pollingSection = new PollingSection
                {
                    EligibleVoters = int.Parse(csvParser.GetField(csvIndexes.EligibleVotersIndex)),
                    Voters         = int.Parse(csvParser.GetField(csvIndexes.TotalVotesIndex)),
                    NullVotes      = int.Parse(csvParser.GetField(csvIndexes.NullVotesIndex)),
                    ValidVotes     = int.Parse(csvParser.GetField(csvIndexes.ValidVotesIndex)),
                    Siruta         = int.Parse(csvParser.GetField(csvIndexes.SirutaIndex)),
                    Country        = csvParser.GetField(csvIndexes.CountryNameIndex),
                    Candidates     = JsonConvert.DeserializeObject <List <CandidateResult> >(JsonConvert.SerializeObject(candidates))
                };
                if (csvIndexes.NullVotesIndex2 != 0)
                {
                    pollingSection.NullVotes += int.Parse(csvParser.GetField(csvIndexes.NullVotesIndex2));
                }
                pollingSections.Add(pollingSection);
                for (int i = csvIndexes.CandidatesIndex; i < csvIndexes.CandidatesIndex + candidates.Count; i++)
                {
                    try
                    {
                        var votes = csvParser.GetField(i);
                        pollingSection.Candidates[index].Votes += int.Parse(votes);
                        index++;
                    }
                    catch (Exception)
                    {
                        return(pollingSections);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private async Task <LiveElectionInfo> ExtractCandidatesFromCsv(Stream csvStream, CsvIndexes csvIndexes)
        {
            List <CandidateResult> candidates;
            var csvContent = await ReadCsvContent(csvStream);

            TextReader sr        = new StringReader(csvContent);
            var        csvParser = new CsvReader(sr, CultureInfo.CurrentCulture);

            csvParser.Configuration.HeaderValidated   = null;
            csvParser.Configuration.MissingFieldFound = null;
            candidates = await GetCandidates(csvParser, csvIndexes);

            var nullVotes = 0;
            var total     = 0;
            var voted     = 0;
            var valid     = 0;
            var siruta    = 0;

            while (true)
            {
                var result = await csvParser.ReadAsync();

                if (!result)
                {
                    return new LiveElectionInfo
                           {
                               Candidates     = candidates,
                               EligibleVoters = total,
                               TotalVotes     = voted,
                               NullVotes      = nullVotes,
                               ValidVotes     = valid,
                               Siruta         = siruta
                           }
                }
                ;
                var index = 0;
                total     += int.Parse(csvParser.GetField(csvIndexes.EligibleVotersIndex));
                voted     += int.Parse(csvParser.GetField(csvIndexes.TotalVotesIndex));
                nullVotes += int.Parse(csvParser.GetField(csvIndexes.NullVotesIndex));
                if (csvIndexes.NullVotesIndex2 != 0)
                {
                    nullVotes += int.Parse(csvParser.GetField(csvIndexes.NullVotesIndex2));
                }
                valid += int.Parse(csvParser.GetField(csvIndexes.ValidVotesIndex));
                siruta = int.Parse(csvParser.GetField(csvIndexes.SirutaIndex));
                for (int i = csvIndexes.CandidatesIndex; i < csvIndexes.CandidatesIndex + candidates.Count; i++)
                {
                    try
                    {
                        var votes = csvParser.GetField(i);
                        candidates[index].Votes += int.Parse(votes);
                        index++;
                    }
                    catch (Exception)
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private async Task <List <CandidateResult> > GetDiasporaResults(Result <string> url, Country country,
                                                                        LiveElectionInfo electionInfo, CsvIndexes csvIndexes)
        {
            List <CandidateResult> candidateResults = new List <CandidateResult>();
            var stream = await _fileDownloader.Download(url.Value);

            var pollingSections = await ExtractCandidateResultsFromCsv(stream, csvIndexes);

            var sectionsForLocality = pollingSections.Where(p => p.Country.EqualsIgnoringAccent(country.Name)).ToList();

            foreach (var pollingSection in sectionsForLocality)
            {
                electionInfo.ValidVotes += pollingSection.ValidVotes;
                electionInfo.NullVotes  += pollingSection.NullVotes;
            }

            candidateResults = sectionsForLocality.SelectMany(s => s.Candidates).ToList();
            return(candidateResults);
        }