public bool AddCity(string userId, string city) { this.logger.LogTrace($"TRACE - {DateTime.Now} - Entered AddCity method"); var info = this.service.GetWeatherInfo(city); if (info != null) { var user = this.users.Get(u => u.Id == userId, c => c.CityWeatherInfos); if (this.cities.GetAll().Any(c => c.Name == city)) { var item = this.cities.Get(city); WeatherParser.Update(info, item); if (user.CityWeatherInfos.Any(c => c.Name == info.Name)) { this.logger.LogDebug($"DEBUG - {DateTime.Now} - Edited info in {user.UserName}'s collection"); this.cities.Update(item); } else { this.logger.LogDebug($"DEBUG - {DateTime.Now} - Added updated info to {user.UserName}'s collection"); user.CityWeatherInfos.Add(item); this.users.Update(user); return(true); } } else { this.logger.LogDebug($"DEBUG - {DateTime.Now} - Added new info to {user.UserName}'s collection"); user.CityWeatherInfos.Add(info); this.users.Update(user); } this.logger.LogInformation($"INFO - {DateTime.Now} - Added info to {user.UserName}'s collection"); this.logger.LogTrace($"TRACE - {DateTime.Now} - Ended AddCity method"); return(true); } return(false); }
public CityWeatherInfo GetWeatherInfo(string city) { var lang = CultureInfo.CurrentCulture.Name; this.logger.LogTrace($"TRACE - {DateTime.Now} - Entered GetWeatherInfo method"); if (!this.cache.TryGetValue(city + lang, out CityWeatherInfo result)) { HttpClient client = new HttpClient { BaseAddress = new Uri(this.options.Baseurl + $"{city}&units={this.options.Units}&lang={lang}&appid={this.options.Appid}"), Timeout = TimeSpan.FromSeconds(5) }; var response = client.GetAsync(client.BaseAddress).Result; response.EnsureSuccessStatusCode(); var content = response.Content.ReadAsStringAsync().Result; var weather = WeatherParser.Parse(JObject.Parse(content)); if (string.Equals(weather.Name, city, StringComparison.OrdinalIgnoreCase)) { this.logger.LogDebug($"DEBUG - {DateTime.Now} - Received info about {city} from WeatherAPI"); result = weather; this.SaveWeatherInfo(result); this.logger.LogTrace($"TRACE - {DateTime.Now} - Ended GetWeatherInfo method"); return(result); } this.logger.LogDebug($"DEBUG - {DateTime.Now} - No info about {city} was found with WeatherAPI"); this.logger.LogTrace($"TRACE - {DateTime.Now} - Ended GetWeatherInfo method"); return(null); } this.logger.LogDebug($"DEBUG - {DateTime.Now} - Returned info about {city} from cache"); this.logger.LogTrace($"TRACE - {DateTime.Now} - Ended GetWeatherInfo method"); return(result); }