Beispiel #1
0
        public async Task<IHttpActionResult> PostCar(CarDto car)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var carParamObject = Mapper.DynamicMap<CarParamObject>(car);
            var addedCar = await carsService.AddCar<CarDto>(carParamObject);

            return CreatedAtRoute("DefaultApi", new { id = addedCar.Id }, addedCar);
        }
        public void WhenAddingAnInvalidCarItShouldReturnABadRequestResponse()
        {
            var newCarDto = new CarDto
            {
                Name = string.Empty,
                Owner = "NewOwner"
            };

            IHttpActionResult result = null;

            Action act = () => { result = carsController.PostCar(newCarDto).Result; };

            act.ShouldThrow<AggregateException>().WithInnerException<ArgumentNullException>();
            context.Cars.Count().Should().Be(2);
        }
        public void WhenAddingACarItShouldAddItAndReturnIt()
        {
            var newCarDto = new CarDto
            {
                Name = "NewCar",
                Owner = "NewOwner"
            };

            var result = this.carsController.PostCar(newCarDto).Result as CreatedAtRouteNegotiatedContentResult<CarDto>;

            context.Cars.Count().Should().Be(3);
            result.Content.Id.Should().Be(3);
            result.Content.Name.Should().Be(newCarDto.Name);
            result.Content.Owner.Should().Be(newCarDto.Owner);
        }
Beispiel #4
0
        public async Task<IHttpActionResult> PutCar(int id, CarDto car)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != car.Id)
            {
                return BadRequest();
            }

            var carParamObject = Mapper.DynamicMap<CarParamObject>(car);
            var modifiedCar = await carsService.UpdateCar<CarDto>(carParamObject);
            if (modifiedCar == null) return NotFound();
            return StatusCode(HttpStatusCode.NoContent);
        }