Пример #1
0
        public IActionResult RemoveItem(string cartID, int spID)
        {
            bool KM     = false;
            bool falshD = false;
            var  myItem = (from c in _context.CartItems where c.CartId == cartID && c.Idsanpham == spID select c).FirstOrDefault();

            if (myItem != null)
            {
                CartView CartView = new CartView();
                CartView.cartItemsModelViews = new List <CartItemsModelView>();
                _context.CartItems.Remove(myItem);
                _context.SaveChanges();
                var returnitems = _context.CartItems.Where(
                    c => c.CartId == cartID).ToList();
                foreach (CartItems cart in returnitems)
                {
                    var sp = _context.SanPham.FirstOrDefault(n => n.IdSp == cart.Idsanpham);

                    CartItemsModelView itemoncart = new CartItemsModelView
                    {
                        CartID    = cart.CartId,
                        Quantity  = cart.Quantity.Value,
                        SpId      = cart.Idsanpham,
                        ImgLink   = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).AnhSp,
                        PricesP   = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp,
                        UnitPrice = float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp) * cart.Quantity.Value,
                        TenSP     = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).TenSp,
                    };
                    if (sp.FlashDealBd.HasValue && sp.FlashDealKt.Value >= DateTime.Now)
                    {
                        itemoncart.giaFlashDeal = sp.GiaFlashDeal;
                        itemoncart.flashDeal    = true;
                        falshD = true;
                        itemoncart.UnitPrice = float.Parse(itemoncart.giaFlashDeal) * cart.Quantity.Value;
                    }
                    else if (sp.NgayBdKm.HasValue && sp.NgayKtKm.Value >= DateTime.Now)
                    {
                        itemoncart.giaKM     = sp.KhuyenMai;
                        itemoncart.khuyenMai = true;
                        KM = true;
                        itemoncart.ngayKTKhuyenmai = sp.NgayKtKm.Value;
                        itemoncart.UnitPrice       = float.Parse(itemoncart.giaKM) * cart.Quantity.Value;
                    }
                    else
                    {
                        itemoncart.UnitPrice = float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp) * cart.Quantity.Value;
                    }
                    CartView.cartItemsModelViews.Add(itemoncart);
                    CartView.total = GetTotal(KM, falshD);
                }
                ;

                if (returnitems.Count <= 0)
                {
                    return(Ok("Cart Empty"));
                }
                return(Ok(CartView));
            }
            return(BadRequest("Error"));
        }
Пример #2
0
        public CartView UpdateCartItems([FromBody] CartItemsModelView itemUpdate)
        {
            //ModelState.Remove("TenSP");
            //ModelState.Remove("ImgLink");
            //ModelState.Remove("PricesP");
            //ModelState.Remove("UnitPrice");
            bool     KM       = false;
            bool     falshD   = false;
            CartView CartView = new CartView();

            CartView.cartItemsModelViews = new List <CartItemsModelView>();
            var caritems = _context.CartItems.SingleOrDefault(
                c => c.CartId == itemUpdate.CartID && c.Idsanpham == itemUpdate.SpId);

            caritems.Quantity = itemUpdate.Quantity;
            _context.SaveChanges();

            var returnitems = _context.CartItems.Where(
                c => c.CartId == itemUpdate.CartID).ToList();

            foreach (CartItems cart in returnitems)
            {
                var sp = _context.SanPham.FirstOrDefault(n => n.IdSp == cart.Idsanpham);
                CartItemsModelView itemoncart = new CartItemsModelView
                {
                    CartID   = cart.CartId,
                    Quantity = cart.Quantity.Value,
                    SpId     = cart.Idsanpham,
                    ImgLink  = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).AnhSp,
                    PricesP  = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp,
                    //UnitPrice = float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp) * cart.Quantity.Value,
                    TenSP = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).TenSp,
                };

                if (sp.FlashDealBd.HasValue && sp.FlashDealKt.Value >= DateTime.Now)
                {
                    itemoncart.giaFlashDeal = sp.GiaFlashDeal;
                    falshD = true;
                    itemoncart.flashDeal = true;
                    itemoncart.UnitPrice = float.Parse(itemoncart.giaFlashDeal) * cart.Quantity.Value;
                }
                else if (sp.NgayBdKm.HasValue && sp.NgayKtKm.Value >= DateTime.Now)
                {
                    itemoncart.giaKM     = sp.KhuyenMai;
                    itemoncart.khuyenMai = true;
                    KM = true;
                    itemoncart.ngayKTKhuyenmai = sp.NgayKtKm.Value;
                    itemoncart.UnitPrice       = float.Parse(itemoncart.giaKM) * cart.Quantity.Value;
                }
                else
                {
                    itemoncart.UnitPrice = float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp) * cart.Quantity.Value;
                }
                CartView.cartItemsModelViews.Add(itemoncart);
                CartView.total = GetTotal(KM, falshD);
            }
            ;
            return(CartView);
        }
Пример #3
0
        public CartView GetCartItems()
        {
            bool KM     = false;
            bool falshD = false;

            ShoppingCartId = GetCartId();
            CartView CartView = new CartView();

            CartView.cartItemsModelViews = new List <CartItemsModelView>();
            var caritems = _context.CartItems.Where(
                c => c.CartId == ShoppingCartId);

            foreach (CartItems cart in caritems)
            {
                var sp = _context.SanPham.FirstOrDefault(n => n.IdSp == cart.Idsanpham);
                CartItemsModelView itemoncart = new CartItemsModelView
                {
                    CartID   = cart.CartId,
                    Quantity = cart.Quantity.Value,
                    SpId     = cart.Idsanpham,
                    ImgLink  = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).AnhSp,
                    PricesP  = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp,
                    //UnitPrice = float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp) * cart.Quantity.Value,
                    TenSP = _context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).TenSp,
                };
                if (sp.FlashDealBd.HasValue && sp.FlashDealKt.Value >= DateTime.Now)
                {
                    itemoncart.giaFlashDeal = sp.GiaFlashDeal;
                    itemoncart.flashDeal    = true;
                    falshD = true;
                    itemoncart.UnitPrice = float.Parse(itemoncart.giaFlashDeal) * cart.Quantity.Value;
                }
                else if (sp.NgayBdKm.HasValue && sp.NgayKtKm.Value >= DateTime.Now)
                {
                    itemoncart.giaKM     = sp.KhuyenMai;
                    itemoncart.khuyenMai = true;
                    KM = true;
                    itemoncart.ngayKTKhuyenmai = sp.NgayKtKm.Value;
                    itemoncart.UnitPrice       = float.Parse(itemoncart.giaKM) * cart.Quantity.Value;
                }
                else
                {
                    itemoncart.UnitPrice = float.Parse(_context.SanPham.FirstOrDefault(n => n.IdSp.Equals(cart.Idsanpham)).GiaSp) * cart.Quantity.Value;
                }
                CartView.cartItemsModelViews.Add(itemoncart);
            }
            ;
            CartView.total = GetTotal(KM, falshD);
            return(CartView);
        }