예제 #1
0
        public async Task <IActionResult> CreateFulfillOrderAsync([FromBody] FulfillBookingRequest fulfillOrderRequest)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(CreateFulfillOrderAsync));

            var response = new SingleResponse <ExpandoObject>();

            try
            {
                var invalidData = ValidationData(fulfillOrderRequest);

                if (invalidData != null)
                {
                    return(invalidData);
                }

                var confimrationInfo = Mapper.Map <ConfirmationInfo>(fulfillOrderRequest.ConfirmationInfoViewModel);
                var payment          = Mapper.Map <Payment>(fulfillOrderRequest.PaymentViewModel);
                var travellers       = Mapper.Map <List <Traveller> >(fulfillOrderRequest.TravellerViewModels);
                var order            = new Order
                {
                    Id           = Guid.NewGuid(),
                    FlightId     = fulfillOrderRequest.FlightId,
                    Code         = Utils.GenerateReservationCode(6),
                    Payment      = payment,
                    Confirmation = confimrationInfo,
                    CreatedDate  = DateTime.Now
                };

                DbContext.Order.Add(order);
                DbContext.Traveller.AddRange(travellers);

                foreach (var traveller in travellers)
                {
                    DbContext.TravellerOrder.Add(new TravellerOrder
                    {
                        OrderId     = order.Id,
                        TravellerId = traveller.Id
                    });
                }

                await DbContext.SaveChangesAsync();

                dynamic returnModel = new ExpandoObject();
                returnModel.BookingId = order.Id;
                returnModel.Code      = order.Code;

                response.Model = returnModel;
            }
            catch (Exception ex)
            {
                response.DidError     = true;
                response.ErrorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(CreateFulfillOrderAsync), ex);
            }

            return(response.ToHttpResponse());
        }