private string CreateAuthorsResourceUri(AuthorsResourceParams authorsResourceParams, ResourceUriType type) { return(type switch { ResourceUriType.PREVIOUS_PAGE => Url.Link("GetAuthors", new{ fields = authorsResourceParams.Fields, pageNumber = authorsResourceParams.PageNumber - 1, pageSize = authorsResourceParams.PageSize, mainCategory = authorsResourceParams.MainCategory, searchQuery = authorsResourceParams.SearchQuery, orderBy = authorsResourceParams.OrderBy }), ResourceUriType.NEXT_PAGE => Url.Link("GetAuthors", new{ fields = authorsResourceParams.Fields, pageNumber = authorsResourceParams.PageNumber + 1, pageSize = authorsResourceParams.PageSize, mainCategory = authorsResourceParams.MainCategory, searchQuery = authorsResourceParams.SearchQuery, orderBy = authorsResourceParams.OrderBy }), // ResourceUriType.CURRENT => _ => Url.Link("GetAuthors", new{ fields = authorsResourceParams.Fields, pageNumber = authorsResourceParams.PageNumber, pageSize = authorsResourceParams.PageSize, mainCategory = authorsResourceParams.MainCategory, searchQuery = authorsResourceParams.SearchQuery, orderBy = authorsResourceParams.OrderBy }) });
public PagedList <Author> GetAuthors(AuthorsResourceParams authorsResourceParameters) { if (authorsResourceParameters == null) { throw new ArgumentNullException(nameof(authorsResourceParameters)); } var collection = mContext.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 mappingDictionary = mPropertyMappingService.GetPropertyMapping <AuthorDto, Author>(); collection = collection.ApplySort(authorsResourceParameters.OrderBy, mappingDictionary); } return(PagedList <Author> .Create(collection, authorsResourceParameters.PageNumber, authorsResourceParameters.PageSize)); }
public IActionResult GetAuthors( [FromQuery] AuthorsResourceParams resourceParams, [FromHeader(Name = "Accept")] string mediaType) { if (!MediaTypeHeaderValue.TryParse(mediaType, out var parsedMediaType)) { return(BadRequest()); } if (!mMappingService.IsMappingValid <AuthorDto, Author>(resourceParams.OrderBy)) { return(BadRequest()); } var authors = mRepository.GetAuthors(resourceParams); // var prevPageLink = authors.HasPrevious // ? CreateAuthorsResourceUri(resourceParams, // ResourceUriType.PREVIOUS_PAGE) // : null; // var nextPageLink = authors.HasNext // ? CreateAuthorsResourceUri(resourceParams, // ResourceUriType.NEXT_PAGE) // : null; // var includeLinksPerAuthor = parsedMediaType.SubTypeWithoutSuffix // .EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase); // IEnumerable<IDictionary<string, object>> authorResourcesToReturn = new List<IDictionary<string, object>>(); // if (includeLinksPerAuthor){ // var primaryMediaType = parsedMediaType.SubTypeWithoutSuffix // .Substring(0, parsedMediaType.SubTypeWithoutSuffix.Length - 8); // // foreach (var author in authors){ // IEnumerable<LinkDto> linksPerAuthor = new List<LinkDto>(); // linksPerAuthor = CreateLinksForAuthor(author.Id, resourceParams.Fields); // // switch (primaryMediaType){ // case "vnd.marvin.author.full":{ // var fullResourceToReturn = mMapper.Map<AuthorFullDto>(author) // .ShapeData(resourceParams.Fields) as IDictionary<string, object>; // fullResourceToReturn.Add("links", linksPerAuthor); // authorResourcesToReturn.Append(fullResourceToReturn); // break; // } // case "vnd.marvin.author.friendly":{ // var fullResourceToReturn = mMapper.Map<AuthorDto>(author) // .ShapeData(resourceParams.Fields) as IDictionary<string, object>; // fullResourceToReturn.Add("links", linksPerAuthor); // authorResourcesToReturn.Append(fullResourceToReturn); // break; // } // } // } // } var pagesMetaData = new { totalCount = authors.TotalCount, pageSize = authors.PageSize, currentPage = authors.CurrentPage, totalPages = authors.TotalPages, // prevPageLink, // nextPageLink }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(pagesMetaData)); if (parsedMediaType.MediaType != "application/vnd.marvin.hateoas+json") { return(Ok(mMapper.Map <IEnumerable <AuthorDto> >(authors))); } var links = CreateLinksForAuthors(resourceParams, authors.HasNext, authors.HasPrevious); var shapedAuthorsData = mMapper.Map <IEnumerable <AuthorDto> >(authors) .ShapeData(resourceParams.Fields); var shapedAuthorsDataWithLinks = shapedAuthorsData .Select(author => { var authorAsDictionary = author as IDictionary <string, object>; var authorLinks = CreateLinksForAuthor((Guid)authorAsDictionary["Id"], null); authorAsDictionary.Add("links", authorLinks); return(authorAsDictionary); }); return(Ok(new { authors = shapedAuthorsDataWithLinks, links })); }