Ejemplo n.º 1
0
        public IActionResult AddtoCart([FromBody] addCartView model)
        {
            //var cart = ShoppingCart.GetCart(this.HttpContext);

            if (Additems(model))
            {
                return(Ok("Đã thêm sản phẩm vào giỏ hàng cho bạn"));
            }
            return(NotFound("We f****d up"));
        }
Ejemplo n.º 2
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);
        }