Beispiel #1
0
        public async Task <PagedList <CategoryViewBaseModel> > Handle(CategoryPagedListRequest request, CancellationToken cancellationToken)
        {
            var list = await this.db.Categories
                       .Where(x => !x.IsDeleted)
                       .Where(x => (string.IsNullOrEmpty(request.Query)) || (x.Name.Contains(request.Query)))
                       .Select(x => new CategoryViewBaseModel(x)).ToListAsync();

            var viewModelProperties = this.GetAllPropertyNameOfViewModel();
            var sortPropertyName    = !string.IsNullOrEmpty(request.SortName) ? request.SortName.ToLower() : string.Empty;
            var matchedPropertyName = viewModelProperties.FirstOrDefault(x => x == sortPropertyName);

            if (string.IsNullOrEmpty(matchedPropertyName))
            {
                matchedPropertyName = "Name";
            }

            var viewModelType = typeof(CategoryViewBaseModel);
            var sortProperty  = viewModelType.GetProperty(matchedPropertyName);

            list = request.IsDesc ? list.OrderByDescending(x => sortProperty.GetValue(x, null)).ToList() : list.OrderBy(x => sortProperty.GetValue(x, null)).ToList();

            return(new PagedList <CategoryViewBaseModel>(list, request.Offset ?? CommonConstants.Config.DEFAULT_SKIP, request.Limit ?? CommonConstants.Config.DEFAULT_TAKE));
        }
Beispiel #2
0
 public async Task <PagedList <CategoryViewBaseModel> > GetAsync([FromQuery] CategoryPagedListRequest request, CancellationToken cancellationToken)
 {
     return(await this.mediator.Send(request, cancellationToken));
 }