public virtual async Task <PagedResultDto <object> > GetAll(TopicFilterDto input) { var query = postRepository.GetAll() .Include(p => p.TopicCategory) .WhereIf(input.Id != null, p => p.Id == input.Id) .WhereIf(input.TopicCategoryId != null, p => p.TopicCategoryId == input.TopicCategoryId); var totalCount = await query.CountAsync(); query = ApplySorting(query, input); query = ApplyPaging(query, input); var entities = await query.ToListAsync(); return(new PagedResultDto <object>( totalCount, entities.Select(p => new { CategoryName = p.TopicCategory?.Name, p.Id, p.Name, p.CreationTime, p.IsActive, p.DisplayOrder }) .ToList() )); }
protected virtual IQueryable <Topic> ApplyPaging(IQueryable <Topic> query, TopicFilterDto input) { var pagedInput = input as IPagedResultRequest; if (pagedInput != null) { return(query.PageBy(pagedInput)); } var limitedInput = input as ILimitedResultRequest; if (limitedInput != null) { return(query.Take(limitedInput.MaxResultCount)); } return(query); }
protected virtual IQueryable <Topic> ApplySorting(IQueryable <Topic> query, TopicFilterDto input) { var sortInput = input as ISortedResultRequest; if (sortInput != null) { if (sortInput.Sorting.IsNotNullOrEmpty()) { return(query.OrderBy(sortInput.Sorting)); } } if (input is ILimitedResultRequest) { return(query.OrderByDescending(e => e.Id)); } return(query); }