Пример #1
0
        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));
        }