public int GetShoppingCartItemCount(Guid userID)
        {
            var user         = userRepository.GetByKey(userID);
            var shoppingCart = shoppingCartRepository.FindShoppingCartByUser(user);

            if (shoppingCart == null)
            {
                throw new InvalidOperationException("没有可用的购物车实例。");
            }
            var shoppingCartItems = shoppingCartItemRepository.FindItemsByCart(shoppingCart);

            return(shoppingCartItems.Sum(s => s.Quantity));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 通过指定的用户及其所拥有的购物篮实体,创建销售订单。
        /// </summary>
        /// <param name="user">用户实体。</param>
        /// <param name="shoppingCart">购物篮实体。</param>
        /// <returns>销售订单实体。</returns>
        public SalesOrder CreateSalesOrder(User user, ShoppingCart shoppingCart)
        {
            var shoppingCartItems = shoppingCartItemRepository.FindItemsByCart(shoppingCart);

            if (shoppingCartItems == null ||
                shoppingCartItems.Count() == 0)
            {
                throw new InvalidOperationException("购物篮中没有任何物品。");
            }

            var salesOrder = new SalesOrder();

            salesOrder.SalesLines = new List <SalesLine>();

            foreach (var shoppingCartItem in shoppingCartItems)
            {
                var salesLine = shoppingCartItem.ConvertToSalesLine();
                salesLine.SalesOrder = salesOrder;
                salesOrder.SalesLines.Add(salesLine);
                shoppingCartItemRepository.Remove(shoppingCartItem);
            }
            salesOrder.User   = user;
            salesOrder.Status = SalesOrderStatus.Paid;
            salesOrderRepository.Add(salesOrder);
            repositoryContext.Commit();
            return(salesOrder);
        }