public Task <PagedList <Role> > GetRolesAsync(RolesResourceParameters rolesResourceParameters) { var collectionBeforePaging = QueryableExtensions.ApplySort(_roleRepository.FindAllActiveRolesPagedOf(rolesResourceParameters.PageIndex, rolesResourceParameters.PageSize), rolesResourceParameters.OrderBy + " " + rolesResourceParameters.SortDirection, _propertyMappingService.GetPropertyMapping <RoleUiModel, Role>()); if (!string.IsNullOrEmpty(rolesResourceParameters.Filter) && !string.IsNullOrEmpty(rolesResourceParameters.SearchQuery)) { var searchQueryForWhereClauseFilterFields = rolesResourceParameters.Filter .Trim().ToLowerInvariant(); var searchQueryForWhereClauseFilterSearchQuery = rolesResourceParameters.SearchQuery .Trim().ToLowerInvariant(); collectionBeforePaging.QueriedItems = (IQueryable <Role>)collectionBeforePaging.QueriedItems .AsEnumerable().FilterData(searchQueryForWhereClauseFilterFields, searchQueryForWhereClauseFilterSearchQuery); } return(Task.Run(() => PagedList <Role> .Create(collectionBeforePaging, rolesResourceParameters.PageIndex, rolesResourceParameters.PageSize))); }
private IEnumerable <LinkDto> CreateLinksForRoles(RolesResourceParameters RolesResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDto> { new LinkDto(CreateRolesResourceUri(RolesResourceParameters, ResourceUriType.Current) , "self", "GET") }; if (hasNext) { links.Add( new LinkDto(CreateRolesResourceUri(RolesResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add( new LinkDto(CreateRolesResourceUri(RolesResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
private string CreateRolesResourceUri(RolesResourceParameters RolesResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetRoles", new { fields = RolesResourceParameters.Fields, orderBy = RolesResourceParameters.OrderBy, searchQuery = RolesResourceParameters.SearchQuery, pageNumber = RolesResourceParameters.PageIndex - 1, pageSize = RolesResourceParameters.PageSize })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetRoles", new { fields = RolesResourceParameters.Fields, orderBy = RolesResourceParameters.OrderBy, searchQuery = RolesResourceParameters.SearchQuery, pageNumber = RolesResourceParameters.PageIndex + 1, pageSize = RolesResourceParameters.PageSize })); case ResourceUriType.Current: default: return(_urlHelper.Link("GetRoles", new { fields = RolesResourceParameters.Fields, orderBy = RolesResourceParameters.OrderBy, searchQuery = RolesResourceParameters.SearchQuery, pageNumber = RolesResourceParameters.PageIndex, pageSize = RolesResourceParameters.PageSize })); } }
public async Task <IActionResult> GetRolesAsync([FromQuery] RolesResourceParameters rolesResourceParameters, [FromHeader(Name = "Accept")] string mediaType) { if (!_propertyMappingService.ValidMappingExistsFor <RoleUiModel, Role> (rolesResourceParameters.OrderBy)) { return(BadRequest()); } if (!_typeHelperService.TypeHasProperties <RoleUiModel> (rolesResourceParameters.Fields)) { return(BadRequest()); } var rolesQueryable = await _inquiryAllRolesProcessor.GetRolesAsync(rolesResourceParameters); var roles = Mapper.Map <IEnumerable <RoleUiModel> >(rolesQueryable); if (mediaType.Contains("application/vnd.marvin.hateoas+json")) { var paginationMetadata = new { totalCount = rolesQueryable.TotalCount, pageSize = rolesQueryable.PageSize, currentPage = rolesQueryable.CurrentPage, totalPages = rolesQueryable.TotalPages, }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); var links = CreateLinksForRoles(rolesResourceParameters, rolesQueryable.HasNext, rolesQueryable.HasPrevious); var shapedPersons = roles.ShapeData(rolesResourceParameters.Fields); var shapedPersonsWithLinks = shapedPersons.Select(person => { var personAsDictionary = person as IDictionary <string, object>; var personLinks = CreateLinksForRole((Guid)personAsDictionary["Id"], rolesResourceParameters.Fields); personAsDictionary.Add("links", personLinks); return(personAsDictionary); }); var linkedCollectionResource = new { value = shapedPersonsWithLinks, links = links }; return(Ok(linkedCollectionResource)); } else { var previousPageLink = rolesQueryable.HasPrevious ? CreateRolesResourceUri(rolesResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = rolesQueryable.HasNext ? CreateRolesResourceUri(rolesResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { previousPageLink = previousPageLink, nextPageLink = nextPageLink, totalCount = rolesQueryable.TotalCount, pageSize = rolesQueryable.PageSize, currentPage = rolesQueryable.CurrentPage, totalPages = rolesQueryable.TotalPages }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata)); return(Ok(roles.ShapeData(rolesResourceParameters.Fields))); } }