Exemplo n.º 1
0
        /// <summary>
        /// Gets PDF document.
        /// </summary>
        /// <param name="bookingId">The booking identifier.</param>
        /// <param name="needPdf">if set to <c>true</c> [need PDF].</param>
        /// <returns>PDF document</returns>
        public HttpResponseMessage Get(int bookingId, bool needPdf)
        {
            return(ApiExceptionHelper.WrapException(() =>
            {
                if (bookingId <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(bookingId));
                }

                _logger.Info("Get PDF-booking by '{0}'", bookingId);

                var result = new HttpResponseMessage(HttpStatusCode.OK);
                using (var stream = new MemoryStream())
                {
                    _bookingService.GetBookingPdf(bookingId, stream);

                    result.Content = new ByteArrayContent(stream.GetBuffer());

                    result.Content.Headers.ContentType =
                        new MediaTypeHeaderValue("application/pdf");
                    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = $"booking_{bookingId}.pdf"
                    };
                }

                return result;
            }, _logger));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Gets all airports.
 /// </summary>
 /// <returns>List of airports</returns>
 public IEnumerable <AirportViewModel> Get()
 {
     return(ApiExceptionHelper.WrapException(() =>
     {
         _logger.Info("Get all airports.");
         var airportViewModels = Mapper.Map <IEnumerable <Airport>, IEnumerable <AirportViewModel> >(_airportService.GetAllAirports());
         _logger.Info("Returned all airports. Airports='{0}'", airportViewModels.ToJson());
         return airportViewModels;
     }, _logger));
 }
Exemplo n.º 3
0
 public IEnumerable <BookingViewModel> Get()
 {
     return(ApiExceptionHelper.WrapException(() =>
     {
         throw new NotImplementedException();
         _logger.Info("Get all bookings of user");
         return
         Mapper.Map <IEnumerable <Booking>, IEnumerable <BookingViewModel> >(
             _bookingService.GetAllBookingsByUser(_httpAuthorizationProvider.UserId));
     }, _logger));
 }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the specified airport by id.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns>
        /// Airport
        /// </returns>
        public AirportViewModel Get(string code)
        {
            return(ApiExceptionHelper.WrapException(() =>
            {
                if (string.IsNullOrEmpty(code))
                {
                    throw new ArgumentNullException(nameof(code));
                }

                _logger.Info("Get airport by code '{0}'", code);
                var airportViewModel = Mapper.Map <Airport, AirportViewModel>(_airportService.GetAirportByCode(code));
                _logger.Info("Returned Airport. Airport='{0}'", airportViewModel.ToJson());
                return airportViewModel;
            }, _logger));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the booking by Id.
        /// </summary>
        /// <param name="bookingId">The booking identifier.</param>
        /// <returns>
        /// Booking
        /// </returns>
        public BookingViewModel Get(int bookingId)
        {
            return(ApiExceptionHelper.WrapException(() =>
            {
                throw new NotImplementedException();
                if (bookingId <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(bookingId));
                }

                _logger.Info("Get booking by '{0}'", bookingId);
                return
                Mapper.Map <Booking, BookingViewModel>(_bookingService.GetBooking(
                                                           bookingId));
            }, _logger));
        }
Exemplo n.º 7
0
        public FlightSearchResponse Post(FlightSearchRequest request)
        {
            return(ApiExceptionHelper.WrapException(() =>
            {
                if (request == null)
                {
                    throw new ArgumentNullException(nameof(request));
                }

                _logger.Info("Try flight search: Request='{0}'", request.ToJson());

                var response = new FlightSearchResponse
                {
                    OutboundFlights = Mapper.Map <IEnumerable <FlightSchedule>, IEnumerable <FlightScheduleViewModel> >(
                        _flightScheduleService.GetFlightSchedules(
                            request.FromAirportCode,
                            request.ToAirportCode,
                            request.StartDateTime,
                            request.AdultCount,
                            request.InfantCount,
                            request.ChildrenCount)
                        ),
                    ReturnFlights = request.EndDateTime.HasValue
                        ? Mapper.Map <IEnumerable <FlightSchedule>, IEnumerable <FlightScheduleViewModel> >(
                        _flightScheduleService
                        .GetFlightSchedules(
                            request.ToAirportCode,
                            request.FromAirportCode,
                            request.EndDateTime.Value,
                            request.AdultCount,
                            request.InfantCount,
                            request.ChildrenCount))
                        : null
                };

                response.OutboundFlights.ForEach(f => f.Price = _priceCalculator.Calculate(f));
                response.ReturnFlights?.ForEach(f => f.Price = _priceCalculator.Calculate(f));

                _logger.Info("Flight found: Response='{0}'", response.ToJson());

                return response;
            }, _logger));
        }
Exemplo n.º 8
0
        public virtual void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Result is RedirectResult)
            {
                return;
            }
            if (!context.HttpContext.Request.Path.Value.ToLower().StartsWith("/schedule/"))
            {
                return;
            }

            var exp = context.Exception;

            if (exp == null)
            {
                object outputData = null;
                bool   isOutput   = false;
                if (context.Result is ObjectResult objectResult)
                {
                    isOutput   = true;
                    outputData = objectResult.Value;
                }
                else if (context.Result is EmptyResult)
                {
                    isOutput   = true;
                    outputData = null;
                }
                else
                {
                    //其他返回结果
                }

                if (isOutput)
                {
                    #region 封装出参
                    var output = new Output()
                    {
                        Code = 1,
                        Msg  = "操作成功",
                        Data = outputData
                    };
                    var result = new ObjectResult(output);
                    result.StatusCode = (int)HttpStatusCode.OK;
                    context.Result    = result;
                    #endregion 封装出参
                }
            }
            else
            {
                //异常
                var    exp1   = ApiExceptionHelper.Resolve(exp);
                Output output = new Output()
                {
                    Code    = 0,
                    Msg     = exp1.Msg,
                    ErrCode = exp1.ErrCode,
                    ErrMsg  = exp1.ErrMsg,
                };
                var result = new ObjectResult(output);
                result.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Result    = result;
            }
        }