Пример #1
0
        private void placeOrderHandler(PlaceOrder placeOrder)
        {
            var customer = _userRepository.Get(placeOrder.UserId);
            if (customer == null)
                throw new InvalidOperationException("User not found.");

            var @event = _eventRepository.Get(placeOrder.EventId);
            if (@event == null)
                throw new InvalidOperationException("Event not found.");

            if (placeOrder.PaymentInfo.CreditCardId.HasValue)
            {
                var creditCard = customer.CreditCards.FirstOrDefault(a => a.CreditCardId.Equals(placeOrder.PaymentInfo.CreditCardId.Value));
                if (creditCard == null)
                    throw new InvalidOperationException("Credit Card not found.");

                placeOrder.PaymentInfo.InstantBuyKey = creditCard.InstantBuyKey;
            }

            var order = new Order(customer, @event, placeOrder.Quantity);
            _orderRepository.Add(order);
            _unitOfWork.Commit();

            Sender.Tell(order);

            Self.Tell(new OrderPlaced(order.OrderId, placeOrder.PaymentInfo));
        }
Пример #2
0
        public async Task<HttpResponseMessage> Post(OrderViewModel orderViewModel)
        {
            if (orderViewModel == null)
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid request.");

            var paymentInfo = Mapper.Map<PaymentInfoViewModel, PaymentInfo>(orderViewModel.PaymentInfo);
            var placeOrder = new PlaceOrder(orderViewModel.Customer.UserId.Value, orderViewModel.Event.EventId.Value, orderViewModel.Quantity, paymentInfo);
            var orderPlaced = await WebApiApplication.OrderProcessorActor.Ask(placeOrder);
            if (orderPlaced is InvalidOperationException)
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ((InvalidOperationException)orderPlaced).Message);

            var result = Mapper.Map<Order, OrderViewModel>(orderPlaced as Order);
            return Request.CreateResponse(HttpStatusCode.Created, result);
        }