//POST: ShopCarts/SaveShopCart
        private async Task <ActionResult> SaveShopCart(ShopCartViewModelList scVMList)
        {
            if (ModelState.IsValid)
            {
                foreach (var sc in scVMList.scList)
                {
                    if (sc.check)
                    {
                        var shopcart = await db.ShopCarts.FindAsync(sc.ShopCartId);

                        if (shopcart == null)
                        {
                            return(HttpNotFound());
                        }
                        shopcart.Quantity        = sc.Quantity;
                        db.Entry(shopcart).State = EntityState.Modified;
                    }
                }
                await db.SaveChangesAsync();

                TempData["Msg"] = "alert('Item selected has been saved successfully!')";
                return(RedirectToAction("ViewShopCart"));
            }
            return(View());
        }
        public async Task <ActionResult> PurchaseNow(ShopCartViewModelList scVMList)
        {
            if (ModelState.IsValid)
            {
                foreach (var scVm in scVMList.scList)
                {
                    if (scVm.check)
                    {
                        var shopcart = await db.ShopCarts.FindAsync(scVm.ShopCartId);

                        if (shopcart == null)
                        {
                            return(HttpNotFound());
                        }
                        var book = await db.Books.FindAsync(shopcart.BookID);

                        if (book == null)
                        {
                            return(HttpNotFound());
                        }
                        //Detect book StockQty, if not enough then cancel this purchase
                        if (book.StockQty < scVm.Quantity)
                        {
                            return(RedirectToAction("PurchaseCancel", "Purchases", book.BookID));
                        }
                        else if (book.StockQty == scVm.Quantity)
                        {
                            //Send Email to tell seller that one item is out of stock.
                            if (EmailUtil.OutofStockNotification(book))
                            {
                                book.OnSell = false;
                            }
                            else
                            {
                                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
                            }
                        }

                        book.StockQty       -= 1;                                         //Modified book StockQty if it was sold
                        db.Entry(book).State = EntityState.Modified;
                        db.Purchaseds.Add(new Purchased(shopcart, scVm.Quantity));
                        db.ShopCarts.Remove(shopcart);                                  //Remove purchased items from shopcart
                        if (!EmailUtil.SoldNotification(scVm, book))
                        {
                            return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
                        }
                    }
                }
                await db.SaveChangesAsync();

                TempData["Msg"] = "alert('Item selected has been purchased successfully!')";
                return(RedirectToAction("PurchaseIndex", "Purchaseds"));
            }
            return(View());
        }
        public ActionResult ViewShopCart()
        {
            FormsIdentity userIDIdentity = (FormsIdentity)User.Identity;
            int           id             = Convert.ToInt32(userIDIdentity.Ticket.UserData);
            var           shopCartList   = db.ShopCarts.Where(sc => sc.UserID == id);
            var           scList         = new ShopCartViewModelList();

            foreach (var sc in shopCartList)
            {
                scList.scList.Add(new ShopCartViewModel(sc));
            }
            return(View(scList));
        }
        public async Task <ActionResult> ShopCartAction(string shopCartButton, ShopCartViewModelList scVMList)
        {
            switch (shopCartButton)
            {
            case "Save Change":
                // delegate sending to another controller action
                return(await(SaveShopCart(scVMList)));

            case "Remove":
                // call another action to perform the cancellation
                return(await(RemoveShopCart(scVMList)));

            case "Buy Now":
                return(await(new PurchasedsController().PurchaseNow(scVMList)));

            default:
                // If they've submitted the form without a submitButton,
                // just return the view again.
                return(View());
            }
        }