private IEnumerable <LinkDto> CreateLinksForBusinesses( CommonResourceParameters comResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDto>(); // self links.Add( new LinkDto(CreateBusinessResourceUri(comResourceParameters, ResourceUriType.Current), "self", "GET")); if (hasNext) { links.Add( new LinkDto(CreateBusinessResourceUri(comResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add( new LinkDto(CreateBusinessResourceUri(comResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
public PagedList <OwpiniEvent> GetOwpiniEvents(CommonResourceParameters commonResourceParameters) { var collectionBeforePaging = _context.OwpiniEvents.ApplySort(commonResourceParameters.OrderBy); if (!string.IsNullOrEmpty(commonResourceParameters.SearchQuery)) { var searchQueryForWhereClause = commonResourceParameters.SearchQuery .Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(b => b.Name.Contains(searchQueryForWhereClause)); } return(PagedList <OwpiniEvent> .Create(collectionBeforePaging, commonResourceParameters.PageNumber, commonResourceParameters.PageSize)); }
public PagedList <Business> GetBusinesses(CommonResourceParameters commonResourceParameters) { var collectionBeforePaging = _context.Businesses.ApplySort(commonResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <BusinessDto, Business>()); if (!string.IsNullOrEmpty(commonResourceParameters.SearchQuery)) { var searchQueryForWhereClause = commonResourceParameters.SearchQuery .Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(b => b.Name.Contains(searchQueryForWhereClause)); } return(PagedList <Business> .Create(collectionBeforePaging, commonResourceParameters.PageNumber, commonResourceParameters.PageSize)); }
private string CreateBusinessResourceUri( CommonResourceParameters commonResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetBusinesses", new { fields = commonResourceParameters.Fields, orderBy = commonResourceParameters.OrderBy, searchQuery = commonResourceParameters.SearchQuery, pageNumber = commonResourceParameters.PageNumber - 1, pageSize = commonResourceParameters.PageSize })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetBusinesses", new { fields = commonResourceParameters.Fields, orderBy = commonResourceParameters.OrderBy, searchQuery = commonResourceParameters.SearchQuery, pageNumber = commonResourceParameters.PageNumber + 1, pageSize = commonResourceParameters.PageSize })); case ResourceUriType.Current: default: return(_urlHelper.Link("GetBusinesses", new { fields = commonResourceParameters.Fields, orderBy = commonResourceParameters.OrderBy, searchQuery = commonResourceParameters.SearchQuery, pageNumber = commonResourceParameters.PageNumber, pageSize = commonResourceParameters.PageSize })); } }
public IActionResult GetBusinesses(CommonResourceParameters comResourceParam, [FromHeader(Name = "Accept")] string mediaType) { if (!_propertyMappingService.ValidMappingExistsFor <BusinessDto, Business> (comResourceParam.OrderBy)) { return(BadRequest()); } if (!_typeHelperService.TypeHasProperties <BusinessDto> (comResourceParam.Fields)) { return(BadRequest()); } var businessesFromRepo = _owpiniRepository.GetBusinesses(comResourceParam); var businesses = Mapper.Map <IEnumerable <BusinessDto> >(businessesFromRepo); if (mediaType == "application/vnd.owpini.hateoas+json") { var paginationMetaData = new { totalCount = businessesFromRepo.TotalCount, pageSize = businessesFromRepo.PageSize, currentPage = businessesFromRepo.CurrentPage, totalPages = businessesFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetaData)); var links = CreateLinksForBusinesses(comResourceParam, businessesFromRepo.HasNext, businessesFromRepo.HasPrevious); var shapedBusinesses = businesses.ShapeData(comResourceParam.Fields); var shapedBusinessesWithLinks = shapedBusinesses.Select(biz => { var bizAsDictionary = biz as IDictionary <string, object>; var businessLinks = CreateLinksForBusiness( (Guid)bizAsDictionary["Id"], comResourceParam.Fields); bizAsDictionary.Add("links", businessLinks); return(bizAsDictionary); }); var linkedCollectionResource = new { value = shapedBusinessesWithLinks, links = links }; return(Ok(linkedCollectionResource)); } var previousPageLink = businessesFromRepo.HasPrevious ? CreateBusinessResourceUri(comResourceParam, ResourceUriType.PreviousPage) : null; var nextPageLink = businessesFromRepo.HasNext ? CreateBusinessResourceUri(comResourceParam, ResourceUriType.NextPage) : null; var paginationMetadata = new { previousPageLink = previousPageLink, nextPageLink = nextPageLink, totalCount = businessesFromRepo.TotalCount, pageSize = businessesFromRepo.PageSize, currentPage = businessesFromRepo.CurrentPage, totalPages = businessesFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); return(Ok(businesses.ShapeData(comResourceParam.Fields))); }