예제 #1
0
        public HistoricalWeatherDto GetActualWeatherMap(string city, string country = "")
        {
            try
            {
                var uri        = _config.GetValue <string>("ApiUrl");
                var parameters = String.Format("weather?q={0},{1}&appid={2}&units=metric", city, country, _config.GetValue <string>("ApiKey"));
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(uri);
                    var responseTask = client.GetAsync(parameters);
                    responseTask.Wait();

                    var result = responseTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var readTask = result.Content.ReadAsStringAsync();
                        readTask.Wait();

                        dynamic response      = JObject.Parse(readTask.Result);
                        var     weatherMapDto = new HistoricalWeatherDto
                        {
                            ThermalSensation = response.main.feels_like,
                            Temperature      = response.main.temp
                        };
                        return(weatherMapDto);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error al consultar con la api");
            }
            return(null);
        }
예제 #2
0
 public bool AddHistoricalWeather(HistoricalWeatherDto historicalWeatherDto)
 {
     try
     {
         //var historicalWeather = _mapper.Map<HistoricalWeather>(historicalWeatherDto);
         var historicalWeather = new HistoricalWeather
         {
             ThermalSensation = historicalWeatherDto.ThermalSensation,
             Temperature      = historicalWeatherDto.Temperature,
             City             = _context.City.FirstOrDefault(x => x.Id == historicalWeatherDto.City.Id),
             Country          = _context.Country.FirstOrDefault(x => x.Id == historicalWeatherDto.Country.Id),
         };
         _context.Add <HistoricalWeather>(historicalWeather);
         _context.SaveChanges();
     }
     catch (Exception e)
     {
         Console.WriteLine("Error al guardar el historico");
         return(false);
     }
     return(true);
 }