public WeatherHistoryModel GetWeatherHistory(string city) { city = city.ToLowerInvariant(); var weatherDataManager = new WeatherDataManager(); if (weatherDataManager.IsCityInMonitorList(city)) { List <WeatherInfoEntity> weatherInfo = weatherDataManager.GetWeatherHistory(city); weatherInfo = weatherInfo.OrderByDescending(o => DateTime.Parse(o.Date)).Take(50).OrderBy(o => DateTime.Parse(o.Date)).ToList(); List <double> highTempList = new List <double>(); List <double> lowTempList = new List <double>(); List <string> dateList = new List <string>(); foreach (WeatherInfoEntity entity in weatherInfo) { highTempList.Add(entity.HighTemp); lowTempList.Add(entity.LowTemp); dateList.Add(entity.Date); } return(new WeatherHistoryModel() { City = city, HighTemp = highTempList.ToArray(), LowTemp = lowTempList.ToArray(), Date = dateList.ToArray() }); } else { //Query OpenWeather CityWeather cityWeather = new CityWeather(); Rootobject weatherInfo = cityWeather.GetWeather(city); if (weatherInfo != null) { string today = DateTime.Now.ToString("d"); weatherDataManager.UpdateCityInfoToTable(city, today); WeatherInfoEntity model = new WeatherInfoEntity(city, convertKtoF(weatherInfo.main.temp_max), convertKtoF(weatherInfo.main.temp_min), today); weatherDataManager.UpdateHistoryInfoToTable(model); return(new WeatherHistoryModel() { City = city, HighTemp = new double[] { model.HighTemp }, LowTemp = new double[] { model.LowTemp }, Date = new string[] { today } }); } else { return(null); // error } } }
private void QueryOpenWeatherAndUpdateTable(CityWeather cityWeather, WeatherDataManager weatherDataManager, string city) { Rootobject weatherInfo = cityWeather.GetWeather(city); if (weatherInfo != null) { string today = DateTime.Now.ToString("d"); // string today = "3/9/2017"; weatherDataManager.UpdateCityInfoToTable(city, today); WeatherInfoEntity model = new WeatherInfoEntity(city, convertKtoF(weatherInfo.main.temp_max), convertKtoF(weatherInfo.main.temp_min), today); weatherDataManager.UpdateHistoryInfoToTable(model); } }