public PagedList <PlaylistTrack> GetAllPagination(PlaylistTrackResourceParameters resourceParams) { var collectionBeforPaging = _context.PlaylistTracks .OrderBy(t => t.Name) .AsQueryable(); //filter by userName if (!string.IsNullOrEmpty(resourceParams.UserName)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.UserName == resourceParams.UserName); } //filter by name if name =||= if (!string.IsNullOrEmpty(resourceParams.Name)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.Name == resourceParams.Name); } //searh if exists if (!string.IsNullOrEmpty(resourceParams.SearchQuery)) { collectionBeforPaging = collectionBeforPaging .Where(a => a.Name.Contains(resourceParams.SearchQuery)); } return(PagedList <PlaylistTrack> .Create(collectionBeforPaging, resourceParams.PageNumber, resourceParams.PageSize)); }
public async Task <IActionResult> Get([FromQuery] PlaylistTrackResourceParameters resourceParameters) { var tracks = _playlistTrackRepo.GetAllPagination(resourceParameters); var mappedTracks = _mapper.Map <IEnumerable <PlaylistTrackDto> >(tracks); //construct links to previus+next page 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 })); }