示例#1
0
        public IActionResult UpdateItems([FromBody] UpdateCartView model)
        {
            if (!UserStatus.getUserStatus(this, "Client"))
            {
                var cartItems = HttpContext.Session.GetObjectFromJson <List <Carts> >(GetCartId());
                if (cartItems != null)
                {
                    //var itemToRM = cartItems.FirstOrDefault(items => items.AutoId.Equals(model.id));

                    var itemIndex = cartItems.FindIndex(sp => sp.AutoId.Equals(model.id.Trim()) && sp.CartId.Equals(ShoppingCartId));
                    cartItems[itemIndex].Quantity = model.quantity;
                    HttpContext.Session.SetObjectAsJson(GetCartId(), cartItems);
                    return(Ok("Cap nhat r nha bo"));
                }
            }
            else
            {
                var items = _context.Carts.FirstOrDefault(sp => sp.AutoId.Equals(model.id) && sp.CartId.Equals(GetCartId()));
                if (items != null)
                {
                    items.Quantity = model.quantity;
                    _context.Carts.Update(items);
                    _context.SaveChangesAsync();
                    return(Ok("Cap nhat r nha bo"));
                }
            }
            return(NotFound("Broo what are u doingg"));
        }
 // GET: Authentication
 public ActionResult Index(string returnUrl)
 {
     if (UserStatus.getUserStatus(this, "admin"))
     {
         return(RedirectToAction("Index", "Home", new { message = "Đăng nhập rồi bạn ie" }));
     }
     return(View());
 }
示例#3
0
        private List <Carts> GetCartitems()
        {
            if (UserStatus.getUserStatus(this, "Client"))
            {
                return(_context.Carts.Where(c => c.CartId.Equals(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value)).Include(sp => sp.Sp).ToList());
            }
            var cartItems = HttpContext.Session.GetObjectFromJson <List <Carts> >(GetCartId());

            return(cartItems);
        }
示例#4
0
        public IActionResult Login()
        {
            if (UserStatus.getUserStatus(this, "Client"))
            {
                return(RedirectToAction("Index", "Home", new { message = "Đăng nhập rồi bạn ie" }));
            }
            var loginNode = new MvcBreadcrumbNode("Login", "Login", "Đăng nhập");

            ViewData["BreadcrumbNode"] = loginNode;
            return(View());
        }
示例#5
0
        public IActionResult DeleteItems(string itemID)
        {
            if (!UserStatus.getUserStatus(this, "Client"))
            {
                var cartItems = HttpContext.Session.GetObjectFromJson <List <Carts> >(GetCartId());
                if (cartItems != null)
                {
                    var itemToRM = cartItems.FirstOrDefault(items => items.AutoId.Equals(itemID));
                    if (itemToRM != null)
                    {
                        cartItems.Remove(itemToRM);
                        HttpContext.Session.SetObjectAsJson(GetCartId(), cartItems);
                        return(Ok("Đã xóa sản phẩm ra khỏi giỏ rồi nha"));
                    }
                    return(NotFound("Bro wtf did just send ?"));
                }
                else
                {
                    return(NotFound("Bro your cart is empty wtf ?"));
                }
            }
            else
            {
                var cartItems = _context.Carts.ToList();;
                if (cartItems.Count() <= 0)
                {
                    return(NotFound("Bro your cart is empty wtf ?"));
                }
                else
                {
                    var itemToRM = cartItems.FirstOrDefault(items => items.AutoId.Equals(itemID));

                    if (itemToRM != null)
                    {
                        _context.Carts.Remove(itemToRM);
                        _context.SaveChanges();
                        return(Ok("Đã xóa sản phẩm ra khỏi giỏ rồi nha"));
                    }
                }
            }
            return(NotFound("Bro wtf did just send ?"));
        }
示例#6
0
 private string GetCartId()
 {
     // setting cartId if we haven't have any
     if (HttpContext.Session.GetString(CartSessionKey) == null)
     {
         if (UserStatus.getUserStatus(this, "Client"))
         {
             HttpContext.Session.SetString(CartSessionKey, HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
         }
         else
         {
             // Generate a new random GUID using System.Guid class.
             Guid tempCartId = Guid.NewGuid();
             HttpContext.Session.SetString(CartSessionKey, tempCartId.ToString());
         }
     }
     else if (UserStatus.getUserStatus(this, "Client") && HttpContext.Session.GetString(CartSessionKey) != null)
     {
         HttpContext.Session.SetString(CartSessionKey, HttpContext.User.Claims //make sure everything at this point is stay in client
                                       .FirstOrDefault(n => n.Type == ClaimTypes.NameIdentifier).Value);
     }
     return(HttpContext.Session.GetString(CartSessionKey));
 }
示例#7
0
        private bool Additems(addCartView addInfo)
        {
            ShoppingCartId = GetCartId();
            var sptoAdd = _context.Sanpham.FirstOrDefault(sp => sp.IdsanPham == addInfo.id);

            if (sptoAdd != null)
            {
                // user already logged in and the cart migrations is gud
                if (UserStatus.getUserStatus(this, "Client"))
                {
                    var items = _context.Carts
                                .SingleOrDefault(item => item.SpId == sptoAdd.IdsanPham && item.CartId.Equals(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value));
                    if (items == null)
                    {
                        //Guid random = new Guid();

                        items = new Carts
                        {
                            AutoId   = Guid.NewGuid().ToString(),
                            SpId     = sptoAdd.IdsanPham,
                            CartId   = ShoppingCartId,
                            Quantity = addInfo.quantity,
                        };
                        _context.Carts.Add(items);
                    }
                    else
                    {
                        items.Quantity = items.Quantity + addInfo.quantity;
                    }
                    _context.SaveChanges();
                }
                else
                {
                    List <Carts> cartItems = HttpContext.Session.GetObjectFromJson <List <Carts> >(GetCartId());;
                    if (cartItems != null)
                    {
                        var items = cartItems.FirstOrDefault(item => item.SpId == sptoAdd.IdsanPham && item.CartId.Equals(ShoppingCartId));
                        if (items == null)
                        {
                            //Guid random = new Guid();
                            items = new Carts
                            {
                                AutoId   = Guid.NewGuid().ToString(),
                                SpId     = sptoAdd.IdsanPham,
                                CartId   = ShoppingCartId,
                                Quantity = addInfo.quantity,
                                Sp       = sptoAdd
                            };
                            cartItems.Add(items);
                        }
                        else
                        {
                            foreach (Carts item in cartItems)
                            {
                                if (item.CartId.Equals(items.CartId) && item.SpId == items.SpId)
                                {
                                    item.Quantity = item.Quantity + addInfo.quantity;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        //Guid random = new Guid();
                        cartItems = new List <Carts>
                        {
                            new Carts
                            {
                                AutoId   = Guid.NewGuid().ToString(),
                                SpId     = sptoAdd.IdsanPham,
                                CartId   = ShoppingCartId,
                                Quantity = addInfo.quantity,
                                Sp       = sptoAdd,
                            }
                        };
                    }
                    HttpContext.Session.SetObjectAsJson(GetCartId(), cartItems);
                }
                return(true);
            }
            return(false);
        }