Advertisement
Guest User

cont

a guest
Jun 23rd, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. public ActionResult AddToCart(Products prod, int amount)
  2.         {
  3.             if (Session["Permission"] == null) // First check the user is logged in
  4.                 return View("NoUser");
  5.             if (((string)Session["Permission"]).Equals("ADMIN")) // Admin cannot add any products into cart
  6.                 return View("AdminNotAllowed");
  7.  
  8.             if (amount == 0)
  9.                 return View("ZeroAmount");
  10.             if (amount <= -1)
  11.                 return View("ErrorAmount");
  12.             if (amount > prod.AvailableQuantity)
  13.                 return View("OutofstockAmuont");
  14.  
  15.             // Update the 'AvailableQuantity' of the specific product in the data base
  16.             var filter = Builders<Products>.Filter.Eq("Id", prod.Id);
  17.             var update = Builders<Products>.Update.Set("AvailableQuantity", prod.AvailableQuantity - amount);
  18.             DBManager.UpdateDetails(filter, update);
  19.  
  20.             if (Session["Cart"] == null) // If the cart is empty of products
  21.             {
  22.                 ((List<Products>)(Session["Cart"])).Add(prod);
  23.                 ViewBag.cart = ((List<Products>)(Session["Cart"])).Count();
  24.                 Session["Count"] = 1;
  25.  
  26.                 Amounter f = new Amounter
  27.                 {
  28.                     productAmount = amount,
  29.                     productID = prod.Id
  30.                 };
  31.  
  32.                 ((List<Amounter>)(Session["Amounts"])).Add(f);
  33.             }
  34.  
  35.             else // There are already products in the cart
  36.             {
  37.                 var obj = ((List<Amounter>)(Session["Amounter"])).FirstOrDefault(x => x.productID == prod.Id);
  38.  
  39.                 if (obj.Equals(null)) // If there is no such product in the list, but the list is not empty
  40.                 {
  41.                     ((List<Products>)Session["Cart"]).Add(prod);
  42.                     ViewBag.cart = ((List<Products>)Session["Cart"]).Count();
  43.                     Session["Count"] = Convert.ToInt64(Session["Count"]) + 1;
  44.  
  45.                     Amounter f = new Amounter
  46.                     {
  47.                         productAmount = amount,
  48.                         productID = prod.Id
  49.                     };
  50.                     ((List<Amounter>)Session["Amounts"]).Add(f);
  51.                 }
  52.  
  53.                 else // There specific product already presents in the list
  54.                 {
  55.                     ((List<Amounter>)(Session["Amounter"])).Remove(obj); // Remove the 'old' product
  56.                     obj.productAmount += amount;
  57.                     ((List<Amounter>)(Session["Amounter"])).Add(obj); // Adding the updated product
  58.                 }
  59.             }
  60.  
  61.             return RedirectToAction("DisplayAllProducts", "Products"); // Go to view of the products
  62.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement