public void FlightBooking_AddNew() { FlightBookingRepository repository = new FlightBookingRepository(dbConnectionString); var rndDays = new Random().Next(1, 30); FlightBooking flightBooking = new FlightBooking { TravelDate = DateTime.Now.Date.AddDays(rndDays), FlightId = 8, PassengerCount = 2 }; var result = repository.Add(flightBooking); Assert.AreEqual(1, result); }
public IActionResult Book([FromBody] Booking bookingRequest) { if (!(bookingRequest.PassengerCount > 0)) { return(BadRequest("At least 1 passenger is required.")); } Flight flight = flightRepository.Find(bookingRequest.FlightId); if (flight == null || flight.PassengerCapacity <= 0) { return(BadRequest("Invalid Flight.")); } Passenger passenger = passengerRepository.Find(bookingRequest.PassengerId); if (passenger == null || string.IsNullOrWhiteSpace(passenger.IdentityNumber)) { return(BadRequest("Invalid Passenger.")); } if (bookingRequest.PassengerCount > flight.PassengerCapacity) { return(BadRequest("Passenger Capacity exceeded.")); } Booking booking = bookingRepository.Find(bookingRequest.TravelDate, bookingRequest.FlightId, bookingRequest.PassengerId); if (booking != null && booking.PassengerCount > 0) { return(BadRequest("Flight already booked for the same travel day by the same passenger.")); } else { // Valid booking request, so proceed. bookingRepository.Add(bookingRequest); } FlightBooking flightBooking = flightBookingRepository.Find(bookingRequest.FlightId, bookingRequest.TravelDate); if (flightBooking != null && flightBooking.FlightBookingId > 0) { var currentPassengerCount = flightBooking.PassengerCount; if ((currentPassengerCount + bookingRequest.PassengerCount) > flight.PassengerCapacity) { return(BadRequest("Passenger Capacity exceeded.")); } else { // Valid passenger count. flightBooking.PassengerCount = (short)(currentPassengerCount + bookingRequest.PassengerCount); flightBookingRepository.Update(flightBooking); } } if (flightBooking == null || flightBooking.FlightBookingId <= 0) { flightBooking = new FlightBooking { TravelDate = bookingRequest.TravelDate, FlightId = bookingRequest.FlightId, PassengerCount = bookingRequest.PassengerCount }; // Valid booking request, so proceed. flightBookingRepository.Add(flightBooking); } return(Ok("Flight Booked.")); }