private string CreateAuthorsRecourceUri(AuthorsParam authorsParam, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetAuthors", new { orderBy = authorsParam.OrderBy, pageNumber = authorsParam.PageNumber - 1, pageSize = authorsParam.PageSize, mainCategory = authorsParam.MainCategory, searchQuery = authorsParam.SearchQuery })); case ResourceUriType.NextPage: return(Url.Link("GetAuthors", new { orderBy = authorsParam.OrderBy, pageNumber = authorsParam.PageNumber + 1, pageSize = authorsParam.PageSize, mainCategory = authorsParam.MainCategory, searchQuery = authorsParam.SearchQuery })); default: return(Url.Link("GetAuthors", new { orderBy = authorsParam.OrderBy, pageNumber = authorsParam.PageNumber, pageSize = authorsParam.PageSize, mainCategory = authorsParam.MainCategory, searchQuery = authorsParam.SearchQuery })); } }
public IActionResult GetAuthors([FromQuery] AuthorsParam authorsParam) { if (!_mappingService.ValidMappingExistFor <AuthorsDto, Author>(authorsParam.OrderBy)) { return(BadRequest()); } var authorsFromRepo = _courseLibraryRepository.GetAuthors(authorsParam); if (authorsFromRepo == null) { return(NotFound()); } var previousPageLink = authorsFromRepo.HasPreviousPage ? CreateAuthorsRecourceUri(authorsParam, ResourceUriType.PreviousPage) : null; var nextPageLink = authorsFromRepo.HasNextPage ? CreateAuthorsRecourceUri(authorsParam, ResourceUriType.NextPage) : null; var paginationMataDate = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPage = authorsFromRepo.TotalPages, previousPageLink, nextPageLink }; Response.Headers.Add("x-Pagination", JsonSerializer.Serialize(paginationMataDate)); return(Ok(_mapper.Map <IEnumerable <AuthorsDto> >(authorsFromRepo))); }
public PagedList <Author> GetAuthors(AuthorsParam authorsParam) { if (authorsParam == null) { throw new ArgumentNullException(nameof(authorsParam)); } var authorsCollection = _courseLibraryContext.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(authorsParam.MainCategory)) { var mainCategory = authorsParam.MainCategory.Trim(); authorsCollection = authorsCollection.Where(x => x.MainCategory.Contains(mainCategory)); } if (!string.IsNullOrEmpty(authorsParam.SearchQuery)) { var searchQuery = authorsParam.SearchQuery.Trim(); authorsCollection = authorsCollection.Where(x => x.FirstName.Contains(searchQuery) || x.LastName.Contains(searchQuery) || x.MainCategory.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(authorsParam.OrderBy)) { var authorPropertyMappingDictionary = _mappingService.GetPropertyMapping <AuthorsDto, Author>(); authorsCollection = authorsCollection.ApplySort(authorsParam.OrderBy, authorPropertyMappingDictionary); } return(PagedList <Author> .Create(authorsCollection, authorsParam.PageNumber, authorsParam.PageSize)); }