public void ParseGetResultsResponseAndUpdateInDb(string httpResponse, RecipeSearchResult recipeSearchResult) { JObject jObect = JObject.Parse(httpResponse); recipeSearchResult.Ingredients = jObect["ingredientLines"].ToString(); recipeSearchResult.RecipeSourceUrl = jObect["source"]["sourceRecipeUrl"].ToString(); db.SaveChanges(); }
private void ParseSearchResultsResponseAndStoreInDb(string httpResponse) { JObject jObect = JObject.Parse(httpResponse); IList <JToken> jTokenList = jObect["matches"].Children().ToList(); // serialize JSON results into .NET objects foreach (JToken jToken in jTokenList) { RecipeSearchResult recipeSearchResult = new RecipeSearchResult { Id = jToken["id"].ToString(), RecipeName = jToken["recipeName"].ToString(), ImageUrl = jToken["imageUrlsBySize"]["90"].ToString() }; db.RecipeSearchResults.Add(recipeSearchResult); } db.SaveChanges(); }
//This is called when users put in their search criteria via the home page. This calls out to the recipeClient, which makes the API //calls to return back the list of recipes public async Task <IActionResult> SearchRecipe(string diet, Intolerances intolerances, int?maxCalorie, int?maxCarb, int?maxProtein, int?minProtein) { var recipes = await _recipeClient.SearchForRecipeByQuery(diet, intolerances, maxCalorie, maxCarb, maxProtein, minProtein); var recipeSearchResults = new List <RecipeSearchResult>(); foreach (var recipe in recipes) { var recipeResult = new RecipeSearchResult() { Id = recipe.Id, Title = recipe.Title, Image = recipe.Image, }; recipeSearchResults.Add(recipeResult); } return(View(recipeSearchResults)); }
public ActionResult Details(string id) { try { RecipeSearchResult recipeSearchResult = db.RecipeSearchResults.Find(id); if (recipeSearchResult.Ingredients == null) { string _requestUrl = "api/recipe/" + recipeSearchResult.Id + "?_app_id=" + AppId + "&_app_key=" + AppKey; string httpResponse = CallYumlyApi(_requestUrl); ParseGetResultsResponseAndUpdateInDb(httpResponse, recipeSearchResult); } return(View(recipeSearchResult)); } catch (Exception e) { TempData["ErrorMessage"] = GetErrorMessage(e); return(RedirectToAction("Error")); } }
/// <summary> /// Send a request at url, deserialize JSON found and display corresponding recipes. /// </summary> private IEnumerator SearchForRecipes(string url) { using (UnityWebRequest request = UnityWebRequest.Get(url)) { yield return(request.SendWebRequest()); if (request.isNetworkError || request.isHttpError) { Debug.LogError(request.error); } else { string json = request.downloadHandler.text; RecipeSearchResult search = JsonConvert.DeserializeObject <RecipeSearchResult>(json); for (int i = 0; i < search.results.Count; i++) { Recipe recipe = search.results[i]; AddRecipe(recipe.title, recipe.thumbnail); } } } }
public ActionResult SaveRecipe(String id) { RecipeSearchResult result = db.RecipeSearchResults.Find(id); if (result != null) { UserRecipe userRecipe = db.UserRecipes.Find(result.Id, User.Identity.Name); if (userRecipe == null) { userRecipe = new UserRecipe(); userRecipe.RecipeId = result.Id; userRecipe.RecipeName = result.RecipeName; userRecipe.ImageUrl = result.ImageUrl; userRecipe.RecipeSourceUrl = result.RecipeSourceUrl; userRecipe.Ingredients = result.Ingredients; userRecipe.UserName = User.Identity.Name; db.UserRecipes.Add(userRecipe); db.SaveChanges(); } } return(RedirectToAction("Index", "UserRecipes")); }
private RecipeSearchResult RecipeSearch(string filters = "", string kwd = "", int itemsPerPage = 3, int currentPage = 1) { RecipeSearchResult resultPage = new RecipeSearchResult(); var searchQuery = _searchClient.Search <ArticlePage>(); if (!string.IsNullOrWhiteSpace(kwd)) { searchQuery = searchQuery.For(kwd) .InFields(x => x.Heading, x => x.ShortDescription, x => x.Categories, x => x.SearchText()); } if (!string.IsNullOrWhiteSpace(filters)) { searchQuery = searchQuery.For(filters) .InField(x => x.CategoryMenu);; } if (searchQuery.GetContentResult().TotalMatching > itemsPerPage * currentPage) { resultPage.HasMorePages = true; } else { resultPage.HasMorePages = false; } var skipNumber = (currentPage - 1) * itemsPerPage; searchQuery = searchQuery.Skip(skipNumber).Take(itemsPerPage); var queryResults = searchQuery.GetContentResult(); var totalResults = queryResults.TotalMatching; if (totalResults > 0) { resultPage.Items = queryResults.Select(x => new RecipeSearchItem { Heading = x.Heading, PageLink = x.ContentLink, ImageLink = x.Image }).ToList(); resultPage.PaginationViewModel = new PaginationViewModel { CurrentPage = currentPage, ItemsPerPage = itemsPerPage, TotalItems = totalResults }; } else { resultPage.SuggestedTerm = GetDidYouMeanSuggestion(kwd); if (string.IsNullOrWhiteSpace(resultPage.SuggestedTerm)) { resultPage.SuggestedTerm = GetSpellCheckerSuggestion(kwd); } } return(resultPage); }
public RecipeSearchResultViewModel() { ResultPage = new RecipeSearchResult(); }
public RecipeSearchResultViewModel(T currentPage) { ResultPage = new RecipeSearchResult(); CurrentPage = currentPage; }