public IActionResult CreateCar([FromBody] CarForCreationDto car) { try { if (car == null) { return(BadRequest("Car object is null")); } if (!ModelState.IsValid) { Console.WriteLine(BadRequest("Invalid model object")); return(BadRequest("Invalid model object")); } var carEntity = _mapper.Map <Car>(car); _repository.Car.CreateCar(carEntity); _repository.Save(); var createdCar = _mapper.Map <CarDto>(carEntity); Console.WriteLine(carEntity + " " + createdCar); return(CreatedAtRoute("CarById", new { id = createdCar.Id }, createdCar)); } catch (Exception ex) { return(StatusCode(500, "Internal server error")); } }
public ActionResult <Car> CreateCar(CarForCreationDto car) { var carEntity = _mapper.Map <Car>(car); _carRentalRepository.AddCar(carEntity); _carRentalRepository.Save(); return(CreatedAtRoute("GetCar", new { carId = carEntity.Id }, carEntity)); }
public async Task <IActionResult> CreateCar([FromBody] CarForCreationDto carFromBody, string userId) { if (!_userService.UserExists(userId)) { return(NotFound()); } var carToRetrun = _carService.AddCar(carFromBody, userId); await _linksService.AddLinksAsync(carToRetrun); return(CreatedAtRoute(Constants.RouteNames.GetCar, new { userId = userId, id = carToRetrun.Id }, carToRetrun)); }
public CarDto AddCar(CarForCreationDto car, string userId) { car.UserId = userId; var carToSave = Mapper.Map <Car>(car); _carRepository.Add(carToSave); if (!_carRepository.Save()) { throw new Exception("Could not save car"); } return(Mapper.Map <CarDto>(carToSave)); }
public IActionResult CreateCar([FromBody] CarForCreationDto car) { if (car == null) { return(BadRequest()); } var carForCreation = Mapper.Map <Car>(car); _carRepository.AddCar(carForCreation); if (!_carRepository.Save()) { throw new Exception(" Saving error in Database"); } var carToReturn = Mapper.Map <CarDto>(carForCreation); //return Ok(carToReturn); return(CreatedAtRoute("GetCar", new { id = carForCreation.Id }, carToReturn)); }
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()); } }