From 2f879d987bf8f948c530512ce0389e89940b2c19 Mon Sep 17 00:00:00 2001 From: franc Date: Mon, 28 Apr 2025 13:11:28 +0200 Subject: [PATCH] Add more methods for improve sort logic --- .../Instruction/IInstructionRepository.cs | 4 +- .../Instruction/InstructionRepository.cs | 42 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs b/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs index 2b17a6f..ef8094f 100644 --- a/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs +++ b/Francesco.Recipes.World/Repositories/Instruction/IInstructionRepository.cs @@ -6,12 +6,14 @@ { Task GetInstructionAsync(Guid instructionId); - Task CreateInstructionToRecipeAsync(Guid recipeId, string description, int number); + Task CreateInstructionToRecipeAsync(Guid recipeId, string description); Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId); Task SwapInstructionNumbersAsync(Instruction a, Instruction b); Task> GetInstructionsOfRecipeAsync(Guid recipeId); + + Task RenumberInstructionsAsync(Guid recipeId); } } diff --git a/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs b/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs index e94fd34..6844c0a 100644 --- a/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs +++ b/Francesco.Recipes.World/Repositories/Instruction/InstructionRepository.cs @@ -22,25 +22,30 @@ return instruction ?? throw new InvalidDataException($"Instruction {instructionId} not found."); } - public async Task CreateInstructionToRecipeAsync(Guid recipeId, string description, int number) + public async Task CreateInstructionToRecipeAsync(Guid recipeId, string description) { - var recipe = await _recipeRepository.GetRecipeAsync(recipeId); - if (string.IsNullOrWhiteSpace(description)) { throw new ArgumentException("Description cannot be empty", nameof(description)); } - if (number <= 0) + var recipe = await _context.Recipes + .Include(r => r.Instructions) + .FirstOrDefaultAsync(r => r.Id == recipeId); + + if (recipe == null) { - throw new ArgumentOutOfRangeException(nameof(number), "Number must be greater than 0."); + throw new ArgumentException("Recipe not found.", nameof(recipeId)); } + var nextNumber = recipe.Instructions?.Max(i => i.Number) ?? 0; + nextNumber++; + var newInstruction = new Instruction { Id = Guid.NewGuid(), Description = description, - Number = number, + Number = nextNumber, Recipe = recipe, }; @@ -65,8 +70,18 @@ if (instructionToRemove != null) { + await _context.Entry(instructionToRemove) + .Collection(i => i.MediaFiles) + .LoadAsync(); + + if (instructionToRemove.MediaFiles != null && instructionToRemove.MediaFiles.Any()) + { + _context.MediaFiles.RemoveRange(instructionToRemove.MediaFiles); + } + recipe.Instructions?.Remove(instructionToRemove); await _context.SaveChangesAsync(); + await RenumberInstructionsAsync(recipeId); } } @@ -85,6 +100,21 @@ return instructions; } + public async Task RenumberInstructionsAsync(Guid recipeId) + { + var instructions = await _context.Instructions + .Where(i => i.Recipe.Id == recipeId) + .OrderBy(i => i.Number) + .ToListAsync(); + + for (var i = 0; i < instructions.Count; i++) + { + instructions[i].Number = i + 1; + } + + await _context.SaveChangesAsync(); + } + public async Task SwapInstructionNumbersAsync(Instruction a, Instruction b) { if (a == null)