public async void Shoud_Create_New_Forecast() { // arrange var createRequest = new CreateWeatherForecastDto() { Date = DateTime.Today, TemperatureC = 14, Summary = "humid" }; // act var result = await controller.Create(createRequest); // assert var expected = fixture.forecastList.Last(); Assert.IsAssignableFrom <IActionResult>(result); Assert.IsType <CreatedResult>(result); // check correct return type var actual = (CreatedResult)result; Assert.Equal(expected, actual.Value); // check correct return value Assert.Equal("/" + expected.Id, actual.Location); // check correct location }
public WeatherForecast Create(CreateWeatherForecastDto dto) { var result = new WeatherForecast { Summary = dto.Summary, Date = DateTime.Now, TemperatureC = dto.TemperatureC }; return(result); }
public async Task <WeatherForecast> Create(CreateWeatherForecastDto request) { var entity = new WeatherForecast() { Date = request.Date, TemperatureC = request.TemperatureC, Summary = request.Summary }; _dbContext.WeatherForecast.Add(entity); await _dbContext.SaveChangesAsync(); return(entity); }
public async Task <IActionResult> Create(CreateWeatherForecastDto forecast) { if (!ModelState.IsValid) { return(BadRequest()); } try { var createdEntity = await _repo.Create(forecast); var uri = $"{Url?.PageLink()}/{createdEntity.Id}"; return(Created(uri, createdEntity)); } catch (Exception) { return(UnprocessableEntity()); } }