public async Task <IActionResult> GetRecipes([FromQuery] UserParamsForFilterRecipes userParams, int amount = 12)
        {
            int userId = int.Parse(User.FindFirst(claim => claim.Type == ClaimTypes.NameIdentifier).Value);
            IEnumerable <Ingredient> ingredients = _repoFridge.GetIngredients(userId);
            string content = null;

            if (userParams.DietType != null || userParams.CuisineType != null)
            {
                content = await _repo.GetResponeWhenPassParams(ingredients, amount, userParams);

                Root recipesToReturn = JsonConvert.DeserializeObject <Root>(content);
                return(Ok(recipesToReturn.Results));
            }

            else
            {
                content = await _repo.GetRespone(ingredients, amount);
            }

            if (content == null)
            {
                return(NotFound());
            }


            IEnumerable <RecipeToList> recipes = JsonConvert.DeserializeObject <IEnumerable <RecipeToList> >(content);

            return(Ok(recipes));
        }
示例#2
0
        public async Task <string> GetResponeWhenPassParams(IEnumerable <Ingredient> ingredients, int numberOfRecipes, UserParamsForFilterRecipes userParams)
        {
            string     igredientUrl = _makePartialUrl.UrlIngredientMaker(ingredients);
            string     parameters   = userParams.ToString();
            RestClient client       = new RestClient();

            client.BaseUrl = new Uri($"{_baseUrl}complexSearch?{_apiKey}&includeIngredients={igredientUrl}&fillIngredients=true&number={numberOfRecipes}{parameters}");
            RestRequest   request  = new RestRequest(Method.GET);
            IRestResponse response = await client.ExecuteAsync(request);

            if (response.IsSuccessful)
            {
                return(response.Content);
            }
            return(null);
        }