public IActionResult OnGet(string categoryId) { Category = categoryData.GetById(categoryId); if (Category == null) { return(RedirectToPage("./NotFound")); } var recipesInAssignedCategory = recipeData.GetBy(Enum.GetName(typeof(CollectionMappings.RecipeFields), CollectionMappings.RecipeFields.CategoryId), Category.Id); if (recipesInAssignedCategory.Any()) { var recipeNamesToUnassignCategory = RecipeNamesToUnassignCategory(recipesInAssignedCategory); Message = $"Recipe category {Category.Name} has assigned recipe(s). Make sure you are okay unassigning category with recipe(s) ({recipeNamesToUnassignCategory}) before deleting."; } return(Page()); }
public IActionResult OnPost(string IngredientOriginal, string Ingredients, string DirectionOriginal, string Directions, Recipe recipe, string categoryId) { RetrieveRecipe(recipe.Id, string.Empty); SetUpdatedRecipe(recipe, categoryId); bool isAddRecipe = string.IsNullOrEmpty(Recipe.Id); bool isUpdatedMainRecipeInfo = IngredientOriginal == null && Ingredients == null && DirectionOriginal == null && Directions == null; bool isUpdateIngredients = IngredientOriginal != null || Ingredients != null; bool isUpdatedDirections = DirectionOriginal != null || Directions != null; if (!(isUpdatedMainRecipeInfo || isUpdateIngredients || isUpdatedDirections) && !isAddRecipe) { Message = "Error in updating Recipe"; return(Page()); } var action = isAddRecipe ? Action.Adding.ToString().ToLower() : Action.Editing.ToString().ToLower(); if (isAddRecipe) { var existingRecipes = recipeData.GetBy(nameof(CollectionMappings.RecipeFields.Name), Recipe.Name.Trim()); if (existingRecipes.Count > 0) { Message = $"Recipe ({Recipe.Name}) already exists. Enter a different recipe name."; return(Page()); } Recipe = recipeData.Add(Recipe); FormTitle = $"{Action.Editing.ToString()} {Recipe.Name}"; } else { UpdateRecipe(IngredientOriginal, Ingredients, DirectionOriginal, Directions, isUpdateIngredients, isUpdatedDirections); } if (Recipe == null) { Message = $"Error in {action} Recipe"; return(Page()); } Message = isAddRecipe || isUpdatedMainRecipeInfo ? $"Completed {action} Recipe" : isUpdateIngredients ? $"Completed {action} Ingredient" : isUpdatedDirections ? $"Completed {action} Direction" : $"Error in {action} Recipe"; return(Page()); }