Add more methods for improve sort logic
This commit is contained in:
@@ -6,12 +6,14 @@
|
|||||||
{
|
{
|
||||||
Task<Instruction> GetInstructionAsync(Guid instructionId);
|
Task<Instruction> GetInstructionAsync(Guid instructionId);
|
||||||
|
|
||||||
Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number);
|
Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description);
|
||||||
|
|
||||||
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
||||||
|
|
||||||
Task SwapInstructionNumbersAsync(Instruction a, Instruction b);
|
Task SwapInstructionNumbersAsync(Instruction a, Instruction b);
|
||||||
|
|
||||||
Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid recipeId);
|
Task<List<Instruction>> GetInstructionsOfRecipeAsync(Guid recipeId);
|
||||||
|
|
||||||
|
Task RenumberInstructionsAsync(Guid recipeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,25 +22,30 @@
|
|||||||
return instruction ?? throw new InvalidDataException($"Instruction {instructionId} not found.");
|
return instruction ?? throw new InvalidDataException($"Instruction {instructionId} not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number)
|
public async Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description)
|
||||||
{
|
{
|
||||||
var recipe = await _recipeRepository.GetRecipeAsync(recipeId);
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(description))
|
if (string.IsNullOrWhiteSpace(description))
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Description cannot be empty", nameof(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
|
var newInstruction = new Instruction
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
Id = Guid.NewGuid(),
|
||||||
Description = description,
|
Description = description,
|
||||||
Number = number,
|
Number = nextNumber,
|
||||||
Recipe = recipe,
|
Recipe = recipe,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -65,8 +70,18 @@
|
|||||||
|
|
||||||
if (instructionToRemove != null)
|
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);
|
recipe.Instructions?.Remove(instructionToRemove);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
await RenumberInstructionsAsync(recipeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +100,21 @@
|
|||||||
return instructions;
|
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)
|
public async Task SwapInstructionNumbersAsync(Instruction a, Instruction b)
|
||||||
{
|
{
|
||||||
if (a == null)
|
if (a == null)
|
||||||
|
|||||||
Reference in New Issue
Block a user