public async Task <IActionResult> Get([FromQuery] PlaylistAlbumResourceParameters resourceParameters) { var albums = _playlistAlbumRepo.GetAllPagination(resourceParameters); var mappedAlbums = _mapper.Map <IEnumerable <PlaylistAlbumDto> >(albums); //construct links to previus+next page var previousPage = albums.HasPrevious ? _linkService.CreateResourceUri(resourceParameters, ResourceType.PreviousPage) : null; var nextPage = albums.HasNext ? _linkService.CreateResourceUri(resourceParameters, ResourceType.NextPage) : null; mappedAlbums = mappedAlbums.Select(track => { track = _linkService.CreateLinks(track); return(track); }); var paginationMetadata = new { totalCount = albums.TotalCount, pageSize = albums.PageSize, currentPage = albums.CurrentPage, totalPages = albums.TotalPages, previousPageLink = previousPage, nextPageLink = nextPage }; return(Ok(new { Values = mappedAlbums, Links = paginationMetadata })); }
public PagedList <PlaylistAlbum> GetAllPagination(PlaylistAlbumResourceParameters resourceParams) { var collectionBeforePaging = _context.PlaylistAlbums .Include(t => t.Tracks) .OrderBy(t => t.UserName) .AsQueryable(); //filter by userName if (!string.IsNullOrEmpty(resourceParams.UserName)) { collectionBeforePaging = collectionBeforePaging .Where(a => a.UserName == resourceParams.UserName); } //filter by type if type exists if (!string.IsNullOrEmpty(resourceParams.Type)) { collectionBeforePaging = collectionBeforePaging .Where(a => a.Type == resourceParams.Type); } //filter by name if name =||= 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) || a.Type.Contains(resourceParams.SearchQuery)); } return(PagedList <PlaylistAlbum> .Create(collectionBeforePaging, resourceParams.PageNumber, resourceParams.PageSize)); }