Exemple #1
0
        public PagedList <Author> GetAllAuthors(PagingResourceParameters paging)
        {
            var query = (IQueryable <Author>)db.Authors
                        .Include(x => x.Books)
                        .OrderBy(author => author.Name);

            if (!string.IsNullOrEmpty(paging.SearchQuery))
            {
                query = query.Where(x => x.Name.ToLowerInvariant().Contains(paging.SearchQuery.ToLowerInvariant()));
            }

            return(PagedList <Author> .Create(query, paging.PageNumber, paging.PageSize));
        }
Exemple #2
0
        public PagedList <Book> GetAllBooksByAuthor(int authorId, PagingResourceParameters paging)
        {
            var query = db.Books
                        .AsNoTracking()
                        .Where(x => x.AuthorId == authorId)
                        .OrderBy(book => book.Title).AsQueryable();

            if (!string.IsNullOrEmpty(paging.SearchQuery))
            {
                query = query.Where(x => x.Title.ToLowerInvariant().Contains(paging.SearchQuery.ToLowerInvariant()));
            }

            return(PagedList <Book> .Create(query, paging.PageNumber, paging.PageSize));
        }
Exemple #3
0
        public LinkedCollectionResourceWrapperDTO <AuthorDTO> GetAuthorsCollection(PagingResourceParameters paging)
        {
            var authors         = authorRepository.GetAllAuthors(paging);
            var authorsToReturn = mapper.Map <IEnumerable <AuthorDTO> >(authors);

            authorsToReturn = authorsToReturn.Select(author =>
            {
                author = createLinksStrategy.CreateLinksForAuthorResource(author);
                return(author);
            });
            var authorsWrapper = new LinkedCollectionResourceWrapperDTO <AuthorDTO>(authorsToReturn);

            return(createLinksStrategy.CreateLinksForAuthors(authorsWrapper, paging, authors.HasPrevious, authors.HasNext));
        }
Exemple #4
0
        public LinkedCollectionResourceWrapperDTO <BookDTO> GetBooks(int authorId, PagingResourceParameters paging)
        {
            var books         = booksRepository.GetAllBooksByAuthor(authorId, paging);
            var booksToReturn = mapper.Map <IEnumerable <BookDTO> >(books);

            booksToReturn = booksToReturn.Select(book =>
            {
                book = createLinksStrategy.CreateLinksForBookResource(book);
                return(book);
            });

            var wrapper = new LinkedCollectionResourceWrapperDTO <BookDTO>(booksToReturn);

            return(createLinksStrategy.CreateLinksForBooks(wrapper, paging, books.HasPrevious, books.HasNext));
        }
Exemple #5
0
        public LinkedCollectionResourceWrapperDTO <BookDTO> CreateLinksForBooks(
            LinkedCollectionResourceWrapperDTO <BookDTO> booksWrapper,
            PagingResourceParameters pagingResourceParameters,
            bool hasPrevious,
            bool hasNext)
        {
            booksWrapper.Links = new List <LinkDTO>();

            booksWrapper.Links.Add(new LinkDTO(
                                       href: urlHelper.Link("GetBooks", new { }),
                                       rel: "self",
                                       method: "GET"));

            if (hasPrevious)
            {
                booksWrapper.Links.Add(new LinkDTO(
                                           href: urlHelper.Link("GetBooks", new
                {
                    searchQuery = pagingResourceParameters.SearchQuery,
                    pageNumber  = pagingResourceParameters.PageNumber - 1,
                    pageSize    = pagingResourceParameters.PageSize
                }),
                                           rel: "previous",
                                           method: "GET"));
            }

            if (hasNext)
            {
                booksWrapper.Links.Add(new LinkDTO(
                                           href: urlHelper.Link("GetBooks", new
                {
                    searchQuery = pagingResourceParameters.SearchQuery,
                    pageNumber  = pagingResourceParameters.PageNumber + 1,
                    pageSize    = pagingResourceParameters.PageSize
                }),
                                           rel: "next",
                                           method: "GET"));
            }

            return(booksWrapper);
        }
Exemple #6
0
        public IActionResult Get([FromQuery] PagingResourceParameters paging)
        {
            var authorsToReturn = getAuthorsStrategy.GetAuthorsCollection(paging);

            return(Ok(authorsToReturn));
        }
Exemple #7
0
        public IActionResult Get(int authorId, [FromQuery] PagingResourceParameters paging)
        {
            var books = getBooksStrategy.GetBooks(authorId, paging);

            return(Ok(books));
        }