public bool DeleteShoppingCart(int Id, int userId)
        {
            try
            {
                using (var db = new IconextBookShopEntities())
                {
                    var item = db.ShoppingCarts.FirstOrDefault(x => x.Id == Id && x.UserId == userId);

                    if (item != null && item.Quantity > 0)
                    {
                        db.ShoppingCarts.Remove(item);
                        db.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
 public List<Product> GetProductsByCategory(int categoryId)
 {
     using (var db = new IconextBookShopEntities())
     {
         var products = db.Products.Where(x => x.CategoryId == categoryId).ToList();
         return products;
     }
 }
 public System.Collections.Generic.List<Product> GetProducts()
 {
     using (var db = new IconextBookShopEntities())
     {
         var products = db.Products.Include("Category").ToList();
         return products;
     }
 }
 public List<Category> GetCategory()
 {
     using (var db = new IconextBookShopEntities())
     {
         var category = db.Categories.ToList();
         return category;
     }
 }
        public Product Get(int productId)
        {
            using (var db = new IconextBookShopEntities())
            {
                var product = db.Products.Include("Category").FirstOrDefault(x => x.Id == productId);

                return product;
            }
        }
 public List<BookDB.OrderDetail> GetByOrderId(int orderId)
 {
     using (var db = new IconextBookShopEntities())
     {
         var orderDetails = db.OrderDetails
             .Include(x => x.Order)
             .Include(x => x.Order.User)
             .Where(x => x.OrderId == orderId).ToList();
         return orderDetails;
     }
 }
 public bool InsertOrderDetail(List<OrderDetail> orderDetails, int orderId)
 {
     try
     {
         using (var db = new IconextBookShopEntities())
         {
             db.OrderDetails.AddRange(orderDetails);
             db.SaveChanges();
         }
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
 public bool UpdateBalance(int productId, int balance)
 {
     using (var db = new IconextBookShopEntities())
     {
         var product = db.Products.FirstOrDefault(m => m.Id == productId);
         if (product != null)
         {
             product.Balance = balance;
             db.SaveChanges();
             return true;
         }
         else
         {
             return false;
         }
     }
 }
        public bool Save(Product product)
        {
            try
            {
                using (var db = new IconextBookShopEntities())
                {
                    db.Products.Add(product);
                    db.SaveChanges();
                }

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
 public bool DeleteAllCart(int userId)
 {
     try
     {
         using (var db = new IconextBookShopEntities())
         {
             var shoppingCarts = db.ShoppingCarts.Where(x => x.UserId == userId);
             db.ShoppingCarts.RemoveRange(shoppingCarts);
             db.SaveChanges();
             return true;
         }
     }
     catch (Exception)
     {
         return false;
     }
 }
        public bool Delete(int productId)
        {
            bool isSuccess = true;
            try
            {
                using (var db = new IconextBookShopEntities())
                {
                    var product = db.Products.Where(x => x.Id == productId).FirstOrDefault();
                    product.IsActive = false;
                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                isSuccess = false;
            }

            return isSuccess;
        }
 public bool AddItemCart(ShoppingCart shoppingCart)
 {
     using (var db = new IconextBookShopEntities())
     {
         var ifHaveInCart = db.ShoppingCarts.FirstOrDefault(x => x.ProductId == shoppingCart.ProductId && x.UserId == shoppingCart.UserId);
         if (ifHaveInCart != null)
         {
             try
             {
                 ifHaveInCart.Quantity += shoppingCart.Quantity;
                 db.SaveChanges();
                 return true;
             }
             catch (Exception e)
             {
                 return false;
             }
         }
         else
         {
             try
             {
                 var item = new ShoppingCart();
                 item.ProductId = shoppingCart.ProductId;
                 item.Quantity = shoppingCart.Quantity;
                 item.UserId = shoppingCart.UserId;
                 item.TimeStamp = DateTime.Now;
                 db.ShoppingCarts.Add(item);
                 db.SaveChanges();
                 return true;
             }
             catch (Exception e)
             {
                 return false;
             }
         }
     }
     return false;
 }
 public List<ShoppingCart> GetList(int Id)
 {
     using (var db = new IconextBookShopEntities())
     {
         var items = db.ShoppingCarts.Where(x => x.UserId == Id).Include(x => x.Product).Include(x => x.Product.Category);
         return items.ToList();
     }
 }
        public bool UpdateShoppingCartTotal(int id, int userid, int quantityTotal)
        {
            try
            {
                using (var db = new IconextBookShopEntities())
                {
                    var item = db.ShoppingCarts.FirstOrDefault(x => x.Id == id && x.UserId == userid);

                    if (item != null)
                    {
                        item.Quantity = quantityTotal;
                        item.TimeStamp = DateTime.Now;
                        db.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }