//get a list of the user's saved recipes and pass that to the view to display public async Task <IActionResult> SavedRecipeList() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var savedRecipeList = await _repositoryClient.RetrieveRecipeList(userId); var savedCustomRecipeList = await _repositoryClient.RetrieveCustomRecipeList(userId); var recipeList = new List <SavedRecipeListViewModel>(); foreach (var recipe in savedRecipeList) { var recipeInfo = await _recipeClient.SearchForRecipeById(recipe.RecipeId); var recipeResult = new SavedRecipeListViewModel() { RecipeId = recipe.RecipeId, Name = recipeInfo.Title, ImageURL = recipeInfo.Image, RecipeURL = recipeInfo.SourceUrl }; recipeList.Add(recipeResult); } foreach (var recipe in savedCustomRecipeList) { var recipeResult = new SavedRecipeListViewModel() { CustomeRecipeId = recipe.Id, Name = recipe.RecipeName, ImageURL = null, RecipeURL = null }; recipeList.Add(recipeResult); } return(View(recipeList)); }
//Retrieve's custom recipe info to display on the edit form public async Task <IActionResult> EditCustomRecipe(SavedRecipeListViewModel recipe) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var customRecipeInfo = await _repositoryClient.RetrieveCustomRecipe(userId, recipe.CustomeRecipeId); var customRecipe = new CustomRecipeViewModel() { UserId = customRecipeInfo.UserId, RecipeName = customRecipeInfo.RecipeName, Ingredients = customRecipeInfo.Ingredients, Directions = string.IsNullOrEmpty(customRecipeInfo.Directions) ?string.Empty : customRecipeInfo.Directions, Notes = string.IsNullOrEmpty(customRecipeInfo.Notes) ? string.Empty : customRecipeInfo.Notes, Id = customRecipeInfo.Id }; return(View("CreateRecipePage", customRecipe)); }
//Displays the list of meal time options for the form public IActionResult MealPlanningForm(SavedRecipeListViewModel recipe) { var mealTimeTypes = new List <MealTimeSelectOptions>(); foreach (MealTimeType mealTimeType in Enum.GetValues(typeof(MealTimeType))) { mealTimeTypes.Add(new MealTimeSelectOptions() { MealTimeType = mealTimeType, MealTimeTypeValue = mealTimeType.ToString() }); } var recipeInfo = new MealPlanningFormViewModel() { RecipeId = recipe.RecipeId, CustomRecipeId = recipe.CustomeRecipeId, MealTimeSelectOptions = mealTimeTypes }; return(View(recipeInfo)); }
//Deleting a saved recipe from the database and returning back to the saved recipe view public async Task <IActionResult> DeleteSavedRecipe(SavedRecipeListViewModel recipeInfo) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var isRecipeDeleted = false; if (recipeInfo.CustomeRecipeId == null) { isRecipeDeleted = await _repositoryClient.DeleteRecipe(userId, recipeInfo.RecipeId, null); } else { isRecipeDeleted = await _repositoryClient.DeleteRecipe(userId, null, recipeInfo.CustomeRecipeId); } if (isRecipeDeleted) { return(RedirectToAction("SavedRecipeList", "Recipe")); } else { return(RedirectToAction("Error", "Home")); } }