public void AddShoppingItem(
            ShoppingCar shoppingCart,
            string productId,
            int count)
        {
            if (shoppingCart == null)
            {
                throw new ArgumentNullException();
            }

            var product = _productRepositorie.GetProductById(productId);

            if (product.ProductCount < count)
            {
                throw new InvalidOperationException($"Product {product.ProductId} count is not enough.");
            }

            var shoppingItem = shoppingCart.ItemListInCar
                               .FirstOrDefault(x => x.ProductId.Equals(productId));

            if (shoppingItem == null)
            {
                shoppingItem = new ShoppingItem
                {
                    ProductId   = productId,
                    ProductName = product.ProductName,
                    ItemPrice   = product.ProductPrice,
                    ItemCount   = count
                };
                shoppingCart.ItemListInCar.Add(shoppingItem);
            }
            else
            {
                shoppingItem.ItemCount += count;
            }
            _productRepositorie.RemoveProduct(productId, count);
        }
Example #2
0
 public int RemoveProductById(string id, int count)
 {
     _productRepositorie.RemoveProduct(id, count);
     return(0);
 }
Example #3
0
 public Product RemoveProductById(int id, int count = 1)
 {
     _productRepositories.RemoveProduct(id, count);
     return(_productRepositories.GetProductById(id));
 }