private IEnumerable <LinkDto> CreateLinksForRecipes(
            RecipesResourceParameters recipesResourceParameters,
            bool hasNext,
            bool hasPrevious)
        {
            var links = new List <LinkDto>();

            links.Add(
                new LinkDto(CreateRecipesResourceUri(
                                recipesResourceParameters, ETypeOfResourceUri.Current),
                            this.SELF,
                            this.GET));

            if (hasNext)
            {
                links.Add(
                    new LinkDto(CreateRecipesResourceUri(
                                    recipesResourceParameters, ETypeOfResourceUri.NextPage),
                                this.NEXT_PAGE,
                                this.GET));
            }

            if (hasPrevious)
            {
                links.Add(
                    new LinkDto(CreateRecipesResourceUri(
                                    recipesResourceParameters, ETypeOfResourceUri.PreviousPage),
                                this.PREVIOUS_PAGE,
                                this.GET));
            }

            return(links);
        }
        public ActionResult <IEnumerable <RecipeWithoutStepsDto> > GetRecipes(
            [FromQuery] RecipesResourceParameters recipesResourceParameters,
            [FromHeader(Name = ExtendedControllerBase.ACCEPT)] string mediaTypes)
        {
            if (!_propertyMappingService.ValidMappingExistsFor <RecipeDto, Entities.Recipe>
                    (recipesResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            var splitMediaTypes = mediaTypes.Split(',');

            if (!MediaTypeHeaderValue.TryParseList(splitMediaTypes,
                                                   out IList <MediaTypeHeaderValue> parsedMediaTypes))
            {
                return(BadRequest());
            }

            var recipes = _homeBrewRepository.GetRecipes(recipesResourceParameters);

            var paginationMetaData = new
            {
                totalCount  = recipes.TotalCount,
                pageSize    = recipes.PageSize,
                currentPage = recipes.CurrentPage,
                totalPages  = recipes.TotalPages
            };

            Response.Headers.Add(this.PAGINATION_HEADER,
                                 JsonSerializer.Serialize(paginationMetaData));

            var shapedRecipes = _mapper.Map <IEnumerable <RecipeWithoutStepsDto> >(recipes)
                                .ShapeData(null);

            if (parsedMediaTypes.Any(pmt => pmt.SubTypeWithoutSuffix.EndsWith(this.HATEOAS, StringComparison.InvariantCultureIgnoreCase)))
            {
                var links = CreateLinksForRecipes(recipesResourceParameters,
                                                  recipes.HasNext,
                                                  recipes.HasPrevious);

                var shapedRecipesWithLinks = shapedRecipes.Select(recipe =>
                {
                    var recipeAsDictionary = recipe as IDictionary <string, object>;
                    var recipeLinks        = CreateLinksForRecipe((int)recipeAsDictionary["Id"], false);
                    recipeAsDictionary.Add(this.LINKS, recipeLinks);
                    return(recipeAsDictionary);
                });

                var linkedCollectionResouce = new
                {
                    value = shapedRecipesWithLinks,
                    links
                };

                return(Ok(linkedCollectionResouce));
            }

            return(Ok(shapedRecipes));
        }
        public IActionResult GetRecipes(
            [FromBody] RecipesResourceParameters recipesResourceParameters)
        {
            var getRecipesResult = _recipeRepository.GetRecipes(recipesResourceParameters);
            var results          = Mapper.Map <RecipeListDto>(getRecipesResult.recipes);

            results.Total = getRecipesResult.TotalRecipeCount;

            if (results.Recipes.Count == 0)
            {
                return(StatusCode(204, "No categories found."));
            }

            return(Ok(results));
        }
        public (int TotalRecipeCount, IEnumerable <Recipe> recipes) GetRecipes(RecipesResourceParameters recipesResourceParameters)
        {
            var recipes = _context.Recipes
                          .Include(recipe => recipe.Ingredients)
                          .Include(recipe => recipe.RecipeCategories)
                          .ThenInclude(recipeCategory => recipeCategory.Category)
                          .OrderBy(recipe => recipe.Title).ToList();

            var totalRecipeCount = recipes.Count;

            if (recipesResourceParameters == null)
            {
                return(totalRecipeCount, recipes);
            }

            if (!string.IsNullOrEmpty(recipesResourceParameters.Category))
            {
                var categoryForWhereClause = recipesResourceParameters.Category
                                             .Trim().ToLowerInvariant();
                recipes = recipes
                          .Where(recipe => recipe.RecipeCategories.Any(rc => rc.Category.Name.Trim().ToLowerInvariant() == categoryForWhereClause)).ToList();
            }

            if (!string.IsNullOrEmpty(recipesResourceParameters.Ingredient))
            {
                var ingredientForWhereClause = recipesResourceParameters.Ingredient
                                               .Trim().ToLowerInvariant();
                recipes = recipes
                          .Where(recipe => recipe.Ingredients.Any(ingredient => ingredient.Name.Trim().ToLowerInvariant().Contains(ingredientForWhereClause))).ToList();
            }

            if (!string.IsNullOrEmpty(recipesResourceParameters.SearchQuery))
            {
                var searchQueryForWhereClause = recipesResourceParameters.SearchQuery
                                                .Trim().ToLowerInvariant();

                recipes = recipes
                          .Where(recipe => recipe.RecipeCategories.Any(rc => rc.Category.Name.Trim().ToLowerInvariant().Contains(searchQueryForWhereClause)) ||
                                 recipe.Ingredients.Any(ingredient => ingredient.Name.Trim().ToLowerInvariant().Contains(searchQueryForWhereClause)) ||
                                 recipe.Title.Trim().ToLowerInvariant().Contains(searchQueryForWhereClause)).ToList();
            }

            return(totalRecipeCount, recipes);
        }
        private string CreateRecipesResourceUri(
            RecipesResourceParameters recipesResourceParameters,
            ETypeOfResourceUri type)
        {
            var linkProps = new ExpandoObject() as IDictionary <string, Object>;

            if (Enum.IsDefined(typeof(ETypeOfBeer), recipesResourceParameters.BeerType))
            {
                linkProps.Add("beerType", recipesResourceParameters.BeerType);
            }

            switch (type)
            {
            case ETypeOfResourceUri.PreviousPage:
                linkProps.Add("orderBy", recipesResourceParameters.OrderBy);
                linkProps.Add("pageNumber", recipesResourceParameters.PageNumber - 1);
                linkProps.Add("pageSize", recipesResourceParameters.PageSize);
                linkProps.Add("searchQuery", recipesResourceParameters.SearchQuery);

                return(Url.Link("GetBrews", linkProps));

            case ETypeOfResourceUri.NextPage:
                linkProps.Add("orderBy", recipesResourceParameters.OrderBy);
                linkProps.Add("pageNumber", recipesResourceParameters.PageNumber + 1);
                linkProps.Add("pageSize", recipesResourceParameters.PageSize);
                linkProps.Add("searchQuery", recipesResourceParameters.SearchQuery);

                return(Url.Link("GetBrews", linkProps));

            case ETypeOfResourceUri.Current:
            default:
                linkProps.Add("orderBy", recipesResourceParameters.OrderBy);
                linkProps.Add("pageNumber", recipesResourceParameters.PageNumber);
                linkProps.Add("pageSize", recipesResourceParameters.PageSize);
                linkProps.Add("searchQuery", recipesResourceParameters.SearchQuery);

                return(Url.Link("GetBrews", linkProps));
            }
        }
Beispiel #6
0
        public PagedList <Recipe> GetRecipes(RecipesResourceParameters recipesResourceParameters)
        {
            if (recipesResourceParameters == null)
            {
                throw new ArgumentNullException(nameof(recipesResourceParameters));
            }

            var collection = _context.Recipes as IQueryable <Recipe>;

            collection = collection.Include(r => r.WaterProfile).ThenInclude(wp => wp.Additions);

            if (Enum.IsDefined(typeof(ETypeOfBeer), recipesResourceParameters.BeerType))
            {
                var beerType = (ETypeOfBeer)recipesResourceParameters.BeerType;

                collection = collection.Where(r => r.Type == beerType);
            }

            if (!string.IsNullOrWhiteSpace(recipesResourceParameters.SearchQuery))
            {
                var searchQuery = recipesResourceParameters.SearchQuery.Trim();

                collection = collection.Where(r => r.Name.Contains(searchQuery) ||
                                              r.Description.Contains(searchQuery));
            }

            if (!string.IsNullOrWhiteSpace(recipesResourceParameters.OrderBy))
            {
                var recipePropertyMappingDictionary =
                    _propertyMappingService.GetPropertyMapping <Models.RecipeDto, Recipe>();

                collection = collection.ApplySort(recipesResourceParameters.OrderBy,
                                                  recipePropertyMappingDictionary);
            }

            return(PagedList <Recipe> .Create(collection,
                                              recipesResourceParameters.PageNumber,
                                              recipesResourceParameters.PageSize));
        }