//public IEnumerable<AuthorDto> GetAuthors(AuthorResourceParameters authorResourceParameters) public PagedList <Author> GetAuthors(AuthorResourceParameters authorResourceParameters) { if (!string.IsNullOrWhiteSpace(authorResourceParameters.OrderBy)) { if (!_propertyMapper.ValidMappingExistsFor <AuthorDto, Author>(authorResourceParameters.OrderBy)) { return(null); } } var x = _repo.GetAuthors(authorResourceParameters); //var previousPageLink = x.HasPrevious ? CreateAuthorsResourceUri(authorResourceParameters, ResourceUriType.PreviousPage) : null; //var nextPageLink = x.HasNext ? CreateAuthorsResourceUri(authorResourceParameters, ResourceUriType.NextPage) : null; var pageMetaData = new { totalCount = x.TotalCount, pageSize = x.PageSize, currentPage = x.CurrentPage, totalPages = x.TotalPages }; _urlHelper.ActionContext.HttpContext.Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(pageMetaData)); return(x);// _mapper.Map<IEnumerable<AuthorDto>>(x); }
public IEnumerable <Author> GetAuthors(AuthorResourceParameters parameters) { if (string.IsNullOrWhiteSpace(parameters.MainCategory) && string.IsNullOrWhiteSpace(parameters.SearchQuery)) { return(GetAuthors()); } var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(parameters.MainCategory)) { parameters.MainCategory = parameters.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == parameters.MainCategory); } if (!string.IsNullOrWhiteSpace(parameters.SearchQuery)) { parameters.SearchQuery = parameters.SearchQuery.Trim(); collection = collection .Where(a => a.MainCategory.Contains(parameters.SearchQuery) || a.FirstName.Contains(parameters.SearchQuery) || a.LastName.Contains(parameters.SearchQuery) ); } return(collection); }
public IEnumerable <Author> GetAuthors(AuthorResourceParameters authorResourceParameters) { var mainCategory = authorResourceParameters.MainCategory; var searchQuery = authorResourceParameters.SearchQuery; if (string.IsNullOrWhiteSpace(mainCategory) && string.IsNullOrWhiteSpace(searchQuery)) { return(GetAuthors()); } var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(mainCategory)) { mainCategory = mainCategory.Trim(); collection = collection.Where(author => author.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(searchQuery)) { collection = collection.Where(author => author.MainCategory.Contains(searchQuery) || author.FirstName.Contains(searchQuery) || author.LastName.Contains(searchQuery)); } return(collection.ToList()); }
public IEnumerable <Author> GetAuthors(AuthorResourceParameters authorResourceParameters) { if (authorResourceParameters == null) { throw new ArgumentNullException(nameof(authorResourceParameters)); } if (string.IsNullOrEmpty(authorResourceParameters.MainCategory) && string.IsNullOrEmpty(authorResourceParameters.SearchCategory)) { return(GetAuthors()); } var authorCollection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrEmpty(authorResourceParameters.MainCategory)) { var mainCategory = authorResourceParameters.MainCategory.Trim(); authorCollection = authorCollection.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrEmpty(authorResourceParameters.SearchCategory)) { var searchQuery = authorResourceParameters.SearchCategory.Trim(); authorCollection = authorCollection.Where( a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } return(authorCollection.ToList()); }
private string CreateAuthorResourceUri(AuthorResourceParameters authorResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetAuthors", new { pageNumber = authorResourceParameters.pageNo - 1, pageSize = authorResourceParameters.PageSize, mainCategory = authorResourceParameters.mainCategory, searchQuery = authorResourceParameters.searchQuery })); // break; case ResourceUriType.NextPage: return(Url.Link("GetAuthors", new { pageNumber = authorResourceParameters.pageNo + 1, pageSize = authorResourceParameters.PageSize, mainCategory = authorResourceParameters.mainCategory, searchQuery = authorResourceParameters.searchQuery })); default: return(Url.Link("GetAuthors", new { pageNumber = authorResourceParameters.pageNo, pageSize = authorResourceParameters.PageSize, mainCategory = authorResourceParameters.mainCategory, searchQuery = authorResourceParameters.searchQuery })); } }
public IEnumerable <Author> GetAuthors(AuthorResourceParameters authorsResourceParameters) { if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } if (string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory) && string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { return(GetAuthors()); } //this will cast the collection to either use the filter or search or both var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory)) { var mainCategory = authorsResourceParameters.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { var searchQuery = authorsResourceParameters.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } return(collection.ToList()); }
private IEnumerable <LinkDto> CreateLinksForAuthors( AuthorResourceParameters authorResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDto>(); // self links.Add(new LinkDto( CreateAuthorResourceUri(authorResourceParameters, ResourceUriType.Current), "self", "GET")); if (hasNext) { links.Add(new LinkDto( CreateAuthorResourceUri(authorResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add(new LinkDto( CreateAuthorResourceUri(authorResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
public async Task <ActionResult <IEnumerable <AuthorDto> > > GetAuthorsAsync( [FromQuery] AuthorResourceParameters parameters) { var pagedList = await RepositoryWrapper.Author.GetAllAsync(parameters); var paginationMetadata = new { totalCount = pagedList.TotalCount, pageSize = pagedList.PageSize, currentPage = pagedList.CurrentPage, totalPages = pagedList.TotalPages, previousePageLink = pagedList.HasPrevious ? Url.Link(nameof(GetAuthorsAsync), new { pageNumber = pagedList.CurrentPage - 1, pageSize = pagedList.PageSize, birthPlace = parameters.BirthPlace, serachQuery = parameters.SearchQuery, sortBy = parameters.SortBy, }) : null, nextPageLink = pagedList.HasNext ? Url.Link(nameof(GetAuthorsAsync), new { pageNumber = pagedList.CurrentPage + 1, pageSize = pagedList.PageSize, birthPlace = parameters.BirthPlace, serachQuery = parameters.SearchQuery, sortBy = parameters.SortBy, }) : null }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var authorDtoList = Mapper.Map <IEnumerable <AuthorDto> >(pagedList); return(authorDtoList.ToList()); }
public PagedList <Author> GetAuthors(AuthorResourceParameters authorResourceParameters) { //var beforePaging = _context // .Authors // .OrderBy(a => a.FirstName) // .ThenBy(a => a.LastName). AsQueryable ( ); var beforePaging = _context.Authors.ApplySort(authorResourceParameters.OrderBy, _propertyService.GetPropertyMapping <AuthorDto, Author>()); if (!string.IsNullOrEmpty(authorResourceParameters.Genre)) { beforePaging = beforePaging.Where(x => x.Genre.ToLowerInvariant( ) == authorResourceParameters.Genre.Trim().ToLowerInvariant( )); } if (!string.IsNullOrEmpty(authorResourceParameters.SearchQuery)) { var searchQueryForWhereClause = authorResourceParameters.SearchQuery.Trim().ToLowerInvariant(); beforePaging = beforePaging.Where( x => x.Genre.ToLowerInvariant( ).Contains(searchQueryForWhereClause) || x.FirstName.ToLowerInvariant( ).Contains(searchQueryForWhereClause) || x.LastName.ToLowerInvariant( ).Contains(searchQueryForWhereClause) ); } return(PagedList <Author> .Create(beforePaging, authorResourceParameters.PageNumber, authorResourceParameters.PageSize)); }
public PagedList <Author> GetAuthors(AuthorResourceParameters authorResource) { if (authorResource == null) { throw new ArgumentNullException(nameof(authorResource)); } //if (string.IsNullOrWhiteSpace(authorResource.MainCategory) && string.IsNullOrWhiteSpace(authorResource.SearchQuery)) //{ // return GetAuthors(); //} var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(authorResource.MainCategory)) { var mainCategory = authorResource.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(authorResource.SearchQuery)) { var searchQuery = authorResource.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } //Adding pagination return(PagedList <Author> .Create(collection, authorResource.PageNumber, authorResource.PageSize)); }
public IActionResult GetAuthors([FromQuery] AuthorResourceParameters authorsResourceParameters) { if (!_propertyCheck.TypeHasProperties <AuthorDto>(authorsResourceParameters.Fields)) { return(BadRequest()); } var x = _manager.GetAuthors(authorsResourceParameters); if (x == null) { return(NotFound()); } var links = CreateLinksForAuthors(authorsResourceParameters, x.HasNext, x.HasPrevious); var shapedAuthors = _mapper.Map <IEnumerable <AuthorDto> >(x).ShapeData(authorsResourceParameters.Fields); var shapedAuthorsWithLinks = shapedAuthors.Select(author => { var authorAsDictionary = author as IDictionary <string, object>; var authorLinks = CreateLinksForAuthor((Guid)authorAsDictionary["Id"], null); authorAsDictionary.Add("links", authorLinks); return(authorAsDictionary); }); var LinkedCollectionResource = new { value = shapedAuthorsWithLinks, links }; return(Ok(LinkedCollectionResource)); }
public ActionResult <IEnumerable <AuthorDTO> > GetAuthors( [FromQuery] AuthorResourceParameters authorResourceParameters) { var authorsFromRepo = _courseLibraryRepository.GetAuthors(authorResourceParameters); return(Ok(_mapper.Map <IEnumerable <AuthorDTO> >(authorsFromRepo))); }
public PagedList <Author> GetAuthors(AuthorResourceParameters authorResourceParameters) { var collectionBeforePaging = _context.Authors.ApplySort(authorResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <AuthorDto, Author>()); if (!string.IsNullOrEmpty(authorResourceParameters.Genre)) { var genreForWhereClause = authorResourceParameters.Genre.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging.Where(a => a.Genre.ToLowerInvariant() == genreForWhereClause); } if (!string.IsNullOrEmpty(authorResourceParameters.SearchQuery)) { var searchQueryForWhereClause = authorResourceParameters.SearchQuery.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(a => a.Genre.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) || a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause)); } return(PagedList <Author> .Create(collectionBeforePaging, authorResourceParameters.PageNumber, authorResourceParameters.PageSize)); }
private ResourceCollection <AuthorDto> CreateLinksForAuthors(ResourceCollection <AuthorDto> authors, AuthorResourceParameters parameters = null, dynamic paginationData = null) { authors.Links.Add(new Link(HttpMethods.Get, "self", Url.Link(nameof(GetAuthorsAsync), parameters))); authors.Links.Add(new Link(HttpMethods.Post, "create author", Url.Link(nameof(CreateAuthorAsync), null))); if (paginationData != null) { if (paginationData.previousePageLink != null) { authors.Links.Add(new Link(HttpMethods.Get, "previous page", paginationData.previousePageLink)); } if (paginationData.nextPageLink != null) { authors.Links.Add(new Link(HttpMethods.Get, "next page", paginationData.nextPageLink)); } } return(authors); }
public async Task <PagedList <Author> > GetAllAsync(AuthorResourceParameters parameters) { var queryableAuthors = DbContext.Set <Author>().AsQueryable(); if (!string.IsNullOrWhiteSpace(parameters.BirthPlace)) //过滤 { queryableAuthors = queryableAuthors.Where("BirthPlace =@0", parameters.BirthPlace); //用dynamic linq // queryableAuthors = queryableAuthors.Where(m => m.BirthPlace.ToLower() == parameters.BirthPlace); } if (!string.IsNullOrWhiteSpace(parameters.SearchQuery))//搜索 { queryableAuthors = queryableAuthors.Where(m => m.Name.ToLower().Contains(parameters.SearchQuery.ToLower()) || m.BirthPlace.ToLower().Contains(parameters.SearchQuery.ToLower())); } // using System.Linq.Dynamic.Core必须引用 queryableAuthors = queryableAuthors.AsQueryable().OrderBy(parameters.Sortby); var totalCount = queryableAuthors.Count(); var orderedAuthors = queryableAuthors.Sort(parameters.Sortby, mappingDict); var items = await queryableAuthors.Skip((parameters.PageNumber - 1) *parameters.PageSize) .Take(parameters.PageSize).ToListAsync(); return(new PagedList <Author>(items, totalCount, parameters.PageNumber, parameters.PageSize)); }
//public ActionResult<IEnumerable<AuthorDto>> GetAuthors(string mainCategory, string searchQuery) public ActionResult <IEnumerable <AuthorDto> > GetAuthors([FromQuery] AuthorResourceParameters authorResource) { var authorsFromRepo = _courseLibraryRepository.GetAuthors(authorResource); //Map authors to Dto with Automapper return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo))); }
public Task <PagedList <Author> > GetAllAsync(AuthorResourceParameters parameters) { IQueryable <Author> queryableAuthors = DbContext.Set <Author>(); if (!string.IsNullOrWhiteSpace(parameters.BirthPlace)) { queryableAuthors = queryableAuthors.Where(m => m.BirthPlace.ToLower() == parameters.BirthPlace); } if (!string.IsNullOrWhiteSpace(parameters.SearchQuery)) { queryableAuthors = queryableAuthors.Where( m => m.BirthPlace.ToLower().Contains(parameters.SearchQuery.ToLower()) || m.Name.ToLower().Contains(parameters.SearchQuery.ToLower())); } //return PagedList<Author>.Create(queryableAuthors, parameters.PageNumber, parameters.PageSize); //queryableAuthors = queryableAuthors.OrderBy(parameters.SortBy); //return PagedList<Author>.Create(queryableAuthors, parameters.PageNumber, parameters.PageSize); var orderedAuthors = queryableAuthors.Sort(parameters.SortBy, mappingDict); return(PagedList <Author> .CreateAsync(orderedAuthors, parameters.PageNumber, parameters.PageSize)); }
public PagedList <Author> GetAuthors(AuthorResourceParameters authorsResourceParameters) { if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } var collection = _context.Authors as IQueryable <Author>; if (!string.IsNullOrWhiteSpace(authorsResourceParameters.MainCategory)) { var mainCategory = authorsResourceParameters.MainCategory.Trim(); collection = collection.Where(a => a.MainCategory == mainCategory); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.SearchQuery)) { var searchQuery = authorsResourceParameters.SearchQuery.Trim(); collection = collection.Where(a => a.MainCategory.Contains(searchQuery) || a.FirstName.Contains(searchQuery) || a.LastName.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(authorsResourceParameters.OrderBy)) { var _authorPropertyMappingDictionary = _propertyMappingService.GetPropertyMapping <AuthorDto, Author>(); collection = collection.ApplySort(authorsResourceParameters.OrderBy, _authorPropertyMappingDictionary); } return(PagedList <Author> .Create(collection, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
//public async Task<IActionResult> GetAuthors() //{ // //throw new Exception("fault"); // var authorsFromRepo = await _CourseLibraryRepository.GetAuthors(); // return Ok(_mapper.Map<IEnumerable<AuthorDto>>(authorsFromRepo)); //} public IActionResult GetAuthors([FromQuery] AuthorResourceParameters authorResourceParameters) { //throw new Exception("fault"); var authorsFromRepo = _CourseLibraryRepository.GetAuthors(authorResourceParameters); return(Ok(_mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo))); }
public void GetAuthors() { var para = new AuthorResourceParameters { City = "Salt Lake City", State = "UT" }; var res = _authorService.GetAuthors(para); Assert.IsType <List <AuthorDto> >(res); }
public void GetAuthorsFiltered() { AuthorResourceParameters resourceParameters = new AuthorResourceParameters { State = "CA", City = "Oakland" }; var result = _auRepo.GetAuthors(resourceParameters); Assert.IsType <List <Author> >(result); Assert.NotEmpty(result); }
public void GetAuthorFiltered() { var para = new AuthorResourceParameters { City = "Salt Lake City", State = "UT" }; var res = _authorService.GetAuthors(para); var resp = _authorService.GetAuthor(res.First().Id); Assert.IsType <AuthorDto>(resp); }
public void AuthorExists() { var para = new AuthorResourceParameters { City = "Salt Lake City", State = "UT" }; var res = _authorService.GetAuthors(para); var resp = _authorService.AuthorExists(res.First().Id); Assert.True(resp); }
public ActionResult <IEnumerable <AuthorDTO> > GetAuthors([FromQuery] AuthorResourceParameters authorResourceParameters) { var authors = _courseLibraryRepository.GetAuthors(authorResourceParameters); var previousPageLink = authors.HasPrevious ? CreateAuthorResourceUri(authorResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = authors.HasNext ? CreateAuthorResourceUri(authorResourceParameters, ResourceUriType.NextPage) : null; var paginationMetaData = new { totalCount = authors.TotalCount, pageSize = authors.PageSize, currentPage = authors.CurrentPage, totalPages = authors.TotalPages, previousPageLink = previousPageLink, nextPageLink = nextPageLink }; Response.Headers.Add("X-Pagination", System.Text.Json.JsonSerializer.Serialize(paginationMetaData)); return(Ok(_mapper.Map <IEnumerable <AuthorDTO> >(authors))); //throw new Exception("Some this"); //var authorsList = _courseLibraryRepository.GetAuthors(); //var Authors = new List<AuthorDTO>(); //return Ok(_mapper.Map<IEnumerable<AuthorDTO>>(authorsList)); // Now we are going to user Mapper //foreach (var author in authorsList) //{ // Authors.Add(new AuthorDTO() // { // Id= author.Id, // Name= $"{author.FirstName} {author.LastName}", // MainCategory=author.MainCategory, // Age=author.DateOfBirth.GetCurrentAge() // this static function returns age // }); //} //return new JsonResult(Authors); }
public async Task Test_GetAllAuthors() { // Arrange var author = new Author { Id = Guid.NewGuid(), Name = "Author Test 1", Email = "*****@*****.**", BirthPlace = "Beijing" }; var authorDto = new AuthorDto() { Id = author.Id, Name = author.Name, BirthPlace = author.BirthPlace, Email = author.Email }; var authorList = new List <Author> { author }; var authorDtoList = new List <AuthorDto> { authorDto }; var parameters = new AuthorResourceParameters(); var authors = new PagedList <Author>(authorList, totalCount: authorList.Count, pageNumber: parameters.PageNumber, pageSize: parameters.PageSize); _mockRepositoryWrapper.Setup(m => m.Author.GetAllAsync(It.IsAny <AuthorResourceParameters>())) .Returns(Task.FromResult(authors)); _mockMapper.Setup(m => m.Map <IEnumerable <AuthorDto> >(It.IsAny <IEnumerable <Author> >())) .Returns(authorDtoList); _mockUrlHelper.Setup(m => m.Link(It.IsAny <string>(), It.IsAny <object>())) .Returns("demo url"); _authorController.Url = _mockUrlHelper.Object; // Act var actionResult = await _authorController.GetAuthorsAsync(parameters); // Assert ResourceCollection <AuthorDto> resourceCollection = actionResult.Value; Assert.True(1 == resourceCollection.Items.Count); Assert.Equal(authorDto, resourceCollection.Items[0]); Assert.True(_authorController.Response.Headers.ContainsKey("X-Pagination")); }
public async Task <PagedList <AuthorViewModel> > GetAuthorPage([FromQuery] AuthorResourceParameters parameters) { var authors = await RepositoryWrapper.Author.GetAllAsync(); if (!string.IsNullOrEmpty(parameters.BirthPlace)) { authors = authors.Where(x => x.BirthPlace == parameters.BirthPlace).AsEnumerable(); } int totalCount = authors.Count(); var authorsPage = authors.AsQueryable().OrderBy(parameters.SortBy).Skip((parameters.PageNumber - 1) * parameters.PageSize) .Take(parameters.PageSize); var authorViewModels = Mapper.Map <IEnumerable <AuthorViewModel> >(authorsPage); return(new PagedList <AuthorViewModel>(authorViewModels.ToList(), totalCount, parameters.PageNumber, parameters.PageSize)); }
public IActionResult GetAuthors([FromQuery] AuthorResourceParameters authorResourceParameters) { if (!_propertyMappingService.ValidMappingExistisFor <AuthorDto, Author>(authorResourceParameters.OrderBy)) { return(BadRequest()); } if (!_propertyCheckerService.TypeHasProperties <AuthorDto>(authorResourceParameters.Fields)) { return(BadRequest()); } var authorsFromRepo = _courseLibraryRepository.GetAuthors(authorResourceParameters); var paginationMetadata = new { totalCount = authorsFromRepo.TotalCount, pageSize = authorsFromRepo.PageSize, currentPage = authorsFromRepo.CurrentPage, totalPages = authorsFromRepo.TotalPages, }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForAuthors(authorResourceParameters, authorsFromRepo.HasNext, authorsFromRepo.HasPrevious); var shapeAuthors = _mapper.Map <IEnumerable <AuthorDto> >(authorsFromRepo).ShapeData(authorResourceParameters.Fields); var shapedAuthorsWithLinks = shapeAuthors.Select(author => { var authorAsDictionary = author as IDictionary <string, object>; var authorLinks = CreateLinksForAuthor((Guid)authorAsDictionary["Id"], null); authorAsDictionary.Add("links", authorLinks); return(authorAsDictionary); }); var linkedCollectionResource = new { value = shapedAuthorsWithLinks, links }; return(Ok(linkedCollectionResource)); }
public Task <PagedList <Author> > GetAllAsync(AuthorResourceParameters parameters) { IQueryable <Author> authors = Table; if (!string.IsNullOrWhiteSpace(parameters.BirthPlace)) { authors = Table.Where(m => m.BirthPlace.ToLower() == parameters.BirthPlace.ToLower()); } if (!string.IsNullOrWhiteSpace(parameters.SearchQuery)) { authors = authors.Where(m => m.BirthPlace.ToLower().Contains(parameters.SearchQuery.ToLower()) || m.Name.ToLower().Contains(parameters.SearchQuery.ToLower() )); } var orderAuthors = authors.Sort(parameters.SortBy, mappingDict); return(PagedList <Author> .CreateAsync(orderAuthors, parameters.PageNumber, parameters.PageSize)); }
public string CreateAuthorsResourceUri( AuthorResourceParameters authorResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetAuthors", new { fields = authorResourceParameters.Fields, orderBy = authorResourceParameters.OrderBy, pageNumber = authorResourceParameters.PageNumber - 1, pageSize = authorResourceParameters.PageSize, mainCategory = authorResourceParameters.MainCategory, searchQuery = authorResourceParameters.SearchQuery })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetAuthors", new { fields = authorResourceParameters.Fields, orderBy = authorResourceParameters.OrderBy, pageNumber = authorResourceParameters.PageNumber + 1, pageSize = authorResourceParameters.PageSize, mainCategory = authorResourceParameters.MainCategory, searchQuery = authorResourceParameters.SearchQuery })); case ResourceUriType.Current: default: return(_urlHelper.Link("GetAuthors", new { fields = authorResourceParameters.Fields, orderBy = authorResourceParameters.OrderBy, pageNumber = authorResourceParameters.PageNumber, pageSize = authorResourceParameters.PageSize, mainCategory = authorResourceParameters.MainCategory, searchQuery = authorResourceParameters.SearchQuery })); } }
[HttpGet(Name = nameof(GetAuthorsAsync))] // nameof(GetAuthorsAsync)一定不能漏,否则rl.Link(nameof(GetAuthorsAsync)会返回null public async Task <ActionResult <IEnumerable <AuthorDto> > > GetAuthorsAsync([FromQuery] AuthorResourceParameters parameters) //页码信息从url中取,因为不是resource数据 { //throw new Exception("test exception filter"); var pagedList = await RepositoryWrapper.Author.GetAllAsync(parameters); Logger.LogWarning(1, parameters.SortBy); //匿名对象,包括所有分页的有关信息 var paginationMetadata = new { //记录总数 totalCount = pagedList.TotalCount, //每页记录数 pageSize = pagedList.PageSize, //当前页 currentPage = pagedList.CurrentPage, //所有页数 totalPages = pagedList.TotalPages, //上一页的链接,Url.Link方法可以根据路由生成url previousePageLink = pagedList.HasPrevious ? Url.Link(nameof(GetAuthorsAsync), new { pageNumber = pagedList.CurrentPage - 1, pageSize = pagedList.PageSize, birthPlace = parameters.BirthPlace, searchQuery = parameters.SearchQuery, sortBy = parameters.SortBy }) : null, //下一页的链接 nextPageLink = pagedList.HasNext ? Url.Link(nameof(GetAuthorsAsync), new { pageNumber = pagedList.CurrentPage + 1, pageSize = pagedList.PageSize, birthPlace = parameters.BirthPlace, searchQuery = parameters.SearchQuery, sortBy = parameters.SortBy }) : null }; //将分页的元数据加入到响应的header内,自定义消息头"X-Pagination" Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var authorDtoList = Mapper.Map <IEnumerable <AuthorDto> >(pagedList); return(authorDtoList.ToList()); }