public List <Category> GetCategories(string search, int pageNo) { int pageSize = 3; using (var context = new GSDBContext()) { if (!string.IsNullOrEmpty(search)) { return(context.Categories.Where(category => category.Name != null && category.Name.ToLower().Contains(search.ToLower())) .OrderBy(x => x.ID) .Skip((pageNo - 1) * pageSize) .Take(pageSize) .Include(x => x.Products) .ToList()); } else { return(context.Categories .OrderBy(x => x.ID) .Skip((pageNo - 1) * pageSize) .Take(pageSize) .Include(x => x.Products) .ToList()); } } }
public Category GetCategory(int ID) { using (var context = new GSDBContext()) { return(context.Categories.Find(ID)); } }
public void UpdateCategory(Category category) { using (var context = new GSDBContext()) { context.Entry(category).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } }
public void SaveCategory(Category category) { using (var context = new GSDBContext()) { context.Categories.Add(category); context.SaveChanges(); } }
public List <Category> GetAllCategories() { using (var context = new GSDBContext()) { return(context.Categories .ToList()); } }
public void DeleteCategory(int ID) { using (var context = new GSDBContext()) { var category = context.Categories.Where(x => x.ID == ID).Include(x => x.Products).FirstOrDefault(); context.Products.RemoveRange(category.Products); //first delete products of this category context.Categories.Remove(category); context.SaveChanges(); } }
public int GetCategoriesCount(string search) { using (var context = new GSDBContext()) { if (!string.IsNullOrEmpty(search)) { return(context.Categories.Where(category => category.Name != null && category.Name.ToLower().Contains(search.ToLower())).Count()); } else { return(context.Categories.Count()); } } }