public void UpdateShoppingCartDatabase(String cartId, ShoppingCartUpdates[] CartItemUpdates) { using (var db = new Shopping_Cart.Models.ProductContext()) { try { int CartItemCount = CartItemUpdates.Count(); List <CartItem> myCart = GetCartItems(); foreach (var cartItem in myCart) { for (int i = 0; i < CartItemCount; i++) { if (cartItem.Product.ProductId == CartItemUpdates[i].ProductId) { if (CartItemUpdates[i].PurchaseQuantity < 1 || CartItemUpdates[i].RemoveItem == true) { RemoveItem(cartId, cartItem.ProductId); } else { UpdateItem(cartId, cartItem.ProductId, CartItemUpdates[i].PurchaseQuantity); } } } } } catch (Exception exp) { throw new Exception("ERRO: Unable to Update Cart Database -" + exp.Message.ToString(), exp); } } }
public void UpdateItem(string updateCartID, int updateProductID, int quantity) { using (var _db = new Shopping_Cart.Models.ProductContext()) { try { var myItem = (from c in _db.ShoppingCartItems where c.CartId == updateCartID && c.Product.ProductID == updateProductID select c).FirstOrDefault(); if (myItem != null) { myItem.Quantity = quantity; _db.SaveChanges(); } } catch (Exception exp) { throw new Exception("Erro: unable to Update Cart Item-" + exp.Message.ToString(), exp); } } }
public void RemoveItem(string removeCartID, int removeProductID) { using (var _db = new Shopping_Cart.Models.ProductContext()) { try { var myItem = (from c in _db.ShoppingCartItems where c.CartId == removeCartID && c.Product.ProductId == removeCartID select c).FirstOrDefault(); if (myItem != null) { _db.ShoppingCartItems.Remove(myItem); _db.SaveChanges(); } } catch (Exception exp) { throw new Exception("Error: Unable to Remove Cart Item -" + exp.Message.ToString(), exp); } } }