public List <Forecast> FindForecast(string country, string county, string city) { var nextUpdate = DateTime.Now.AddHours(1); // check of location for forecast GET request exists. var location = this._repository.QueryLocations() .Where(l => l.Country == country) .Where(l => l.County == county) .Where(l => l.City == city) .ToList(); var locationId = location.FirstOrDefault().LocationId; this._repository = new WeatherAppRepository(); // first, try get forecast from DB var forecast = this._repository.QueryForecasts() .Where(f => f.LocationId == locationId) .ToList(); // if no forecast or forecast in DB is old if (forecast != null && (forecast.Count() == 0 || forecast.Any(f => f.NextUpdate < DateTime.Now))) { forecast.ForEach(f => this._repository.Delete(f)); forecast.Clear(); } // if no forecast in DB, get new forecast and insert into DB if (forecast == null || forecast.Count() == 0) { var webService = new YrNoWebservice(); forecast = webService.FindForecast(country, county, city); foreach (var f in forecast) { f.NextUpdate = nextUpdate; f.LocationId = locationId; this._repository.Add(f); } //forecast.ForEach(f => this._repository.Add(f)); this._repository.Save(); } return(forecast); }
public WeatherAppService(IWeatherAppRepository repository, IWeatherWebservice webservice) { _repository = repository; _webservice = webservice; }
public WeatherAppService(IWeatherAppRepository repository) { this._repository = repository; }