public override IEnumerable <Forecast> GetForecast(City city)
        {
            var forecast = _repository.FindForecast(city.CityID);

            if (forecast.Count() == 0)
            {
                forecast = _yrWebservice.GetForecast(city);
                _repository.AddForecast(forecast);
                _repository.Save();
            }
            else
            {
                foreach (Forecast item in forecast)
                {
                    if (item.NextUpdate < DateTime.Now)
                    {
                        _repository.DeleteForecast(forecast);
                        _repository.Save();

                        forecast = _yrWebservice.GetForecast(city);

                        _repository.AddForecast(forecast);
                        _repository.Save();
                        break;
                    }
                }
            }
            return(forecast);
        }
        public override IEnumerable <Forecast> RefreshForecast(string cityId, string lat, string lon)
        {
            IEnumerable <Forecast> forcasts;

            try
            {
                // Try to find forcast if geonameid exists!
                // READ in qrud. KRAV!
                forcasts = _weatherRepository.FindForecastsByGeonameID(cityId);

                //No hit in db. Exception is thrown and Get forcst from openweather api instead.
                forcasts.First();
            }
            catch
            {
                //No existing forecasts in db for geonamId.
                //Instead we request from OpenWeatherMap.
                //We save this into db.
                forcasts = new OpenWeatherMapWebservice().Get5DaysForecastByCityId(int.Parse(cityId), lat, lon);

                foreach (var forcast in forcasts)
                {
                    try
                    {
                        _weatherRepository.AddForecast(forcast);
                        _weatherRepository.Save();
                    }
                    catch (DbEntityValidationException)
                    {
                        //Ignore adding...
                        //Forecast data not valid.
                        _weatherRepository.RemoveForecast(forcast);
                    }
                }
            }
            try
            {
                _weatherRepository.FindForecastsByGeonameID(cityId).First();
                return(_weatherRepository.FindForecastsByGeonameID(cityId));
            }
            catch
            {
                //No hit!
                //For user we presents response from openweathermap directly.
                //But Forcasts is not saved into db!
                return(forcasts);
            }
        }
예제 #3
0
        public override void RefreshForecast(Location location)
        {
            // If there are no weather data for location, or if it is time to update data.
            if (!location.Forecasts.Any() || location.NextUpdate < DateTime.Now)
            {
                // ... delete old data (if there is any)
                foreach (var forecast in location.Forecasts.ToList())
                {
                    _repository.RemoveForecast(forecast.ForecastID);
                }

                // then get forecasts from web service and insert them.
                foreach (var forecast in _webservice.GetLocationForecasts(location))
                {
                    _repository.AddForecast(forecast);
                }

                // Cache data for 10 minutes according to yr api guidelines.
                location.NextUpdate = DateTime.Now.AddMinutes(10);

                _repository.Save();
            }
        }
예제 #4
0
        public async Task <IActionResult> AddForecast(WeatherForecast newforecast)
        {
            await _repo.AddForecast(newforecast);

            return(Ok());
        }