Ejemplo n.º 1
0
        public ActionResult <IEnumerable <AuthorDto> > GetAuthors(authorsResourceParameters paging)
        {
            var authorsFromRepo  = _libraryRepository.GetAuthors(paging);
            var previousPageLink = authorsFromRepo.HasPrevious ? CreateAuthorResourceUri(paging, ResourceUriType.PreviousPage) : null;

            var nextPageLink       = authorsFromRepo.HasNext ? CreateAuthorResourceUri(paging, ResourceUriType.NextPage) : null;
            var paginationMetaData = new
            {
                totalCount       = authorsFromRepo.TotalCount,
                pageSize         = authorsFromRepo.PageSize,
                currentPage      = authorsFromRepo.CurrentPages,
                totalPages       = authorsFromRepo.TotalPages,
                previousPageLink = previousPageLink,
                nextPageLink     = nextPageLink
            };


            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetaData));


            var authors = Mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo);

            if (authors == null)
            {
                return(NotFound());
            }
            return(Ok(authors));
        }
Ejemplo n.º 2
0
        private string CreateAuthorResourceUri(authorsResourceParameters paging, ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(_urlHelper.Link("GetAuthors", new
                {
                    SearchQuery = paging.SearchQuery,
                    genre = paging.Genre,
                    pageNumber = paging.PageNumber - 1,
                    pageSize = paging.PageSize
                }));

            case ResourceUriType.NextPage:
                return(_urlHelper.Link("GetAuthors", new
                {
                    SearchQuery = paging.SearchQuery,
                    genre = paging.Genre,
                    pageNumber = paging.PageNumber + 1,
                    pageSize = paging.PageSize
                }));

            default:
                return(_urlHelper.Link("GetAuthors", new
                {
                    SearchQuery = paging.SearchQuery,
                    genre = paging.Genre,
                    pageNumber = paging.PageNumber,
                    pageSize = paging.PageSize
                }));
            }
        }
Ejemplo n.º 3
0
        public PagedList <Author> GetAuthors(authorsResourceParameters paging)
        {
            //Fetch the Data
            var collectionbeforePaging = _context.Authors.OrderBy(a => a.FirstName).ThenBy(a => a.LastName).AsQueryable();

            //
            if (!string.IsNullOrEmpty(paging.Genre))
            {
                var genreForwhereClause = paging.Genre.Trim().ToLowerInvariant();
                collectionbeforePaging = collectionbeforePaging.Where(a => a.Genre.ToLowerInvariant() == genreForwhereClause);
            }
            if (!string.IsNullOrEmpty(paging.SearchQuery))
            {
                var SearchQueryForwhereClause = paging.SearchQuery.Trim().ToLowerInvariant();
                collectionbeforePaging = collectionbeforePaging.Where(
                    a => a.FirstName.ToLowerInvariant().Contains(SearchQueryForwhereClause) ||
                    a.LastName.Contains(SearchQueryForwhereClause)
                    );
            }

            //return the data back with paging
            return(PagedList <Author> .Create(collectionbeforePaging, paging.PageNumber, paging.PageSize));
        }