コード例 #1
0
        public void FlightBooking_FindByDate()
        {
            FlightBookingRepository repository = new FlightBookingRepository(dbConnectionString);

            var result = repository.Find(DateTime.Parse("2018-8-8"), DateTime.Parse("2018-8-12"));

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ToList().Count > 0);
        }
コード例 #2
0
        public void FlightBooking_FindByFlightAndDate()
        {
            FlightBookingRepository repository = new FlightBookingRepository(dbConnectionString);

            var result = repository.Find(1, DateTime.Parse("2018-8-8"));

            Assert.IsNotNull(result);
            Assert.IsTrue(result.FlightBookingId > 0);
        }
コード例 #3
0
        public IActionResult CheckAvailability([FromBody] AvailabilityRequest availabilityRequest)
        {
            if (availabilityRequest.StartDate > availabilityRequest.EndDate)
            {
                return(BadRequest("Invalid date range."));
            }

            var bookingList = flightBookingRepository.Find(availabilityRequest.StartDate, availabilityRequest.EndDate);

            if (bookingList == null || bookingList.ToList().Count <= 0)
            {
                return(BadRequest("No Flights Available."));
            }

            var flightList = flightRepository.GetAll();

            if (flightList == null || flightList.ToList().Count <= 0)
            {
                return(BadRequest("No Flights Available."));
            }

            IList <AvailabilityResponseDTO> availabilityResponse = new List <AvailabilityResponseDTO>();

            foreach (FlightBooking booking in bookingList)
            {
                Flight flight = flightList.First(f => f.Id == booking.FlightId);
                if ((flight.PassengerCapacity - booking.PassengerCount) <= 0)
                {
                    continue;
                }

                var availability = new AvailabilityResponseDTO
                {
                    FlightNumber   = flight.Number,
                    FlightName     = flight.Name,
                    TravelDate     = booking.TravelDate,
                    DepartureCity  = flight.DepartureCity,
                    DepartureTime  = flight.DepartureTime,
                    ArrivalCity    = flight.ArrivalCity,
                    ArrivalTime    = flight.ArrivalTime,
                    AvailableSeats = (short)(flight.PassengerCapacity - booking.PassengerCount)
                };

                availabilityResponse.Add(availability);
            }

            return(Ok(availabilityResponse));
        }
コード例 #4
0
        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."));
        }