Exemplo n.º 1
0
        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());
                }
            }
        }
Exemplo n.º 2
0
 public Category GetCategory(int ID)
 {
     using (var context = new GSDBContext())
     {
         return(context.Categories.Find(ID));
     }
 }
Exemplo n.º 3
0
 public void UpdateCategory(Category category)
 {
     using (var context = new GSDBContext())
     {
         context.Entry(category).State = System.Data.Entity.EntityState.Modified;
         context.SaveChanges();
     }
 }
Exemplo n.º 4
0
 public void SaveCategory(Category category)
 {
     using (var context = new GSDBContext())
     {
         context.Categories.Add(category);
         context.SaveChanges();
     }
 }
Exemplo n.º 5
0
 public List <Category> GetAllCategories()
 {
     using (var context = new GSDBContext())
     {
         return(context.Categories
                .ToList());
     }
 }
Exemplo n.º 6
0
        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();
            }
        }
Exemplo n.º 7
0
 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());
         }
     }
 }