public IResult <Pagination <TSource> > Paginate ( IEnumerable <TSource> items, IPaginationInstruction paginationInstruction ) { var limit = paginationInstruction.Limit; var maxLimit = paginationInstruction.MaxLimit; var offset = paginationInstruction.Offset; var sort = paginationInstruction.Sort; if (limit < 1) { return(ResultFactory .Failure($"The limit parameter is expected to be a positive number (max: {maxLimit}).")); } if (limit > maxLimit) { return(ResultFactory .Failure($"The limit parameter must not exceed what is max allowed (max: {maxLimit}).")); } if (offset < 0) { return(ResultFactory .Failure("The offset parameter is expected to be a non-negative number.")); } var sortDirectives = SortDirective .GetSortDirectivesFrom(sort); if (sortDirectives == null) { return(ResultFactory .Failure("The sort parameter contains invalid directives (expected format: 'property[:asc|desc][,property..]', e.g. 'property1,property2:desc').")); } var invalidSortFields = sortDirectives .Where(x => !_sorters.ContainsKey(x.Property)) .Select(x => x.Property) .ToList(); if (invalidSortFields.Any()) { return(ResultFactory .Failure($"The sort parameter contains directives for nonexistent fields ('${string.Join("', '", invalidSortFields)}').")); } var paginated = Sort(items, sortDirectives) .Skip(offset) .Take(limit) .ToList(); var pagination = new Pagination <TSource>(paginated, limit, offset, sort, items.Count()); return(ResultFactory .Success(pagination)); }
public string BuildListMessagesLinkFor ( IPaginationInstruction paginationInstruction ) => BuildLinkFor(new { limit = paginationInstruction.Limit, offset = paginationInstruction.Offset, sort = paginationInstruction.Sort });