public void GetRecipeInfoCall(Recipe recipe) { string urlString = recipe.apiId.ToString() + "/information?includeNutrition=false"; var response = GetApiRequest(urlString); var result = response.Result.Body; JObject jobject = JObject.Parse(result); var ingredientInfo = jobject.SelectToken("extendedIngredients"); var stepInfo = jobject.SelectToken("analyzedInstructions"); List <string> ingredientsRetreived = new List <string>(); List <GetAnalyzedInstructionsStep> stepsRetreived = new List <GetAnalyzedInstructionsStep>(); foreach (var ingredient in ingredientInfo) { IngredientsForRecipes ingredientsFor = new IngredientsForRecipes { RecipeId = recipe.Id, IngredientName = ingredient.SelectToken("name").ToString(), Amount = (double)ingredient.SelectToken("amount"), Unit = ingredient.SelectToken("unit").ToString() }; _context.IngredientsForRecipes.Add(ingredientsFor); } GetAnalyzedInstructions[] rootObject = JsonConvert.DeserializeObject <GetAnalyzedInstructions[]>(stepInfo.ToString()); try { var root = rootObject[0]; foreach (var step in root.steps) { StepsForRecipe newStep = new StepsForRecipe { RecipeId = recipe.Id, StepNumber = step.number, StepDescription = step.step }; _context.StepsForRecipe.Add(newStep); } } catch (IndexOutOfRangeException) { Recipe ignoringRecipe = _context.Recipe.Find(recipe.Id); _context.Recipe.Remove(ignoringRecipe); } _context.SaveChanges(); }
public void GetComplexRecipeCall(int numberOfRecipes) { var fridgeItems = _context.FoodItem.ToList(); var listOfIncludedIngredients = string.Join(",", fridgeItems.Select(o => o.Name)); var intolerances = _context.DietaryRestriction.Where(o => o.IsAllergic == true).ToList(); var listOfIntolerances = string.Join(",", intolerances.Select(o => o.Restriction)); var listOfExcludedIngredients = listOfIntolerances; string urlString = "searchComplex?addRecipeInformation=true&excludeIngredients=" + listOfExcludedIngredients + "&fillIngredients=false&includeIngredients=" + listOfIncludedIngredients + "&instructionsRequired=true&intolerances=" + listOfIntolerances + "&limitLicense=false&number=1&offset=1&type=main+course"; var response = GetApiRequest(urlString); string result = response.Result.Body; JObject jobject = JObject.Parse(result); if (numberOfRecipes == 1) { DeserializeComplexCall rootObject = JsonConvert.DeserializeObject <DeserializeComplexCall>(result); Recipe newRecipe = new Recipe { apiId = rootObject.results[0].id, title = rootObject.results[0].title, image = rootObject.results[0].image, Saved = false }; _context.Recipe.Add(newRecipe); var stepInfo = jobject.SelectToken("results")[0].SelectToken("analyzedInstructions"); GetAnalyzedInstructions[] rootSteps = JsonConvert.DeserializeObject <GetAnalyzedInstructions[]>(stepInfo.ToString()); var root = rootSteps[0]; try { foreach (var step in root.steps) { StepsForRecipe newStep = new StepsForRecipe { RecipeId = newRecipe.Id, StepNumber = step.number, StepDescription = step.step }; _context.StepsForRecipe.Add(newStep); } //if (root.steps[0].ingredients.Count != 0 && root.steps[0].ingredients != null) //{ // foreach (var ingredient in root.steps) // { // IngredientsForRecipes ingredientsFor = new IngredientsForRecipes // { // RecipeId = recipe.Id, // IngredientName = ingredient.SelectToken("name").ToString(), // Amount = (double)ingredient.SelectToken("amount"), // Unit = ingredient.SelectToken("unit").ToString() // }; // } //} } catch (IndexOutOfRangeException) { Recipe ignoringRecipe = _context.Recipe.Find(newRecipe.Id); _context.Recipe.Remove(ignoringRecipe); } } else { DeserializeComplexCall[] rootObject = JsonConvert.DeserializeObject <DeserializeComplexCall[]>(result); foreach (var recipeResult in rootObject) { Recipe recipe = new Recipe { }; } } }