Exemplo n.º 1
0
 public ShopCartVM(ShopCartDB model)
 {
     Id            = model.Id;
     UserId        = model.UserId;
     ProductId     = model.ProductId;
     ProductName   = model.ProductName;
     Description   = model.Description;
     AmountProduct = model.AmountProduct;
     Price         = model.Price;
     Image         = model.Image;
 }
Exemplo n.º 2
0
        public async Task <ActionResult> DeleteFromCart(int?id)
        {
            ViewBag.Menu = "Shop";

            if (id == null)
            {
            }
            else
            {
                using (Db db = new Db())
                {
                    ShopCartDB productCartDb = await db.ShopCarts.FindAsync(id);

                    if (productCartDb != null)
                    {
                        return(View(new ShopCartVM(productCartDb)));
                    }
                }
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> DeleteFromCart(ShopCartVM model)
        {
            using (Db db = new Db())
            {
                ShopCartDB modelDb = await db.ShopCarts.FindAsync(model.Id);

                db.ShopCarts.Remove(modelDb);
                db.SaveChanges();

                ViewBag.MessageInfo = model.ProductName + " is delete";

                modelDb = await db.ShopCarts.FindAsync(model.Id);

                if (modelDb != null)
                {
                    ViewBag.MessageInfo = modelDb.ProductName + " is't delete";
                }
                else
                {
                    var listCart = (List <ShopCartVM>)Session["Cart"];

                    if (Session["Cart"] != null)
                    {
                        for (int i = 0; i < listCart.Count; i++)
                        {
                            if (listCart[i].Id == model.Id)
                            {
                                listCart.RemoveAt(i);
                                Session["Cart"] = listCart;
                                break;
                            }
                        }
                    }
                    //await Index(null);
                }
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        //IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
        // GET: Cart
        public async Task <ActionResult> Index(int?id)
        {
            ViewBag.Menu = "Shop";

            if (id != null)
            {
                using (Db db = new Db())
                {
                    ShopProductDB productDb = await db.ShopProducts.FindAsync(id);

                    if (productDb != null)
                    {
                        // sdd to Session Cart
                        bool checkFirst = true;

                        var listCart = (List <ShopCartVM>)Session["Cart"];

                        if (Session["Cart"] != null)
                        {
                            for (int i = 0; i < listCart.Count; i++)
                            {
                                if (listCart[i].ProductId == productDb.Id)
                                {
                                    listCart[i].AmountProduct++;
                                    Session["Cart"] = listCart;
                                    checkFirst      = false;
                                    break;
                                }
                            }
                        }
                        // add to ShopCartDB
                        string userId = (string)Session["UserId"];

                        if (checkFirst)
                        {
                            ShopCartDB addProduct = new ShopCartDB
                            {
                                UserId        = (string)Session["UserId"],
                                ProductId     = productDb.Id,
                                ProductName   = productDb.Name,
                                Description   = productDb.Description,
                                Price         = productDb.Price,
                                AmountProduct = 1,
                                Image         = productDb.ImageName
                            };

                            db.ShopCarts.Add(addProduct);
                            db.SaveChanges();

                            addProduct = db.ShopCarts.Where(m => m.ProductId == addProduct.ProductId && m.UserId == userId).FirstOrDefault();
                            if (addProduct != null)
                            {
                                listCart.Add(new ShopCartVM(addProduct));
                                Session["Cart"] = listCart;
                            }
                        }
                        else
                        {
                            ShopCartDB addProduct = db.ShopCarts.Where(m => m.ProductName == productDb.Name &&
                                                                       m.UserId == userId).FirstOrDefault();
                            addProduct.AmountProduct++;
                            db.Entry(addProduct).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        //ViewBag. = "Product don't add to cart";
                        return(RedirectToAction("Index", "Shop"));
                    }
                }
            }

            if (((List <ShopCartVM>)Session["Cart"]).Count == 0)
            {
                ViewBag.Message = "Cart is empty";
            }

            var cart = Session["Cart"] as List <ShopCartVM> ?? new List <ShopCartVM>();

            decimal total = 0m;

            foreach (var item in cart)
            {
                total += item.Total;
            }

            ViewBag.TotalPriceCart = total;

            return(View(cart));
        }