示例#1
0
        public async Task <IActionResult> FilterRecipes([FromBody] FilterDTO filterDto)
        {
            if ((filterDto.IncludedIngredients.Count != 0 || filterDto.ExcludedIngredients.Count != 0) &&
                filterDto.OnlyIngredients.Count != 0)
            {
                return(BadRequest("Ingredients lists are initialized wrong"));
            }
            List <Ingredient> includedIngredients = new List <Ingredient>();
            List <Ingredient> onlyIngredients     = new List <Ingredient>();
            List <Ingredient> excludedIngredients = new List <Ingredient>();

            foreach (var ing in filterDto.IncludedIngredients)
            {
                includedIngredients.Add(await _ingredientsRepository.GetByNameAndMeasure(ing.Name, ing.MeasurementUnit));
            }
            foreach (var ing in filterDto.OnlyIngredients)
            {
                onlyIngredients.Add(await _ingredientsRepository.GetByNameAndMeasure(ing.Name, ing.MeasurementUnit));
            }
            foreach (var ing in filterDto.ExcludedIngredients)
            {
                excludedIngredients.Add(await _ingredientsRepository.GetByNameAndMeasure(ing.Name, ing.MeasurementUnit));
            }
            Filter filter = new Filter
            {
                Cost                = filterDto.Cost,
                Cuisine             = filterDto.Cuisine,
                ExcludedIngredients = excludedIngredients,
                IncludedIngredients = includedIngredients,
                Name                = filterDto.Name,
                OnlyIngredients     = onlyIngredients,
                PreparationTime     = filterDto.PreparationTime,
                Rating              = filterDto.Rating,
                VotesNumber         = filterDto.VotesNumber
            };

            return(Ok(await _recipesRepository.GetByFilter(filter)));
        }