public static Response UpdateCart(int ProductID, int UserID, int Quantity) { Product pro = ProductRepositories.GetProduct(ProductID); if (Quantity > pro.Stock) { return(new Response(false, "Quantity must be less than or equals to current stock ")); } List <Cart> CartList = CartRepositories.GetCartbyProduct(ProductID); Cart cart = CartRepositories.GetCart(ProductID, UserID); if (Quantity == 0) { CartRepositories.DeleteCart(ProductID, UserID); return(new Response(true)); } if (CartList.Any()) { int total = TotalInCart(CartList); int stock = pro.Stock - total + cart.Quantity; if (Quantity > stock) { return(new Response(false, "Quantity must be " + stock.ToString() + "or below")); } else { CartRepositories.UpdateCart(cart, Quantity); return(new Response(true)); } } CartRepositories.UpdateCart(cart, Quantity); return(new Response(true)); }
public static Response InsertCart(int UserID, int ProductID, int Quantity) { Product pro = ProductRepositories.GetProduct(ProductID); if (Quantity > pro.Stock) { return(new Response(false, "Quantity must be less than or equals to current stock ")); } int total = 0; List <Cart> CartList = CartRepositories.GetCartbyProduct(ProductID); if (CartList.Any()) { total = TotalInCart(CartList); int stock = pro.Stock - total; if (Quantity > stock) { if (stock < 1) { return(new Response(false, "Cant add to cart because out of stock.")); } else { return(new Response(false, "Quantity must be " + stock.ToString() + " or below")); } } } Cart CheckCartExists = CartRepositories.GetCart(ProductID, UserID); if (CheckCartExists != null) { int stock = pro.Stock - total; if (Quantity > stock) { if (stock < 1) { return(new Response(false, "Cant add to cart because out of stock.")); } else { return(new Response(false, "Quantity must be " + stock.ToString() + " or below")); } } else { CartRepositories.UpdateCart(CheckCartExists, Quantity + CheckCartExists.Quantity); return(new Response(true)); } } Cart cart = CartFactories.InsertCart(UserID, ProductID, Quantity); CartRepositories.InsertCart(cart); return(new Response(true)); }