public ActionResult BuyThisLaptop(int id, int CustomerId, int Quantity) { Customer customer = db.Customers.Find(CustomerId); Laptop laptop = db.Laptops.Find(id); if (customer == null) { return(HttpNotFound()); } var customerlaptop = customer.CustomerLaptops.SingleOrDefault(cl => cl.LaptopId == id); if (customerlaptop == null) { customerlaptop = new CustomerLaptop(); customerlaptop.LaptopId = id; customerlaptop.CustomerId = CustomerId; customerlaptop.Quantity = Quantity; } else { customerlaptop.Quantity += Quantity; } customerlaptop.Cost = customerlaptop.Quantity * laptop.DollarValue; if (customer.Wallet < customerlaptop.Cost) { ViewBag.Message = "The customer does not have enough money"; ViewBag.CustomerId = new SelectList(db.Customers, "Id", "Email"); return(View(laptop)); } else if (customerlaptop.Quantity > laptop.QuantityAvailable) { ViewBag.Message = "The laptop does not have enough stock"; ViewBag.CustomerId = new SelectList(db.Customers, "Id", "Email"); return(View(laptop)); } else { customer.Wallet -= customerlaptop.Cost; laptop.QuantityAvailable -= customerlaptop.Quantity; db.CustomerLaptops.Add(customerlaptop); db.SaveChanges(); return(RedirectToAction("PurchaseResult", customerlaptop)); } }
public ActionResult PurchaseResult(CustomerLaptop customerlaptop) { return(View(db.CustomerLaptops.Find(customerlaptop.Id))); }