Пример #1
0
        public async Task <IActionResult> GetPets([FromQuery] PetParametersDto petParametersDto)
        {
            var petsFromRepo = await _petRepository.GetPetsAsync(petParametersDto);

            var paginationMetadata = new
            {
                totalCount        = petsFromRepo.TotalCount,
                pageSize          = petsFromRepo.PageSize,
                currentPageSize   = petsFromRepo.CurrentPageSize,
                currentStartIndex = petsFromRepo.CurrentStartIndex,
                currentEndIndex   = petsFromRepo.CurrentEndIndex,
                pageNumber        = petsFromRepo.PageNumber,
                totalPages        = petsFromRepo.TotalPages,
                hasPrevious       = petsFromRepo.HasPrevious,
                hasNext           = petsFromRepo.HasNext
            };

            Response.Headers.Add("X-Pagination",
                                 JsonSerializer.Serialize(paginationMetadata));

            var petsDto  = _mapper.Map <IEnumerable <PetDto> >(petsFromRepo);
            var response = new Response <IEnumerable <PetDto> >(petsDto);

            return(Ok(response));
        }
Пример #2
0
        public async Task <PagedList <Pet> > GetPetsAsync(PetParametersDto petParameters)
        {
            if (petParameters == null)
            {
                throw new ArgumentNullException(nameof(petParameters));
            }

            var collection = _context.Pets
                             as IQueryable <Pet>; // TODO: AsNoTracking() should increase performance, but will break the sort tests. need to investigate

            var sieveModel = new SieveModel
            {
                Sorts   = petParameters.SortOrder ?? "PetId",
                Filters = petParameters.Filters
            };

            collection = _sieveProcessor.Apply(sieveModel, collection);

            return(await PagedList <Pet> .CreateAsync(collection,
                                                      petParameters.PageNumber,
                                                      petParameters.PageSize));
        }