Exemple #1
0
        public PagedList <Track> GetAllPagination(TrackResourceParameters resourceParams)
        {
            var collectionBeforPaging = _context.Tracks
                                        .Include(a => a.Artists)
                                        .AsQueryable();

            //filter by name if exists
            if (!string.IsNullOrEmpty(resourceParams.Name))
            {
                collectionBeforPaging = collectionBeforPaging
                                        .Where(t => t.Name == resourceParams.Name);
            }

            //searh if exists
            if (!string.IsNullOrEmpty(resourceParams.SearchQuery))
            {
                collectionBeforPaging = collectionBeforPaging
                                        .Where(a => a.Name.Contains(resourceParams.SearchQuery));
            }

            return(PagedList <Track> .Create(collectionBeforPaging, resourceParams.PageNumber, resourceParams.PageSize));
        }
        public async Task <IActionResult> Get([FromQuery] TrackResourceParameters resourceParameters)
        {
            //Also must modify paginationAsync name since it is not async
            var tracks = _trackRepo.GetAllPagination(resourceParameters);

            var mappedTracks = _mapper.Map <IEnumerable <TrackDto> >(tracks);

            //constructing links to previous and next pages
            var previousPage = tracks.HasPrevious ?
                               _linkService.CreateResourceUri(resourceParameters, ResourceType.PreviousPage) : null;

            var nextPage = tracks.HasNext ?
                           _linkService.CreateResourceUri(resourceParameters, ResourceType.NextPage) : null;


            mappedTracks = mappedTracks.Select(track =>
            {
                track = _linkService.CreateLinks(track);
                return(track);
            });

            var paginationMetadata = new
            {
                totalCount       = tracks.TotalCount,
                pageSize         = tracks.PageSize,
                currentPage      = tracks.CurrentPage,
                totalPages       = tracks.TotalPages,
                previousPageLink = previousPage,
                nextPageLink     = nextPage
            };

            return(Ok(new
            {
                Values = mappedTracks,
                Links = paginationMetadata
            }));
        }