public List <T> SqlRaw(string sqlCode) { using (TheBestShopContext _context = new TheBestShopContext()) { return(_context.Set <T>().FromSqlRaw(sqlCode).ToList()); } }
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()); } }
public T FindById(int id) { using (TheBestShopContext _context = new TheBestShopContext()) { return(_context.Set <T>().Find(id)); } }
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()); } }
public virtual T GetOne(Expression <Func <T, bool> > filter) { using (TheBestShopContext _context = new TheBestShopContext()) { return(_context.Set <T>().FirstOrDefault(filter)); } }
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()); } }
public override Cart Update(Cart entity) { using (var context = new TheBestShopContext()) { context.Carts.Update(entity); context.SaveChanges(); return(entity); } }
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)); } }
public override Order Update(Order entity) { using (var context = new TheBestShopContext()) { context.Orders.Update(entity); context.SaveChanges(); return(entity); } }
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)); } }
public override Product Update(Product entity) { using (var context = new TheBestShopContext()) { context.Products.Update(entity); context.SaveChanges(); return(entity); } }
public DbContext CreateInstance(string serverName = "", bool instance = false, DbContext context = null) { if (instance && context != null) { return(_contextt = (TheBestShopContext)context); } else { return(_contextt = new TheBestShopContext()); } }
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()); } }
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()); } }
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(); } }