コード例 #1
0
        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
        }
コード例 #2
0
        public WeatherForecast Create(CreateWeatherForecastDto dto)
        {
            var result = new WeatherForecast
            {
                Summary      = dto.Summary,
                Date         = DateTime.Now,
                TemperatureC = dto.TemperatureC
            };

            return(result);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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());
            }
        }