Пример #1
0
 public void AddProduct(Product product)
 {
     if (product == null)
     {
         throw new ArgumentNullException(nameof(product));
     }
     BasketProducts.Add(product);
 }
Пример #2
0
        /// <summary>
        /// Adds the passed product into the basket with the specified ID.
        /// If the basket with tha ID is null a new basket it will be automatically created before adding the item
        /// </summary>
        /// <param name="basketItem">The basket item.</param>
        /// <param name="basketId">The basket identifier.</param>
        public void AddProduct(BasketItem basketItem, int basketId)
        {
            var basket = Baskets.FirstOrDefault(i => i.BasketId == basketId);

            if (basket == null)
            {
                CreateNewBasket(basketId);
            }
            basketItem.BasketId = basketId;
            BasketItem existingItem = BasketProducts.FirstOrDefault(i => i.ProductId == basketItem.ProductId && i.BasketId == basketId);

            if (existingItem == null)
            {
                BasketProducts.Add(basketItem);
            }
            else
            {
                existingItem.Quantity += basketItem.Quantity;
            }
            UpdateBasketProperty(basketItem, basketId);
            UpdateProductStockQuantity(basketItem, null);
            SaveChanges();
        }