public IActionResult GetCar(Guid id) { if (!_carRepository.CarExists(id)) { return(NotFound()); } var carFromRepo = _carRepository.GetCar(id); CarDto carToReturn = Mapper.Map <CarDto>(carFromRepo); return(Ok(carFromRepo)); }
public IActionResult CreateBooking(Guid carId, [FromBody] BookingForCreationDto booking) { if (booking == null) { return(BadRequest()); } if (!_carRepository.CarExists(carId)) { return(NotFound()); } var isCarAvailable = _carRepository.CarAvailable(carId); if (!isCarAvailable) { return(BadRequest($"The car is not available.")); } var bookingEntity = _mapper.Map <Booking>(booking); _carRepository.AddBooking(carId, bookingEntity); if (!_carRepository.Save()) { throw new Exception($"Creating a booking for car {carId} failed on save."); } var bookingToReturn = _mapper.Map <BookingDto>(bookingEntity); return(CreatedAtRoute("GetBooking", new { bookingId = bookingToReturn.Id }, bookingToReturn)); }
public IActionResult Add([FromBody] DetailDto value) { Car car = value; if (String.IsNullOrWhiteSpace(car.PremiseId)) { return(BadRequest(new ErrorModel { Message = "Premise MUST be chosen!" })); } try { if (_repo.CarExists(car)) { return(Conflict("Car already exists!")); } } catch (ArgumentNullException ex) { return(BadRequest(new ErrorModel { Message = ex.Message, CauseValue = nameof(value) })); } _repo.Add(value); _repo.Save(); return(Ok()); }
public IActionResult Create([FromBody] Car car) { if (car == null) { return(BadRequest()); } if (_carRepository.CarExists(car.VinNo)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } _carRepository.AddCar(car); if (!_carRepository.Save()) { throw new Exception("Creating a car failed on save."); } return(CreatedAtRoute("GetCar", new { id = car.VinNo }, car)); }
private bool CarExists(Guid id) { return(_carRepository.CarExists(id)); }