예제 #1
0
        public Task <BookingResource> Handle(CreateBookingCommand request, CancellationToken cancellationToken)
        {
            if (request.Form.Flight == null)
            {
                throw new ArgumentNullException(nameof(request.Form.Flight), "Flight form is required to create a booking.");
            }

            if (request.Form.Passengers == null)
            {
                throw new ArgumentNullException(nameof(request.Form.Flight), "Passenger forms is required to create a booking.");
            }

            // Create flight
            var flightForm          = request.Form.Flight;
            var createFlightService = new CreateFlightService();
            var flight = createFlightService.CreateFlight(id: _bookingRepository.GetNextFlightId(),
                                                          number: flightForm.FlightNumber,
                                                          departureAirport: _airportRepository.GetById(flightForm.DepartureAirportId) ??
                                                          throw new ArgumentException("Airport not found", nameof(flightForm.DepartureAirportId)),
                                                          departureDate: flightForm.DepartureDate,
                                                          arrivalAirport: _airportRepository.GetById(flightForm.ArrivalAirportId) ??
                                                          throw new ArgumentException("Airport not found", nameof(flightForm.DepartureAirportId)),
                                                          arrivalDate: flightForm.ArrivalDate,
                                                          carrier: _airlineRepository.GetById(flightForm.CarrierId) ??
                                                          throw new ArgumentException("Airline not found", nameof(flightForm.DepartureAirportId)),
                                                          price: flightForm.Price);

            // Create passenger
            var passengers = new List <Passenger>();

            foreach (var passengerForm in request.Form.Passengers)
            {
                var passenger = new Passenger(id: _bookingRepository.GetNextPassengerId(),
                                              name: passengerForm.Name,
                                              email: passengerForm.Email,
                                              address: passengerForm.Address,
                                              gender: (GenderType)passengerForm.Gender,
                                              dateBirth: passengerForm.DateBirth);
                passengers.Add(passenger);
            }

            // Create booking
            var createBookingService = new CreateBookingService();
            var booking = createBookingService.CreateBooking(id: _bookingRepository.GetNextBookingId(),
                                                             bookingNumber: request.Form.BookingNumber,
                                                             bookingDate: request.Form.DateBooking,
                                                             passengers: passengers,
                                                             flight: flight,
                                                             customer: _customerRepository.GetById(request.Form.CustomerId)) ??
                          throw new ArgumentException("Customer not found", nameof(flightForm.DepartureAirportId));

            // Booking is successfully created in memory. Lets save it to db.
            _bookingRepository.Save(booking);

            return(Task.FromResult(booking.ToResource()));
        }
        private Func <Booking> BuildCreateBookingServiceCall(Action <SampleArguments> argsModifier = null)
        {
            var createBookingService = new CreateBookingService();
            var args = new SampleArguments();

            argsModifier?.Invoke(args);

            return(() => createBookingService.CreateBooking(args.Id,
                                                            args.BookingNumber,
                                                            args.Customer,
                                                            args.BookingDate,
                                                            args.Flight,
                                                            args.Passengers));
        }