Ejemplo n.º 1
0
        public ShoppingCart GetCart(int shoppingCartId)
        {
            var cart = shoppingCartRepository.Get(shoppingCartId);

            cart.Items = shoppingCartItemRepository.Fetch(shoppingCartId);
            return(cart);
        }
        public Order Create(int shoppingCartId, int userId, OrderPaymentMethod paymentMethod, string address1, string address2, string address3, string city, string state, string postalCode, string emailAddress)
        {
            var cart = _shoppingCartRepository.Get(shoppingCartId);

            if (cart == null)
            {
                throw new Exception("Shopping cart not found.");
            }

            if (userId != cart.UserId)
            {
                throw new Exception("Shopping cart not found.");
            }

            var cartItems = _shoppingCartItemRepository.Fetch(shoppingCartId);

            if (cartItems == null || !cartItems.Any())
            {
                throw new Exception("No items found in shopping cart.");
            }

            Order newOrder = new Order()
            {
                Items         = new List <OrderItem>(),
                CreateDate    = DateTime.UtcNow,
                PaymentMethod = paymentMethod,
                Status        = OrderStatus.Placed,
                UserId        = cart.UserId,
                Address1      = address1,
                Address2      = address2,
                Address3      = address3,
                City          = city,
                State         = state,
                PostalCode    = postalCode,
                EmailAddress  = emailAddress
            };

            foreach (var item in cartItems)
            {
                OrderItem orderItem = new OrderItem()
                {
                    ProductId = item.ProductId,
                    Quantity  = item.Quantity,
                    Price     = item.Price
                };

                newOrder.Items.Add(orderItem);
            }

            _orderRepository.Add(newOrder);

            foreach (var orderItem in newOrder.Items)
            {
                orderItem.OrderId = newOrder.Id;
                _orderItemRepository.Add(orderItem);
            }

            foreach (var cartItem in cartItems)
            {
                _shoppingCartItemRepository.Delete(cartItem.Id);
            }

            return(newOrder);
        }