From bcc0a606694c3b6f719cc63c10fc9e29430d6180 Mon Sep 17 00:00:00 2001 From: franc Date: Mon, 28 Apr 2025 13:10:41 +0200 Subject: [PATCH] Update Controller stuff with Repo Methods --- .../Controller/Recipe/RecipeController.cs | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs index 7f31c54..72bf903 100644 --- a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs +++ b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs @@ -188,6 +188,47 @@ return RedirectToAction("Details", new { id = recipeId }); } + // GET: /Recipe/{recipeId}/RemoveInstruction/{instructionId} + [HttpGet("{recipeId}/RemoveInstruction/{instructionId}")] + public async Task RemoveInstruction(Guid recipeId, Guid instructionId) + { + var recipe = await _recipeRepository.GetRecipeAsync(recipeId); + + if (recipe == null) + { + return NotFound("Recipe not found."); + } + + var instruction = recipe.Instructions?.FirstOrDefault(i => i.Id == instructionId); + if (instruction == null) + { + return NotFound("Instruction not found in the specified recipe."); + } + + ViewBag.RecipeId = recipeId; + ViewBag.InstructionId = instructionId; + + return View(); + } + + // POST: /Recipe/{recipeId}/RemoveInstruction/{instructionId} + [HttpPost("{recipeId}/RemoveInstruction/{instructionId}")] + [ValidateAntiForgeryToken] + public async Task RemoveInstructionConfirmed(Guid recipeId, Guid instructionId) + { + try + { + await _instructionRepository.RemoveInstructionFromRecipeAsync(recipeId, instructionId); + TempData["SuccessMessage"] = "Instruction removed successfully."; + return RedirectToAction("Details", new { recipeId }); + } + catch (Exception ex) + { + TempData["ErrorMessage"] = $"An error occurred while removing the instruction: {ex.Message}"; + return RedirectToAction("Details", new { recipeId }); + } + } + // GET: /Recipe/FilterByDifficulty [HttpGet("FilterByDifficulty")] public async Task FilterByDifficulty(Difficulty? selectedDifficulty) @@ -218,11 +259,11 @@ // POST: /Recipe/{recipeId}/AddInstruction [HttpPost("{recipeId}/AddInstruction")] [ValidateAntiForgeryToken] - public async Task AddInstruction(Guid recipeId, string description, int number) + public async Task AddInstruction(Guid recipeId, string description) { try { - await _instructionRepository.CreateInstructionToRecipeAsync(recipeId, description, number); + await _instructionRepository.CreateInstructionToRecipeAsync(recipeId, description); return RedirectToAction("AddInstruction", new { recipeId }); } catch (Exception ex)