Example #1
0
        /// <summary>
        /// 将指定的商品添加到指定客户的购物篮里。
        /// </summary>
        /// <param name="customerID">用于指代特定客户对象的全局唯一标识。</param>
        /// <param name="productID">用于指代特定商品对象的全局唯一标识。</param>
        public void AddProductToCart(Guid customerID, Guid productID, int quantity)
        {
            var user = userRepository.Get(customerID);

            var shoppingCart = shoppingCartRepository.FindShoppingCartByUser(user);

            if (shoppingCart == null)
            {
                throw new LException("Shopping cart doesn't exist for customer {0}.", customerID);
            }
            var product          = productRepository.Get(productID);
            var shoppingCartItem = shoppingCartItemRepository.FindItem(shoppingCart, product);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem(product, shoppingCart, quantity);
                shoppingCartItemRepository.Insert(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                shoppingCartItemRepository.Update(shoppingCartItem);
            }
            Context.Commit();
        }
        public bool Add(ShoppingCartItem shoppingCartItem)
        {
            // valida quantidade
            if(shoppingCartItem.ProductQuantity <= 0)
            {
                throw new ArgumentException($"Quantidade invalida");
            }

            
           if (_productBusiness.GetById(shoppingCartItem.ProductId) == null)
                throw new ArgumentException($"Produto não encontrado : {shoppingCartItem.ProductId}");


            return _shoppingCartItemRepository.Insert(shoppingCartItem);
        }
        public void AddToCart(string customerUsername, ShoppingCartItem entity)
        {
            var product = _productRepository.GetById(entity.ProductId);

            product.StockQuantity -= entity.Quantity;
            _productRepository.Update(product);

            var shoppingCartItem = _shoppingCartItemRepository.GetEx(s => s.CustomerUserName == customerUsername && s.ProductId == entity.ProductId).FirstOrDefault();

            if (shoppingCartItem == null)
            {
                _shoppingCartItemRepository.Insert(entity);
            }
            else
            {
                _shoppingCartItemRepository.Update(entity);
            }
        }