コード例 #1
0
        //Adds custom recipe to the database, then redirects to the saved recipe view
        public async Task <IActionResult> CreateRecipe(string recipeName, string ingredients, string directions, string notes, int?id)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (string.IsNullOrEmpty(recipeName))
            {
                TempData["Error"] = "You cannot have a blank name for recipe. Please try again.";
                return(RedirectToAction("Error", "Home"));
            }
            var customRecipe = new CustomRecipe()
            {
                UserId      = userId,
                RecipeName  = recipeName,
                Ingredients = string.IsNullOrEmpty(ingredients) ? string.Empty : ingredients,
                Directions  = string.IsNullOrEmpty(directions) ? string.Empty : directions,
                Notes       = string.IsNullOrEmpty(notes) ? string.Empty : notes
            };
            var isRecipeSaved = false;

            if (id == null)
            {
                isRecipeSaved = await _repositoryClient.AddCustomRecipe(customRecipe);
            }
            else
            {
                customRecipe.Id = id.Value;
                isRecipeSaved   = await _repositoryClient.UpdateRecipe(customRecipe);
            }

            if (isRecipeSaved)
            {
                return(RedirectToAction("SavedRecipeList", "Recipe"));
            }
            else
            {
                return(RedirectToAction("Error", "Home"));
            }
        }