public void ShouldPaginateResults(int skip, int take) { PaginationDto pagination = new PaginationDto { Skip = skip, Take = take }; PersonOrderingDto ordering = new PersonOrderingDto(); var paginatedResult = _peopleRepository.GetAllWithPaginationAndFiltering("", ordering, pagination); var results = paginatedResult.Results.ToList(); int expectedResultCount; if (pagination.Skip >= 5) { expectedResultCount = 0; } else if (pagination.Skip + pagination.Take > 5) { expectedResultCount = 5 - pagination.Skip; } else { expectedResultCount = pagination.Take; } Assert.Equal(expectedResultCount, results.Count); for (int i = 0; i < results.Count; i++) { Assert.Equal(i + skip + 1, results[i].ID); } }
public PaginatedResultsDto <BasePersonDto> GetAllWithPaginationAndFiltering(string filter, PersonOrderingDto ordering, PaginationDto pagination) { IQueryable <Person> query = GetEntities(); int returnedCount = 0; int totalResultsCount = 0; if (!string.IsNullOrWhiteSpace(filter)) { var searchTerms = filter.Split(' ').Where(st => !string.IsNullOrWhiteSpace(st)); foreach (var searchTerm in searchTerms) { query = query.Where(p => p.Name.Contains(searchTerm)); } } query = query.OrderPeopleWithDirection(ordering.OrderingColumn, ordering.OrderingDirection); totalResultsCount = query.Count(); query = query.Skip(pagination.Skip).Take(pagination.Take); List <Person> results = query.ToList(); returnedCount = results.Count; return(new PaginatedResultsDto <BasePersonDto> { ReturnedCount = returnedCount, TotalResultsCount = totalResultsCount, Results = Mapper.Map <IEnumerable <BasePersonDto> >(results) }); }
public PaginatedResultsDto <BasePersonDto> Get(PersonOrderingDto ordering, PaginationDto pagination, string filter = null) { return(_peopleRepository.GetAllWithPaginationAndFiltering(filter, ordering, pagination)); }