diff --git a/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs b/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs index d86aadf..4ce79ef 100644 --- a/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs +++ b/Francesco.Recipes.World/Controller/Instruction/InstructionController.cs @@ -1,6 +1,65 @@ namespace Francesco.Recipes.World.Controller.Instruction { - public class InstructionController + using Francesco.Recipes.World.Repositories.Instruction; + using Francesco.Recipes.World.Services.Instruction; + using Microsoft.AspNetCore.Mvc; + + public class InstructionController : Controller { + private readonly IInstructionService _instructionService; + private readonly IInstructionRepository _instructionRepository; + + public InstructionController(IInstructionService instructionService, IInstructionRepository instructionRepository) + { + _instructionService = instructionService; + _instructionRepository = instructionRepository; + } + + [HttpPost("Recipe/{recipeId}/Instruction/{instructionId}/move-up")] + [ValidateAntiForgeryToken] + public async Task MoveUp(Guid recipeId, Guid instructionId) + { + try + { + await _instructionService.MoveInstructionUpAsync(recipeId, instructionId); + return Ok(new { Message = "Instruction moved up successfully." }); + } + catch (Exception ex) + { + return BadRequest(new { Error = ex.Message }); + } + } + + [HttpPost("Recipe/{recipeId}/Instruction/{instructionId}/move-down")] + [ValidateAntiForgeryToken] + public async Task MoveDown(Guid recipeId, Guid instructionId) + { + try + { + await _instructionService.MoveInstructionDownAsync(recipeId, instructionId); + return Ok(new { Message = "Instruction moved down successfully." }); + } + catch (Exception ex) + { + return BadRequest(new { Error = ex.Message }); + } + } + + [HttpGet("Recipe/{recipeId}/Instructions")] + public async Task GetInstructions(Guid recipeId) + { + try + { + var instructions = await _instructionRepository.GetInstructionsOfRecipeAsync(recipeId); + var sortedInstructions = instructions.OrderBy(i => i.Number).ToList(); + ViewData["RecipeId"] = recipeId; + + return View("~/Views/Shared/_GetInstructions.cshtml", sortedInstructions); + } + catch (Exception ex) + { + return BadRequest(new { Error = ex.Message }); + } + } } } diff --git a/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs b/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs index 5e2ebde..c624b4a 100644 --- a/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs +++ b/Francesco.Recipes.World/Controller/MediaFile/MediaFileController.cs @@ -1,6 +1,6 @@ namespace Francesco.Recipes.World.Controller.MediaFile { - using Francesco.Recipes.World.Data; + using Francesco.Recipes.World.Repositories.MediaFile; using Microsoft.AspNetCore.Mvc; @@ -9,7 +9,7 @@ { private readonly IMediaFileRepository _mediaFileRepository; - public MediaFileController(IMediaFileRepository mediaFileRepository, FrancescosRecipesWorldDbContext context) + public MediaFileController(IMediaFileRepository mediaFileRepository) { _mediaFileRepository = mediaFileRepository; } diff --git a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs index 24d9618..7f31c54 100644 --- a/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs +++ b/Francesco.Recipes.World/Controller/Recipe/RecipeController.cs @@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; - public class RecipeController : Controller { private readonly IRecipeRepository _recipeRepository; diff --git a/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs b/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs index e1fd1e6..2b17a6f 100644 --- a/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs +++ b/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs @@ -12,6 +12,6 @@ Task SwapInstructionNumbersAsync(Instruction a, Instruction b); - Task> GetInstructionsOfRecipeAsync(Guid instructionId); + Task> GetInstructionsOfRecipeAsync(Guid recipeId); } } diff --git a/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs b/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs index 9a4445d..e94fd34 100644 --- a/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs +++ b/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs @@ -70,19 +70,19 @@ } } - public async Task> GetInstructionsOfRecipeAsync(Guid instructionId) + public async Task> GetInstructionsOfRecipeAsync(Guid recipeId) { - var instruction = await _context.Instructions - .Include(i => i.Recipe) - .ThenInclude(r => r.Instructions) - .FirstOrDefaultAsync(i => i.Id == instructionId); + var instructions = await _context.Instructions + .Where(i => i.Recipe.Id == recipeId) + .OrderBy(i => i.Number) + .ToListAsync(); - if (instruction?.Recipe == null) + if (!instructions.Any()) { - throw new InvalidDataException($"Instruction with ID {instructionId} or its Recipe not found."); + throw new InvalidDataException($"No instructions found for Recipe ID {recipeId}."); } - return instruction.Recipe.Instructions.ToList(); + return instructions; } public async Task SwapInstructionNumbersAsync(Instruction a, Instruction b) diff --git a/Francesco.Recipes.World/Views/Shared/_GetInstructions.cshtml b/Francesco.Recipes.World/Views/Shared/_GetInstructions.cshtml new file mode 100644 index 0000000..251b4fc --- /dev/null +++ b/Francesco.Recipes.World/Views/Shared/_GetInstructions.cshtml @@ -0,0 +1,89 @@ +@Html.AntiForgeryToken() +
+ @for (int i = 0; i < Model.Count; i++) + { +
+
+ + + +
+
+ + +
+
+ } +
+ + + +@section Scripts { + +}