public ActionResult <IEnumerable <RecipeDto> > GetCategories([FromQuery] RecipeParametersDto recipeParametersDto) { var recipesFromRepo = _recipeRepository.GetRecipes(recipeParametersDto); var previousPageLink = recipesFromRepo.HasPrevious ? CreateRecipesResourceUri(recipeParametersDto, ResourceUriType.PreviousPage) : null; var nextPageLink = recipesFromRepo.HasNext ? CreateRecipesResourceUri(recipeParametersDto, ResourceUriType.NextPage) : null; var paginationMetadata = new { totalCount = recipesFromRepo.TotalCount, pageSize = recipesFromRepo.PageSize, pageNumber = recipesFromRepo.PageNumber, totalPages = recipesFromRepo.TotalPages, hasPrevious = recipesFromRepo.HasPrevious, hasNext = recipesFromRepo.HasNext, previousPageLink, nextPageLink }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); var recipesDto = _mapper.Map <IEnumerable <RecipeDto> >(recipesFromRepo); return(Ok(recipesDto)); }
public PagedList <Recipe> GetRecipes(RecipeParametersDto recipeParameters) { if (recipeParameters == null) { throw new ArgumentNullException(nameof(recipeParameters)); } var collection = _context.Recipes as IQueryable <Recipe>; if (!string.IsNullOrWhiteSpace(recipeParameters.QueryString)) { var QueryString = recipeParameters.QueryString.Trim(); collection = collection.Where(r => r.RecipeTextField1.Contains(QueryString) || r.RecipeTextField2.Contains(QueryString)); } var sieveModel = new SieveModel { Sorts = recipeParameters.SortOrder, Filters = recipeParameters.Filters }; collection = _sieveProcessor.Apply(sieveModel, collection); return(PagedList <Recipe> .Create(collection, recipeParameters.PageNumber, recipeParameters.PageSize)); }
private string CreateRecipesResourceUri( RecipeParametersDto recipeParametersDto, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetRecipes", new { filters = recipeParametersDto.Filters, orderBy = recipeParametersDto.SortOrder, pageNumber = recipeParametersDto.PageNumber - 1, pageSize = recipeParametersDto.PageSize, searchQuery = recipeParametersDto.QueryString })); case ResourceUriType.NextPage: return(Url.Link("GetRecipes", new { filters = recipeParametersDto.Filters, orderBy = recipeParametersDto.SortOrder, pageNumber = recipeParametersDto.PageNumber + 1, pageSize = recipeParametersDto.PageSize, searchQuery = recipeParametersDto.QueryString })); default: return(Url.Link("GetRecipes", new { filters = recipeParametersDto.Filters, orderBy = recipeParametersDto.SortOrder, pageNumber = recipeParametersDto.PageNumber, pageSize = recipeParametersDto.PageSize, searchQuery = recipeParametersDto.QueryString })); } }