예제 #1
0
 public async Task ValidateListGetModel(CategoryListGet model)
 {
     if (model == null)
     {
         throw new ValidationFailedException("No query data provided");
     }
     if (model.LanguageId == 0)
     {
         throw new ValidationFailedException("Must specify language");
     }
     if (model.Page <= 0)
     {
         throw new ValidationFailedException("Page must be greater than zero");
     }
     if (model.PageSize <= 0)
     {
         throw new ValidationFailedException("Page size must be greater than zero");
     }
 }
예제 #2
0
        public async Task <CategoryListGetResult> GetCategoryList(CategoryListGet model)
        {
            var validatorResult = validator.ValidateListGetModel(model);

            var query = from cat in context.Categories

                        where cat.LanguageId == model.LanguageId &&
                        (model.Name == null || model.Name == "" || cat.Name.Contains(model.Name) || model.Name.Contains(cat.Name))

                        select new CategoryModel
            {
                Id   = cat.Id,
                Name = cat.Name
            };

            var count = await query.CountAsync();

            if (model.OrderByDesc)
            {
                query = query.OrderByDescending(x => x.Name);
            }
            else
            {
                query = query.OrderBy(x => x.Name);
            }

            if (model.PageSize > 0)
            {
                var skipValue = (model.Page - 1) * model.PageSize;
                query = query.Skip(skipValue).Take(model.PageSize);
            }

            var categories = await query.ToListAsync();

            return(new CategoryListGetResult()
            {
                Count = count,
                Page = model.Page,
                PageCount = (int)Math.Ceiling((decimal)count / (decimal)model.PageSize),
                CategoryList = categories
            });
        }
예제 #3
0
 public async Task <CategoryListGetResult> GetCategoryList([FromBody] CategoryListGet request)
 {
     return(await categoryProvider.GetCategoryList(request));
 }