public ActionResult Buy(CheckoutPurchase checkoutPurchase)
        {
            Product product;

            foreach (var item in checkoutPurchase)
            {
                product = db.Product.Find(item.ProductId);
                if (product != null)
                {
                    // update quantities products
                    product.Quantity -= item.Quantity;
                    db.Product.Attach(product);
                    db.Entry(product).Property(p => p.Quantity).IsModified = true;

                    // add new purchase
                    Purchase purchase = new Purchase
                    {
                        ClientId    = User.Identity.GetUserId(),
                        ProductId   = item.ProductId,
                        StorageDate = DateTime.Now
                    };
                    db.Purchase.Add(purchase);
                    db.SaveChanges();
                }
                else
                {
                    return(View(checkoutPurchase));
                }
            }
            checkoutPurchase.Clear();
            return(RedirectToAction("Index", "Products"));
        }
Exemple #2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl,
                                               CheckoutPurchase checkoutPurchase)
        {
            checkoutPurchase.Clear();
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
 // GET: Checkout/Delete/5
 public ActionResult Delete(CheckoutPurchase checkoutPurchase, int?id)
 {
     if (id == null)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     return(View(checkoutPurchase[id.Value] as CheckoutProduct));
 }
        public object BindModel(ControllerContext controllerContext,
                                ModelBindingContext bindingContext)
        {
            CheckoutPurchase checkoutPurchase = (controllerContext.HttpContext.Session != null) ?
                                                (controllerContext.HttpContext.Session["key_cp"] as CheckoutPurchase) :
                                                null;

            if (checkoutPurchase == null)
            {
                checkoutPurchase = new CheckoutPurchase();
                controllerContext.HttpContext.Session["key_cp"] = checkoutPurchase;
            }

            return(checkoutPurchase);
        }
 public ActionResult Edit([Bind(Include = "Index,ProductId,ProductName,ProductImage,ClientName,ProductPrice,Quantity")] CheckoutProduct checkoutProductViewModel,
                          CheckoutPurchase checkoutPurchase)
 {
     if (ModelState.IsValid)
     {
         Product product = db.Product.Find(checkoutProductViewModel.ProductId);
         if (product.Quantity >= checkoutProductViewModel.Quantity)
         {
             checkoutPurchase[checkoutProductViewModel.Index].Quantity = checkoutProductViewModel.Quantity;
             return(RedirectToAction("Index"));
         }
         else
         {
             ModelState.AddModelError("Quantity", "Error! Quantity cannot be higher than stock available");
         }
     }
     return(View(checkoutProductViewModel));
 }
        public ActionResult AddProduct(CheckoutPurchase checkoutPurchase, int productId, string clientName, int quantity = 0)
        {
            Product         product            = db.Product.Find(productId);
            CheckoutProduct myCheckoutPurchase = new CheckoutProduct
            {
                Index        = checkoutPurchase.Count,
                ProductId    = product.ProductId,
                ProductName  = product.Name,
                ProductImage = product.ImagePath,
                ClientName   = clientName,
                ProductPrice = product.Price,
                Quantity     = quantity
            };

            checkoutPurchase.Add(myCheckoutPurchase);

            return(RedirectToAction("Index", "Products"));
        }
Exemple #7
0
 public ActionResult LogOff(CheckoutPurchase checkoutPurchase)
 {
     checkoutPurchase.Clear();
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
     return(RedirectToAction("Index", "Home"));
 }
 public ActionResult QuantityProducts(CheckoutPurchase checkoutPurchase)
 {
     return(PartialView(checkoutPurchase));
 }
 // GET: Checkout
 public ActionResult Index(CheckoutPurchase checkoutPurchase)
 {
     ViewBag.TotalPrice = checkoutPurchase.Sum(cp => cp.ProductPrice * cp.Quantity);
     return(View(checkoutPurchase));
 }
 public ActionResult DeleteConfirmed(CheckoutPurchase checkoutPurchase, int id)
 {
     checkoutPurchase.RemoveAt(id);
     return(RedirectToAction("Index"));
 }