示例#1
0
        public int Post(BookingViewModel bookingViewModel)
        {
            return(ApiExceptionHelper.WrapException(() =>
            {
                if (bookingViewModel == null)
                {
                    throw new ArgumentNullException(nameof(bookingViewModel));
                }

                _logger.Info("Attempt to create new booking. Booking='{0}'", bookingViewModel.ToJson());

                var booking = Mapper.Map <BookingViewModel, Booking>(bookingViewModel);
                booking.PaymentDateTime = DateTime.Now;
                booking.ApplicationUserId = _httpAuthorizationProvider.UserId;
                booking.Pnr = Guid.NewGuid().ToString("N").Substring(0, 6).ToUpper();

                booking.Tickets = new List <Ticket>();
                bookingViewModel.Passengers.ForEach(p =>
                {
                    var passenger = Mapper.Map <PassengerViewModel, Passenger>(p);
                    booking.Tickets.Add(new Ticket
                    {
                        //Booking = booking,
                        ElectronicTicketNumber = Guid.NewGuid().ToString("N").Substring(0, 16).ToUpper(),
                        FlightScheduleId = bookingViewModel.OutboundFlightScheduleId,
                        Passenger = passenger
                    });

                    if (bookingViewModel.ReturnFlightScheduleId.HasValue)
                    {
                        booking.Tickets.Add(new Ticket
                        {
                            //Booking = booking,
                            ElectronicTicketNumber = Guid.NewGuid().ToString("N").Substring(0, 16).ToUpper(),
                            FlightScheduleId = bookingViewModel.ReturnFlightScheduleId.Value,
                            Passenger = passenger
                        });
                    }
                });

                booking.Payment = _paymentProvider.GetPayment(bookingViewModel);

                var bookingId = _bookingService.CreateBooking(booking);
                _bookingService.SaveBooking();
                _logger.Info("Created new booking. BookingId='{0}'", bookingId);
                _emailService.SendBookingToEmail(bookingId, _httpAuthorizationProvider.CurrentEmail);
                return bookingId;
            }, _logger));
        }