public bool GetUpdatedCountryData() { _logger.LogInformation("Checking database for list of countries to update"); using (var scope = _serviceScopeFactory.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <CovidStatCruncherContext>(); var countriesToCheck = context.Countries .Where(country => country.HasData == true) .ToListAsync(); _logger.LogInformation($"ReturnCountries:{countriesToCheck.Result.Count}"); foreach (var country in countriesToCheck.Result) { var lastUpdatedDate = context.CountryData .Where(c => c.CountryName == country.CountryName) .OrderByDescending(c => c.DateTime) .FirstOrDefault(); if (lastUpdatedDate.DateTime >= DateTime.Today.Date.Subtract(TimeSpan.FromDays(2))) { _logger.LogInformation($"Country: {country.CountryName} is all ready up to date"); continue; } _logger.LogDebug($"Fetching new updates for {country.CountryName}"); var newUpdates = _covidHandler.GetCovidData <List <CountryUpdates> >(RequestType.ByCountryAllStatusRange, country.CountrySlug, lastUpdatedDate.DateTime.AddDays(1)); _logger.LogInformation($"Country:{country.CountryName},NumberOfUpdates:{newUpdates.Result.Count}"); foreach (var mappedUpdate in newUpdates.Result.Select(updateToMap => new CountryData { CountryName = updateToMap.Country, CountryId = country.CountryId, City = updateToMap.City, CountryCode = country.Iso2, Province = updateToMap.Province, Latitude = updateToMap.Latitude.ToString(), Longitude = updateToMap.Longitude.ToString(), ActiveCases = updateToMap.Active, Deaths = updateToMap.Deaths, Recovered = updateToMap.Recovered, ConfirmedCases = updateToMap.Confirmed, DateTime = updateToMap.Date })) { context.CountryData.Add(mappedUpdate); context.SaveChanges(); } _logger.LogInformation($"Country:{country.CountryName}, UpdateStatus:Complete, NumberOfSavedUpdates{newUpdates.Result.Count}"); } } return(true); }
public async Task <bool> GetCountries() { try { _logger.LogInformation("Fetching Country Data from the Covid19Api"); var result = await _covidHandler.GetCovidData <List <Country> >(RequestType.Countries); _logger.LogInformation($"Returned {result.Count} countries"); using (var scope = _serviceScopeFactory.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <CovidStatCruncherContext>(); { foreach (var country in result) { var countryToAdd = new Countries { CountryName = country.Name, CountrySlug = country.Slug, Iso2 = country.ISO2, HasData = false }; _logger.LogInformation($"Added {countryToAdd.CountryName} to the context"); context.Countries.Add(countryToAdd); } _logger.LogInformation($"Storing {context.Countries.Count()} Countries"); context.SaveChanges(); } } } catch (Exception exc) { _logger.LogError($"Error fetching and storing startup country data. {exc.Message}"); } return(true); }
public IActionResult Get(RequestType requestType) { var result = _covidHandler.GetCovidData <Summary>(requestType); return(Ok(result.Result)); }