public PagedList <ActivityCategory> GetActivityCategories(ActivityCategoryResourceParameters activityCategoryResourceParameters) { var collectionBeforePaging = _context.ActivityCategories .ApplySort(activityCategoryResourceParameters.OrderBy, _propertyMappingService.GetPropertyMapping <ActivityCategoryDTO, ActivityCategory>()); if (!string.IsNullOrEmpty(activityCategoryResourceParameters.SearchQuery)) { // trim & ignore casing var searchQueryForWhereClause = activityCategoryResourceParameters.SearchQuery.Trim().ToLowerInvariant(); collectionBeforePaging = collectionBeforePaging .Where(ac => ac.Name.ToLowerInvariant().Contains(searchQueryForWhereClause)); } return(PagedList <ActivityCategory> .Create(collectionBeforePaging, activityCategoryResourceParameters.PageNumber, activityCategoryResourceParameters.PageSize)); }
private string CreateActivityCategoriesResourceUri(ActivityCategoryResourceParameters activityCategoryResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(_urlHelper.Link("GetActivityCategories", new { fields = activityCategoryResourceParameters.Fields, orderBy = activityCategoryResourceParameters.OrderBy, searchQuery = activityCategoryResourceParameters.SearchQuery, pageNumber = activityCategoryResourceParameters.PageNumber - 1, pageSize = activityCategoryResourceParameters.PageSize })); case ResourceUriType.NextPage: return(_urlHelper.Link("GetActivityCategories", new { fields = activityCategoryResourceParameters.Fields, orderBy = activityCategoryResourceParameters.OrderBy, searchQuery = activityCategoryResourceParameters.SearchQuery, pageNumber = activityCategoryResourceParameters.PageNumber + 1, pageSize = activityCategoryResourceParameters.PageSize })); case ResourceUriType.Current: default: return(_urlHelper.Link("GetActivityCategories", new { fields = activityCategoryResourceParameters.Fields, orderBy = activityCategoryResourceParameters.OrderBy, searchQuery = activityCategoryResourceParameters.SearchQuery, pageNumber = activityCategoryResourceParameters.PageNumber, pageSize = activityCategoryResourceParameters.PageSize })); } }
public IActionResult GetActivityCategories(ActivityCategoryResourceParameters activityCategoryResourceParameters) { if (!_propertyMappingService.ValidMappingExistsFor <ActivityCategoryDTO, ActivityCategory>(activityCategoryResourceParameters.OrderBy)) { return(BadRequest()); } if (!_typeHelperService.TypeHasProperties <ActivityCategoryDTO>(activityCategoryResourceParameters.Fields)) { return(BadRequest()); } var activityCategoriesFromRepo = _activityCategoryRepository.GetActivityCategories(activityCategoryResourceParameters); var activityCategories = Mapper.Map <IEnumerable <ActivityCategoryDTO> >(activityCategoriesFromRepo); var previousPageLink = activityCategoriesFromRepo.HasPrevious ? CreateActivityCategoriesResourceUri(activityCategoryResourceParameters, ResourceUriType.PreviousPage) : null; var nextPageLink = activityCategoriesFromRepo.HasNext ? CreateActivityCategoriesResourceUri(activityCategoryResourceParameters, ResourceUriType.NextPage) : null; var paginationMetadata = new { previousPageLink = previousPageLink, nextPageLink = nextPageLink, totalCount = activityCategoriesFromRepo.TotalCount, pageSize = activityCategoriesFromRepo.PageSize, currentPage = activityCategoriesFromRepo.CurrentPage, totalPages = activityCategoriesFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); var shapedActivityCategories = activityCategories.ShapeData(activityCategoryResourceParameters.Fields); return(Ok(JsonConvert.SerializeObject(shapedActivityCategories))); //return Ok(activityCategories.ShapeData(activityCategoryResourceParameters.Fields)); }