Ejemplo n.º 1
0
        public IHttpActionResult CreateCar([FromBody] CarForCreationDto carFromBody, int userId)
        {
            try
            {
                if (carFromBody == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!_userRepository.UserExists(userId))
                {
                    return(NotFound());
                }

                var car = Mapper.Map <Car>(carFromBody);


                _carRepository.AddCarForUser(userId, car);


                if (!_appRepository.Commit())
                {
                    return(InternalServerError());
                }

                var carToReturn = Mapper.Map <CarDto>(car);

                return(CreatedAtRoute("GetCar", new { id = carToReturn.Id }, carToReturn));
            }
            catch (Exception e)
            {
                return(InternalServerError());
            }
        }