Пример #1
0
        public async Task <IActionResult> Get([FromQuery] AlbumResourceParameters resourceParameters)
        {
            var albums       = _albumRepo.GetAllPagination(resourceParameters);
            var mappedAlbums = _mapper.Map <IEnumerable <AlbumDto> >(albums);

            //Construct links to previous+ next page
            var previousPage = albums.HasPrevious ?
                               _albumLinkService.CreateResourceUri(resourceParameters, ResourceType.PreviousPage) : null;

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

            mappedAlbums = mappedAlbums.Select(album =>
            {
                album = _albumLinkService.CreateLinks(album);

                return(album);
            });

            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
            }));
        }
Пример #2
0
        public PagedList <Album> GetAllPagination(AlbumResourceParameters resourceParams)
        {
            var collectionBeforPaging = _context.Albums
                                        .Include(t => t.Tracks)
                                        .OrderBy(t => t.Name)
                                        .AsQueryable();

            //filter by type if type exists
            if (!string.IsNullOrEmpty(resourceParams.Type))
            {
                collectionBeforPaging = collectionBeforPaging
                                        .Where(a => a.Type == resourceParams.Type);
            }

            //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) ||
                                               a.Type.Contains(resourceParams.SearchQuery));
            }

            return(PagedList <Album> .Create(collectionBeforPaging, resourceParams.PageNumber, resourceParams.PageSize));
        }