Пример #1
0
        public IActionResult GetCountries()
        {
            var result = _covidHandler.GetCovidData <List <Country> >(RequestType.Countries);

            using (CovidStatCruncherContext context = new CovidStatCruncherContext())
            {
                foreach (var country in result.Result)
                {
                    var countryToAdd = new Countries
                    {
                        CountryName = country.Name,
                        CountrySlug = country.Slug,
                        Iso2        = country.ISO2,
                        HasData     = false
                    };

                    context.Countries.Add(countryToAdd);
                }

                context.SaveChanges();
            }


            return(Ok(result.Result));
        }
Пример #2
0
        public async Task <IActionResult> Startup()
        {
            using (CovidStatCruncherContext context = new CovidStatCruncherContext())
            {
                var countries = await context.Countries.AsNoTracking().ToListAsync();

                foreach (var country in countries)
                {
                    _logger.LogInformation($"Checking if Country:{country.CountryName} has been updated");
                    if (country.HasData == null || country.HasData == false)
                    {
                        _logger.LogInformation($"Updating Country:{country.CountryName}");
                        var results = await _covidHandler.GetCovidData <List <CountryUpdates> >(RequestType.ByCountryAllStatus, country.CountrySlug);

                        var coordResults = await _covidHandler.GetCovidData <List <LiveByCountryUpdates> >(RequestType.LiveByCountryAllStatus, country.CountrySlug);

                        var         coordResultsToUse = coordResults.FirstOrDefault();
                        bool        hasCoords         = coordResultsToUse != null;
                        CountryData updateToAdd       = new CountryData();
                        foreach (var result in results)
                        {
                            if (hasCoords)
                            {
                                _logger.LogInformation($"Country:{country.CountryName}, has valid coords");
                                updateToAdd = new CountryData
                                {
                                    CountryName    = result.Country,
                                    CountryId      = country.CountryId,
                                    City           = result.City,
                                    CountryCode    = coordResultsToUse.CountryCode ?? result.CountryCode,
                                    Province       = result.Province,
                                    Latitude       = coordResultsToUse.Lat ?? result.Latitude.ToString(),
                                    Longitude      = coordResultsToUse.Lon ?? result.Longitude.ToString(),
                                    ActiveCases    = result.Active,
                                    Deaths         = result.Deaths,
                                    Recovered      = result.Recovered,
                                    ConfirmedCases = result.Confirmed,
                                    DateTime       = result.Date
                                };
                            }
                            else
                            {
                                _logger.LogInformation($"Country:{country.CountryName}, does not have valid coords");

                                updateToAdd = new CountryData
                                {
                                    CountryName    = result.Country,
                                    CountryId      = country.CountryId,
                                    City           = result.City,
                                    CountryCode    = result.CountryCode,
                                    Province       = result.Province,
                                    Latitude       = result.Latitude.ToString(),
                                    Longitude      = result.Longitude.ToString(),
                                    ActiveCases    = result.Active,
                                    Deaths         = result.Deaths,
                                    Recovered      = result.Recovered,
                                    ConfirmedCases = result.Confirmed,
                                    DateTime       = result.Date
                                };
                            }

                            //Add the Country Updates one by one.
                            context.CountryData.Add(updateToAdd);
                            context.SaveChanges();

                            //Change the hasData value in the DB for that country.
                            country.HasData = true;
                            context.Countries.Update(country);

                            //context.ChangeTracker.AcceptAllChanges();
                        }
                    }
                }
            }

            return(Ok("All G Homie"));
        }