Exemplo n.º 1
0
        public async Task GetWeatherFromFavoritesList()
        {
            var list = await favoriteRepository.GetAll();

            foreach (var item in list)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=" + item.CityName + "&units=metric"
                                                                           + openWatherMapsApiKey);

                var    webResponse = (HttpWebResponse)request.GetResponse();
                var    reader      = new StreamReader(webResponse.GetResponseStream() ?? throw new InvalidOperationException());
                string s           = reader.ReadToEnd();
                var    json        = JsonConvert.DeserializeObject <WeatherJson>(s);
                var    weather     = Mapper.Map <WeatherJson, Weather>(json);
                weather.WindChill = Math.Round(33 + (0.478 + 0.237 * Math.Sqrt(weather.WindSpeed) - 0.0124 * weather.WindSpeed)
                                               * (weather.Temperature - 33)) * 100 / 100;
                foreach (var user in item.Id)
                {
                    WeatherHistory weatherHistory = Mapper.Map <Weather, WeatherHistory>(weather);
                    weatherHistory.Id   = user;
                    weatherHistory.Date = DateTime.Now;
                    await weatherHistoryRepository.Add(weatherHistory);

                    await weatherHistoryRepository.Save();
                }
            }
        }
Exemplo n.º 2
0
        public async Task AddHistory(Weather model)
        {
            WeatherHistory weatherHistory = Mapper.Map <Weather, WeatherHistory>(model);

            weatherHistory.Date = DateTime.Now;
            weatherHistory.Id   = authRepository.GetUserId();
            await weatherHistoryRepository.Add(weatherHistory);
        }