コード例 #1
0
        public PagedList <PlaylistArtist> GetAllPagination(PlaylistArtistResourceParameters resourceParams)
        {
            var collectionBeforePaging = _context.PlaylistArtists
                                         .Include(t => t.Tracks)
                                         .OrderBy(a => a.Name)
                                         .AsQueryable();


            //filter by userName
            if (!string.IsNullOrEmpty(resourceParams.UserName))
            {
                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.UserName == resourceParams.UserName);
            }



            //if name filter exists
            if (!string.IsNullOrEmpty(resourceParams.Name))
            {
                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.Name == resourceParams.Name);
            }

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

            return(PagedList <PlaylistArtist> .Create(collectionBeforePaging, resourceParams.PageNumber, resourceParams.PageSize));
        }
コード例 #2
0
        public async Task <IActionResult> Get([FromQuery] PlaylistArtistResourceParameters resourceParameters)
        {
            var artists       = _playlistArtistRepo.GetAllPagination(resourceParameters);
            var mappedArtists = _mapper.Map <IEnumerable <PlaylistArtistDto> >(artists);

            //construct links to previus+next page
            var previousPage = artists.HasPrevious ?
                               _linkService.CreateResourceUri(resourceParameters, ResourceType.PreviousPage) : null;

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

            mappedArtists = mappedArtists.Select(artist =>
            {
                artist = _linkService.CreateLinks(artist);
                return(artist);
            });

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

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