//[HttpHead] public ActionResult <IEnumerable <AuthorDto> > GetAuthors([FromQuery] AuthorFilters authorFilters) { try { var authorFromRepo = _courseLibraryRepository.GetAuthors(authorFilters); var previousPageLink = authorFromRepo.HasPrevious ? CreateAuthorResourceUri(authorFilters, ResourceUriType.PreviousPage) : null; var nextPageLink = authorFromRepo.HasPrevious ? CreateAuthorResourceUri(authorFilters, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = authorFromRepo.TotalCount, pageSize = authorFilters.PageSize, currentPage = authorFromRepo.CurrentPage, totalPages = authorFromRepo.TotalPages, previousPageLink, nextPageLink }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorFromRepo))); } catch (Exception ex) { _logger.LogError(ex, $"{ex.Message}"); return(StatusCode(500, "Internal server error, try again later.")); } }
public PagedList <Author> GetAuthors(AuthorFilters authorFilters) { var collection = _context.Authors as IQueryable <Author>; if (authorFilters == null) { throw new ArgumentNullException(nameof(authorFilters)); } if (!string.IsNullOrWhiteSpace(authorFilters.MainCategory)) { authorFilters.MainCategory = authorFilters.MainCategory.Trim(); collection = collection.Where(m => m.MainCategory.Contains(authorFilters.MainCategory)); } if (!string.IsNullOrWhiteSpace(authorFilters.SearchQuery)) { authorFilters.SearchQuery = authorFilters.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(authorFilters.SearchQuery) || a.FirstName.Contains(authorFilters.SearchQuery) || a.LastName.Contains(authorFilters.SearchQuery)); } if (!string.IsNullOrEmpty(authorFilters.OrderBy)) { var authorPropertyMappingDictionary = _propertyMappingService.GetPropertyMapping <AuthorDto, Author>(); collection = collection.ApplySort(authorFilters.OrderBy, authorPropertyMappingDictionary); } return(PagedList <Author> .Create(collection, authorFilters.PageNumber, authorFilters.PageSize)); }
public string CreateAuthorResourceUri(AuthorFilters authorsResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetAuthors", new { pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize, mainCategory = authorsResourceParameters.MainCategory, searchQuery = authorsResourceParameters.SearchQuery })); case ResourceUriType.NextPage: return(Url.Link("GetAuthors", new { pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize, mainCategory = authorsResourceParameters.MainCategory, searchQuery = authorsResourceParameters.SearchQuery })); case ResourceUriType.Current: default: return(Url.Link("GetAuthors", new { pageNumber = authorsResourceParameters.PageNumber - 1, pageSize = authorsResourceParameters.PageSize, mainCategory = authorsResourceParameters.MainCategory, searchQuery = authorsResourceParameters.SearchQuery })); } }