public async Task AddProductAsync(string customerId, long productId, int quantity)
        {
            await _unitOfWork.RunInTrunsaction(async() =>
            {
                var product = await _productRepository.Get(productId);

                var cart = await _shoppingCartRepository.GetByCustomerId(customerId, includeItems: true);
                if (cart == null)
                {
                    cart = new ShoppingCart(customerId);
                    await _shoppingCartRepository.Add(cart);

                    var item = new ShoppingCartItem(cart.Id, product.Id, product.Cost, quantity);
                    await _shoppingCartItemRepository.Add(item);

                    return;
                }

                cart.LatestUpdatedOn = DateTimeOffset.UtcNow;
                await _shoppingCartRepository.Update(cart);

                var cartItem = cart.ShoppingCartItems.FirstOrDefault(x => x.ProductId == product.Id);
                if (cartItem == null)
                {
                    var item = new ShoppingCartItem(cart.Id, product.Id, product.Cost, quantity);
                    await _shoppingCartItemRepository.Add(item);
                }
                else
                {
                    cartItem.Quantity += quantity;
                    await _shoppingCartItemRepository.Update(cartItem);
                }
            }, System.Data.IsolationLevel.Serializable);
        }
        /// <summary>
        /// 将指定的商品添加到指定客户的购物篮里。
        /// </summary>
        /// <param name="customerID">用于指代特定客户对象的全局唯一标识。</param>
        /// <param name="productID">用于指代特定商品对象的全局唯一标识。</param>
        public void AddProductToCart(Guid customerID, Guid productID, int quantity)
        {
            var user = userRepository.GetByKey(customerID);

            var shoppingCart = shoppingCartRepository.FindShoppingCartByUser(user);

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

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem(product, shoppingCart, quantity);
                shoppingCartItemRepository.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                shoppingCartItemRepository.Update(shoppingCartItem);
            }
            Context.Commit();
        }
        public void AddProductToCart(Guid customerId, Guid productId, int quantity)
        {
            var user = _userRepository.GetByKey(customerId);

            var shoppingCart = _shoppingCartRepository.GetBySpecification(new ExpressionSpecification <ShoppingCart>(s => s.User.Id == user.Id));

            if (shoppingCart == null)
            {
                throw new DomainException("用户{0}不存在购物车.", customerId);
            }

            var product          = _productRepository.GetByKey(productId);
            var shoppingCartItem = _shoppingCartItemRepository.FindItem(shoppingCart, product);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem()
                {
                    Product      = product,
                    ShoopingCart = shoppingCart,
                    Quantity     = quantity
                };

                _shoppingCartItemRepository.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                _shoppingCartItemRepository.Update(shoppingCartItem);
            }

            RepositorytContext.Commit();
        }
Beispiel #4
0
        public ShoppingCart AddItemToCart(int productId, int quantity, int userId)
        {
            if (quantity < 1)
            {
                throw new Exception("Quantity cannot be less than one");
            }
            ShoppingCart cart = GetActiveCart(userId);

            if (cart == null)
            {
                cart = new ShoppingCart()
                {
                    CreateDate = DateTime.Now,
                    UserId     = userId,
                    Status     = ShoppingCartStatus.Active
                };
                shoppingCartRepository.Add(cart);
            }


            Product product = this.inventoryRepository.Get(productId);

            if (product == null)
            {
                throw new Exception($"A product with id {productId} was not found");
            }

            if (cart.Items != null && cart.Items.Any(x => x.ProductId == productId))
            {
                var item = cart.Items.First(x => x.ProductId == productId);
                item.Quantity += quantity;
                shoppingCartItemRepository.Update(item);
            }
            else
            {
                shoppingCartItemRepository.Add(new ShoppingCartItem()
                {
                    Price          = product.ContractPrice,
                    Description    = product.Description,
                    ProductId      = product.Id,
                    Quantity       = quantity,
                    ShoppingCartId = cart.Id
                });
            }

            cart.Items = shoppingCartItemRepository.Fetch(cart.Id);
            return(cart);
        }
        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);
            }
        }
Beispiel #6
0
        public void AddProductToCart(Guid customerId, Guid productId, int quantity)
        {
            var user = _userRepository.GetByKey(customerId);

            //备注:源代码此处 可以加载到shoppingCart.User的值,而这里不能,为什么?(已解决,仓储上下文必须单例)
            var shoppingCart = _shoppingCartRepository.GetBySpecification(new ExpressionSpecification <ShoppingCart>(s => s.User.Id == user.Id));

            /*调试代码*/
            //var shoppingCart1 = _shoppingCartRepository.GetBySpecification(new ExpressionSpecification<ShoppingCart>(s => s.User.Id == user.Id),s=>s.User);
            //var userInfo = shoppingCart.User;   /*User 非vitrual  不支持延时加载*/
            /*以上*/
            if (shoppingCart == null)
            {
                throw new DomainException("用户{0}不存在购物车", customerId);
            }

            var product          = _productRepository.GetByKey(productId);
            var shoppingCartItem = _shoppingCartItemRepository.FindItem(shoppingCart, product);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem()
                {
                    Product      = product,
                    ShoopingCart = shoppingCart,
                    Quantity     = quantity
                };

                _shoppingCartItemRepository.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.UpdateQuantity(shoppingCartItem.Quantity + quantity);
                _shoppingCartItemRepository.Update(shoppingCartItem);
            }
            RepositoryContext.Commit();
        }
        public ShoppingCartItem Update(ShoppingCartItem shoppingCartItem)
        {
            _shoppingCartItemRepository.Update(shoppingCartItem);

            return(shoppingCartItem);
        }
Beispiel #8
0
 public void EditShoppingCartItem(ShoppingCartItem shoppingCartItem)
 {
     _shoppingCartItemRepository.Update(shoppingCartItem);
 }