/// <summary> /// Deletes the basket and all elements inside the basket. /// Returns BasketDeleteResult.NotFound In case the basket with the passed Id is non existent /// </summary> /// <param name="basketId">The basket identifier.</param> /// <returns>BasketDeleteResult.</returns> public BasketDeleteResult DeleteBasketAndAllElements(int basketId) { var basket = Baskets.FirstOrDefault(i => i.BasketId == basketId); if (basket == null) { return(BasketDeleteResult.NotFound); } else { var products = BasketProducts.FirstOrDefault(i => i.BasketId == basketId); if (products != null) { BasketProducts.RemoveRange(products); } Baskets.Remove(basket); SaveChanges(); } return(BasketDeleteResult.Ok); }
/// <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(); }
/// <summary> /// Modifies the quantity of the passed basket item. /// </summary> /// <param name="id">The identifier of the item you want to modify.</param> /// <param name="basketItem">The new basket item.</param> /// <returns>ProductInjuryResult.</returns> public ProductInjuryResult ModifyCartItem(int id, BasketItem basketItem) { BasketItem existingItem = BasketProducts.FirstOrDefault(i => i.Id == id); if (existingItem == null) { return(ProductInjuryResult.NotFound); } var newQuantity = basketItem.Quantity - existingItem.Quantity; if (newQuantity > 0) { var result = CheckProduct(basketItem.ProductId, newQuantity); if (result != ProductInjuryResult.Ok) { return(result); } } existingItem.Quantity = basketItem.Quantity; UpdateProductStockQuantity(basketItem, newQuantity); SaveChanges(); return(ProductInjuryResult.Ok); }