public void Delete([FromRoute] int id) { WeatherDataEntry dataEntry = _context.WeatherDataEntries.Find(id); _context.WeatherDataEntries.Remove(dataEntry); _context.SaveChanges(); }
public void Post([FromBody] dynamic value) { WeatherDataEntry dataEntry = (WeatherDataEntry)JsonConvert.DeserializeObject(value.ToString(), typeof(WeatherDataEntry)); _context.WeatherDataEntries.Add(dataEntry); _context.SaveChanges(); }
public async Task <IActionResult> GetAndSaveData() { WeatherDataEntry dataEntry = await ExternalDataProvider.GetCurrentWeatherData(); await HistoryAPIDataProvider.SaveDataInHistory(dataEntry); return(View(dataEntry)); }
public static async Task <WeatherDataEntry> GetCurrentWeatherData() { string path = "weather?id=" + CityId + "&APPID=" + APIkey; HttpResponseMessage responseMessage = await client.GetAsync(path); if (responseMessage.IsSuccessStatusCode) { var respone = await responseMessage.Content.ReadAsStringAsync(); JObject jsonResponse = JObject.Parse(respone); WeatherDataEntry dataEntry = new WeatherDataEntry(); dataEntry.MeasurementDate = UnixTimeStampToDateTime((double)jsonResponse["dt"]); dataEntry.Sunrise = UnixTimeStampToDateTime((double)jsonResponse["sys"]["sunrise"]); dataEntry.Sunset = UnixTimeStampToDateTime((double)jsonResponse["sys"]["sunset"]); dataEntry.CityId = (int)jsonResponse["id"]; dataEntry.CityName = (string)jsonResponse["name"]; dataEntry.MainWeather = (string)jsonResponse["weather"][0]["main"]; dataEntry.WeatherDescription = (string)jsonResponse["weather"][0]["description"]; dataEntry.Temperature = ((double)jsonResponse["main"]["temp"]).KelvinToCelsius(); dataEntry.Pressure = (int)jsonResponse["main"]["pressure"]; dataEntry.Humidity = (int)jsonResponse["main"]["humidity"]; dataEntry.MinTemperature = ((double)jsonResponse["main"]["temp_min"]).KelvinToCelsius(); dataEntry.MaxTemperature = ((double)jsonResponse["main"]["temp_max"]).KelvinToCelsius(); dataEntry.WindSpeed = (double)jsonResponse["wind"]["speed"]; dataEntry.Cloudiness = (int)jsonResponse["clouds"]["all"]; dataEntry.RainVolume = (int?)jsonResponse["rain"]?["rain.3h"] ?? null; dataEntry.SnowVolume = (int?)jsonResponse["snow"]?["snow.3h"]; return(dataEntry); } else { throw new Exception(); } }
public static async Task SaveDataInHistory(WeatherDataEntry dataEntry) { string jsonInString = JsonConvert.SerializeObject(dataEntry); await client.PostAsync("http://localhost:60165/api/values", new StringContent(jsonInString, Encoding.UTF8, "application/json")); }