Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ActionResult AddToCart(Products prod, int amount)
- {
- if (Session["Permission"] == null) // First check the user is logged in
- return View("NoUser");
- if (((string)Session["Permission"]).Equals("ADMIN")) // Admin cannot add any products into cart
- return View("AdminNotAllowed");
- if (amount == 0)
- return View("ZeroAmount");
- if (amount <= -1)
- return View("ErrorAmount");
- if (amount > prod.AvailableQuantity)
- return View("OutofstockAmuont");
- // Update the 'AvailableQuantity' of the specific product in the data base
- var filter = Builders<Products>.Filter.Eq("Id", prod.Id);
- var update = Builders<Products>.Update.Set("AvailableQuantity", prod.AvailableQuantity - amount);
- DBManager.UpdateDetails(filter, update);
- if (Session["Cart"] == null) // If the cart is empty of products
- {
- ((List<Products>)(Session["Cart"])).Add(prod);
- ViewBag.cart = ((List<Products>)(Session["Cart"])).Count();
- Session["Count"] = 1;
- Amounter f = new Amounter
- {
- productAmount = amount,
- productID = prod.Id
- };
- ((List<Amounter>)(Session["Amounts"])).Add(f);
- }
- else // There are already products in the cart
- {
- var obj = ((List<Amounter>)(Session["Amounter"])).FirstOrDefault(x => x.productID == prod.Id);
- if (obj.Equals(null)) // If there is no such product in the list, but the list is not empty
- {
- ((List<Products>)Session["Cart"]).Add(prod);
- ViewBag.cart = ((List<Products>)Session["Cart"]).Count();
- Session["Count"] = Convert.ToInt64(Session["Count"]) + 1;
- Amounter f = new Amounter
- {
- productAmount = amount,
- productID = prod.Id
- };
- ((List<Amounter>)Session["Amounts"]).Add(f);
- }
- else // There specific product already presents in the list
- {
- ((List<Amounter>)(Session["Amounter"])).Remove(obj); // Remove the 'old' product
- obj.productAmount += amount;
- ((List<Amounter>)(Session["Amounter"])).Add(obj); // Adding the updated product
- }
- }
- return RedirectToAction("DisplayAllProducts", "Products"); // Go to view of the products
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement