public void SaveNullShouldNotTryAddToDatabase() { //Arrange WeatherMain expecWeatherMain = new WeatherMain() { Humidity = 0, Pressure = 0, Temperature = 0, TemperatureMin = 0, TemperatureMax = 0, }; Wind expectedWind = new Wind() { Direction = 0, Speed = 0 }; PredictionDate expectedPredictionDate = new PredictionDate() { Time = DateTime.Now, }; Clouds expectedClouds = new Clouds() { All = 0 }; Forecast expectedForecast = new Forecast() { Clouds = expectedClouds, WeatherMain = expecWeatherMain, Wind = expectedWind, Time = expectedPredictionDate, CityId = 0 }; ForecastEntity expectedEntity = new ForecastEntity() { WeatherMain = expecWeatherMain, Wind = expectedWind, PredictionDate = expectedPredictionDate, Clouds = expectedClouds, CityServiceId = 0, Forecast = expectedForecast }; var weatherContextMock = new Mock <WeatherDataContext>(); weatherContextMock.Setup(x => x.Forecast.Add(It.IsAny <Forecast>())).Returns((Forecast f) => f); weatherContextMock.Setup(x => x.City.Add(It.IsAny <City>())).Returns((City f) => f); weatherContextMock.Setup(x => x.Clouds.Add(It.IsAny <Clouds>())).Returns((Clouds f) => f); weatherContextMock.Setup(x => x.PredictionDate.Add(It.IsAny <PredictionDate>())).Returns((PredictionDate f) => f); weatherContextMock.Setup(x => x.WeatherMain.Add(It.IsAny <WeatherMain>())).Returns((WeatherMain f) => f); weatherContextMock.Setup(x => x.Wind.Add(It.IsAny <Wind>())).Returns((Wind f) => f); WeatherManager weather = new WeatherManager(weatherContextMock.Object); //Act var savedEntities = weather.SaveForecastEntity(expectedEntity).Result; //Assert Assert.Equal(1, savedEntities); weatherContextMock.Verify(x => x.SaveChangesAsync(), Times.Never); }
public async Task <int> SaveForecastEntity(ForecastEntity forecast) { var queryExists = dataContext.Forecast.Where(x => x.City.ServiceId == forecast.CityServiceId).Where(x => x.Time.Time == forecast.PredictionDate.Time); if (queryExists.Count() != 0) { return(0); } dataContext.Clouds.Add(forecast.Clouds); dataContext.PredictionDate.Add(forecast.PredictionDate); dataContext.WeatherMain.Add(forecast.WeatherMain); dataContext.Wind.Add(forecast.Wind); if (forecast.Forecast == null) { forecast.Forecast = new Forecast { Clouds = forecast.Clouds, Time = forecast.PredictionDate, WeatherMain = forecast.WeatherMain, Wind = forecast.Wind }; } forecast.Forecast.CityId = dataContext.City.Single(u => u.ServiceId == forecast.CityServiceId).Id; dataContext.Forecast.Add(forecast.Forecast); return(await dataContext.SaveChangesAsync()); }
public async Task AddForecastAsync(ForecastEntity forecast) { await Task.Run(() => { _uow.Repository <ForecastEntity>().Create(forecast); _uow.Save(); }); }
private void SaveForecast(IDbRepository dbRepository) { var entity = new ForecastEntity() { Temperature = CurrentWeather.Model.main.temp, Pressure = CurrentWeather.Model.main.pressure }; dbRepository.Add(entity); }
public ForecastEntity ConvertEntity(Forecast forecast) { Clouds clouds = forecast.Clouds; PredictionDate predictionDate = forecast.Time; WeatherMain weatherMain = forecast.WeatherMain; Wind wind = forecast.Wind; ForecastEntity forecastEntity = new ForecastEntity { Clouds = clouds, PredictionDate = predictionDate, WeatherMain = weatherMain, Wind = wind }; return(forecastEntity); }
public ForecastEntity GetForecastEntity(City city, DateTime date) { DateTime preciseTime = GetCLosestForecastTime(date); var forecast = dataContext.Forecast.Where(f => f.Time.Time == preciseTime).Where(c => c.City.ServiceId == city.ServiceId).First(); Clouds clouds = forecast.Clouds; PredictionDate predictionDate = forecast.Time; WeatherMain weatherMain = forecast.WeatherMain; Wind wind = forecast.Wind; ForecastEntity forecastEntity = new ForecastEntity { Clouds = clouds, PredictionDate = predictionDate, WeatherMain = weatherMain, Wind = wind }; return(forecastEntity); }
public ForecastEntity ConvertToEntity(ForecastObject obj) { var entity = new ForecastEntity(); var temp = obj.list[0]; entity.City = obj.City.Name; entity.Description = temp.Weather[0].Description; entity.Humidity = temp.Humidity; entity.Icon = temp.Weather[0].Icon; entity.Pressure = temp.Pressure; entity.TemperatureDay = temp.Temp.Day; entity.TemperatureEvening = temp.Temp.Eve; entity.TemperatureMorning = temp.Temp.Morn; entity.TemperatureNight = temp.Temp.Night; entity.WindSpeed = temp.Speed; entity.Date = DateTime.Now; return(entity); }
private List <ForecastEntity> ConvertEntities(IQueryable <Forecast> forecast) { List <ForecastEntity> forecasts = new List <ForecastEntity>(); foreach (var item in forecast) { Clouds clouds = item.Clouds; PredictionDate predictionDate = item.Time; WeatherMain weatherMain = item.WeatherMain; Wind wind = item.Wind; ForecastEntity forecastEntity = new ForecastEntity { Clouds = clouds, PredictionDate = predictionDate, WeatherMain = weatherMain, Wind = wind }; forecasts.Add(forecastEntity); } return(forecasts); }
public async Task <ActionResult> Index(string city, int days = 1) { ViewBag.Cities = _cities; if (string.IsNullOrEmpty(city)) { return(View()); } try { ForecastObject forecast = await _forecastProvider.GetForecastAsync(city, days); ForecastEntity entity = _converter.ConvertToEntity(forecast); await _weatherService.AddForecastAsync(entity); return(View(forecast)); } catch (WebException ex) { ViewBag.ErrorMessage = $"Weather forecast for city '{city}' not found."; return(View()); } }
/// <summary> /// Converts a DTO object to entity. /// </summary> /// <param name="forecastDTO">Model converted from JSON string.</param> /// <returns>List of forecast entites to store in a database.</returns> public List <ForecastEntity> ConvertModel(Forecast forecastDTO) { List <ForecastEntity> entities = new List <ForecastEntity>(); ForecastEntity entity = new ForecastEntity(); foreach (var weather in forecastDTO.List) { entity = new ForecastEntity { CityServiceId = (int)forecastDTO.City.Id, Clouds = new Common.Models.Clouds() }; entity.Clouds.All = weather.Clouds.All; entity.PredictionDate = new PredictionDate { Time = weather.DtTxt.DateTime }; entity.WeatherMain = new WeatherMain { Humidity = (int)weather.Main.Humidity, Pressure = (int)weather.Main.Pressure, Temperature = weather.Main.Temp - KelvinToCelciusDifference, TemperatureMax = weather.Main.TempMax - KelvinToCelciusDifference, TemperatureMin = weather.Main.TempMin - KelvinToCelciusDifference }; entity.Wind = new Common.Models.Wind { Direction = weather.Wind.Deg, Speed = weather.Wind.Speed }; entities.Add(entity); } return(entities); }