public ActionResult <PagedCollectionResponse <BookDto> > GetBooks([FromQuery] BooksFilterModel filter) { //Get the data for the current page var result = new PagedCollectionResponse <BookDto> { Items = _bookService.GetFilteredBooks(filter) }; if (!result.Items.Any()) { return(NoContent()); } //Get next page URL string BooksFilterModel nextFilter = filter.Clone() as BooksFilterModel; nextFilter.Page += 1; string nextUrl = !_bookService.GetFilteredBooks(nextFilter).Any() ? null : Url.Action("GetBooks", null, nextFilter, Request.Scheme); //Get previous page URL string BooksFilterModel previousFilter = filter.Clone() as BooksFilterModel; previousFilter.Page -= 1; string previousUrl = previousFilter.Page <= 0 ? null : Url.Action("GetBooks", null, previousFilter, Request.Scheme); result.NextPage = !IsNullOrWhiteSpace(nextUrl) ? new Uri(nextUrl) : null; result.PreviousPage = !IsNullOrWhiteSpace(previousUrl) ? new Uri(previousUrl) : null; return(Ok(result)); }
public IEnumerable <BookDto> GetFilteredBooks(BooksFilterModel filterModel) { //Filtering logic var books = _db.Books.Include(b => b.Author) .Include(b => b.Publisher) .Where(b => b.Title.StartsWith(filterModel.Title ?? Empty, InvariantCultureIgnoreCase)) .Where(b => b.Author.Name.StartsWith(filterModel.AuthorName ?? Empty, InvariantCultureIgnoreCase)) .Skip((filterModel.Page - 1) * filterModel.Limit) .Take(filterModel.Limit); return(_mapper.Map <IEnumerable <Book>, IEnumerable <BookDto> >(books)); }