예제 #1
0
 public List <T> SqlRaw(string sqlCode)
 {
     using (TheBestShopContext _context = new TheBestShopContext())
     {
         return(_context.Set <T>().FromSqlRaw(sqlCode).ToList());
     }
 }
예제 #2
0
 public int Count(Expression <Func <T, bool> > filter = null)
 {
     using (TheBestShopContext _context = new TheBestShopContext())
     {
         return(filter == null?_context.Set <T>().Count() : _context.Set <T>().Where(filter).Count());
     }
 }
예제 #3
0
 public T FindById(int id)
 {
     using (TheBestShopContext _context = new TheBestShopContext())
     {
         return(_context.Set <T>().Find(id));
     }
 }
예제 #4
0
 public List <T> GetAll(Expression <Func <T, bool> > filter = null)
 {
     using (TheBestShopContext _context = new TheBestShopContext())
     {
         return(filter == null?_context.Set <T>().ToList() : _context.Set <T>().Where(filter).ToList());
     }
 }
예제 #5
0
 public virtual T GetOne(Expression <Func <T, bool> > filter)
 {
     using (TheBestShopContext _context = new TheBestShopContext())
     {
         return(_context.Set <T>().FirstOrDefault(filter));
     }
 }
예제 #6
0
        public List <Product> GetProductsByCategoryWithFilter(int categoryId, int pageNumber, int pageSize, Expression <Func <Product, bool> > filter = null, Func <IQueryable <Product>, IOrderedQueryable <Product> > orderby = null)
        {
            using (TheBestShopContext context = new TheBestShopContext())
            {
                var products = context.Products.AsQueryable();

                if (categoryId != -1)
                {
                    return(filter == null ?
                           orderby == null?
                           products.Include(c => c.ProductsCategories).ThenInclude(c => c.Category).Where(c => c.ProductsCategories.Any(x => x.Categories_Id == categoryId)).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                           orderby(products.Include(c => c.ProductsCategories).ThenInclude(c => c.Category).Where(c => c.ProductsCategories.Any(x => x.Categories_Id == categoryId))).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                               orderby == null?
                               products.Include(c => c.ProductsCategories).ThenInclude(c => c.Category).Where(c => c.ProductsCategories.Any(x => x.Categories_Id == categoryId)).Where(filter).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                                   orderby(products.Include(c => c.ProductsCategories).ThenInclude(c => c.Category).Where(c => c.ProductsCategories.Any(x => x.Categories_Id == categoryId))).Where(filter).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());
                }
                return(filter == null ?
                       orderby == null?
                       products.Skip((pageNumber - 1) *pageSize).Take(pageSize).ToList() :
                       orderby(products).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                           orderby == null?
                           products.Where(filter).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                               orderby(products).Where(filter).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());
            }
        }
예제 #7
0
 public override Cart Update(Cart entity)
 {
     using (var context = new TheBestShopContext())
     {
         context.Carts.Update(entity);
         context.SaveChanges();
         return(entity);
     }
 }
예제 #8
0
 public Cart GetCart(string userId)
 {
     using (var context = new TheBestShopContext())
     {
         return(context.Carts.Include(c => c.CartItems)
                .ThenInclude(c => c.Product)
                .FirstOrDefault(c => c.UserId == userId));
     }
 }
예제 #9
0
 public override Order Update(Order entity)
 {
     using (var context = new TheBestShopContext())
     {
         context.Orders.Update(entity);
         context.SaveChanges();
         return(entity);
     }
 }
예제 #10
0
 public Product GetProductWithCategory(int productId)
 {
     using (var context = new TheBestShopContext())
     {
         return(context.Products.Include(c => c.ProductsCategories)
                .ThenInclude(c => c.Category)
                .FirstOrDefault(c => c.Id == productId));
     }
 }
예제 #11
0
 public override Product Update(Product entity)
 {
     using (var context = new TheBestShopContext())
     {
         context.Products.Update(entity);
         context.SaveChanges();
         return(entity);
     }
 }
예제 #12
0
 public DbContext CreateInstance(string serverName = "", bool instance = false, DbContext context = null)
 {
     if (instance && context != null)
     {
         return(_contextt = (TheBestShopContext)context);
     }
     else
     {
         return(_contextt = new TheBestShopContext());
     }
 }
예제 #13
0
 public List <T> Pagination(Expression <Func <T, bool> > filter = null, Func <IQueryable <T>, IOrderedQueryable <T> > orderby = null, int pageNumber = 1, int pageSize = 10)
 {
     using (TheBestShopContext _context = new TheBestShopContext())
     {
         return(filter == null ?
                orderby == null?
                _context.Set <T>().Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                orderby(_context.Set <T>()).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                    orderby == null?
                    _context.Set <T>().Where(filter).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList() :
                        orderby(_context.Set <T>()).Where(filter).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());
     }
 }
예제 #14
0
        public List <Product> GetProductsByCategory(int categoryId)
        {
            using (TheBestShopContext context = new TheBestShopContext())
            {
                var products = context.Products.AsQueryable();

                if (categoryId != -1)
                {
                    return(products.Include(c => c.ProductsCategories).ThenInclude(c => c.Category).Where(c => c.ProductsCategories.Any(x => x.Categories_Id == categoryId)).ToList());
                }
                return(products.ToList());
            }
        }
예제 #15
0
 public void RemoveFromCart(int cartId, string productId = null)
 {
     using (var context = new TheBestShopContext())
     {
         if (productId != null)
         {
             context.Database.ExecuteSqlCommand($"delete from CartItems where Carts_Id={cartId} and Products_Id={Convert.ToInt32(productId)}");
         }
         else
         {
             context.Database.ExecuteSqlCommand($"delete from CartItems where Carts_Id={cartId}");
         }
         context.SaveChanges();
     }
 }