public List <Product_Cart> getProductCartsByEmail(string email) { List <Product_Cart> result = new List <Product_Cart>(); string sql = "SELECT * FROM Product_Cart WHERE Email = N'" + email + "'"; SqlDataReader reader = DataProvider.ExecuteQueryWithDataReader(sql, CommandType.Text); while (reader.Read()) { int productId = reader.GetInt32(1); ProductDAL productDal = new ProductDAL(); Product product = productDal.getProductById(productId); Product_Cart productCart = new Product_Cart() { Email = reader.GetString(0), Product_ID = reader.GetInt32(1), Quantity = reader.GetInt32(2), Product = product, }; result.Add(productCart); } reader.Close(); return(result); }
public bool addProductCart(Product_Cart productCart) { string sql = "INSERT Product_Cart VALUES(@Email, @ProductId, @Quantity)"; SqlParameter emailParam = new SqlParameter("@Email", productCart.Email); SqlParameter productIdParam = new SqlParameter("@ProductId", productCart.Product_ID); SqlParameter quantityParam = new SqlParameter("@Quantity", productCart.Quantity); bool result = DataProvider.ExecuteNonQuery(sql, CommandType.Text, emailParam, productIdParam, quantityParam); return(result); }
public bool updateQuantity(Product_Cart productCart) { string sql = "UPDATE Product_Cart" + " SET Quantity = @Quantity" + " WHERE Email = @Email AND Product_ID = @ProductId"; SqlParameter quantityParam = new SqlParameter("@Quantity", productCart.Quantity); SqlParameter emailParam = new SqlParameter("@Email", productCart.Email); SqlParameter productIdParam = new SqlParameter("@ProductId", productCart.Product_ID); bool result = DataProvider.ExecuteNonQuery(sql, CommandType.Text, quantityParam, emailParam, productIdParam); return(result); }
private bool addToCart() { List <Product_Cart> myCart = new List <Product_Cart>(); if (Session["CART"] != null) { myCart = (List <Product_Cart>)Session["CART"]; } if (Session["USER"] != null) { User_Account userAccount = (User_Account)Session["USER"]; ICollection <Product_Cart> productCarts = userAccount.Product_Cart; Product_CartDAL productCartDal = new Product_CartDAL(); foreach (var myProduct in myCart) { int productId = myProduct.Product_ID; int quantity = (int)myProduct.Quantity; ProductDAL productDal = new ProductDAL(); Product product = productDal.getProductById(productId); bool isContain = false; foreach (var item in productCarts) { if (item.Product_ID == productId) { isContain = true; if (item.Quantity + quantity <= product.Products_Available) { item.Quantity += quantity; productCartDal.updateQuantity(item); } else { item.Quantity = product.Products_Available; productCartDal.updateQuantity(item); } break; } } if (!isContain) { Product_Cart productCart = new Product_Cart() { Email = userAccount.Email, Quantity = quantity, Product_ID = productId, Product = product, }; productCarts.Add(productCart); productCartDal.addProductCart(productCart); } } } else { return(false); } return(true); }
public ActionResult Add(FormCollection f) { string quantityStr = f["myNumber"]; Int32 quantity = Convert.ToInt32(quantityStr); string productIdStr = f["productId"]; int productId = int.Parse(productIdStr); ProductDAL productDal = new ProductDAL(); Product product = productDal.getProductById(productId); if (quantity > product.Products_Available) { Session["ADD_ERROR"] = "This product has only " + product.Products_Available + " items"; return(RedirectToAction("ProductDetails", "Product", new { productIDStr = @product.Product_ID + "" })); } if (Session["USER"] != null) { User_Account userAccount = (User_Account)Session["USER"]; ICollection <Product_Cart> productCarts = userAccount.Product_Cart; Product_CartDAL productCartDal = new Product_CartDAL(); bool isContain = false; foreach (var item in productCarts) { if (item.Product_ID == productId) { isContain = true; if (item.Quantity + quantity > product.Products_Available) { Session["ADD_ERROR"] = "This product has only " + (product.Products_Available - item.Quantity) + " items"; return(RedirectToAction("ProductDetails", "Product", new { productIDStr = @product.Product_ID + "" })); } item.Quantity += quantity; productCartDal.updateQuantity(item); break; } } if (!isContain) { Product_Cart productCart = new Product_Cart() { Email = userAccount.Email, Quantity = quantity, Product_ID = productId, Product = product, }; productCarts.Add(productCart); productCartDal.addProductCart(productCart); } } else { List <Product_Cart> myCart = new List <Product_Cart>(); if (Session["CART"] != null) { myCart = (List <Product_Cart>)Session["CART"]; } Product_Cart productCart = null; foreach (var item in myCart) { if (productIdStr.Equals(item.Product_ID + "")) { productCart = item; break; } } if (productCart != null) { if (productCart.Quantity + quantity > product.Products_Available) { Session["ADD_ERROR"] = "This product has only " + (product.Products_Available - productCart.Quantity) + " items"; return(RedirectToAction("ProductDetails", "Product", new { productIDStr = @product.Product_ID + "" })); } productCart.Quantity += quantity; } else { productCart = new Product_Cart() { Quantity = quantity, Product_ID = productId, Product = product, }; myCart.Add(productCart); } Session["CART"] = myCart; } return(RedirectToAction("ViewCart", "Cart")); }