Refactoring some code and change logic and make sure to reduce db calls and avoid redundants
This commit is contained in:
@@ -8,12 +8,10 @@
|
|||||||
|
|
||||||
Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number);
|
Task<Instruction> CreateInstructionToRecipeAsync(Guid recipeId, string description, int number);
|
||||||
|
|
||||||
Task<List<Instruction>> GetInstructionsByRecipeIdAsync(Guid recipeId);
|
|
||||||
|
|
||||||
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
||||||
|
|
||||||
Task SwapInstructionOrderAsync(Instruction a, Instruction b);
|
Task SwapInstructionOrderAsync(Instruction a, Instruction b);
|
||||||
|
|
||||||
Task<Instruction> GetInstructionWithRecipeAsync(Guid instructionId);
|
Task<List<Instruction>> GetInstructionsByInstructionIdAsync(Guid instructionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,32 +70,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Instruction>> GetInstructionsByRecipeIdAsync(Guid recipeId)
|
public async Task<List<Instruction>> GetInstructionsByInstructionIdAsync(Guid instructionId)
|
||||||
{
|
|
||||||
return await _context.Instructions
|
|
||||||
.Include(i => i.Recipe)
|
|
||||||
.Where(i => i.Recipe.Id == recipeId)
|
|
||||||
.ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Instruction> GetInstructionWithRecipeAsync(Guid instructionId)
|
|
||||||
{
|
{
|
||||||
var instruction = await _context.Instructions
|
var instruction = await _context.Instructions
|
||||||
.Include(i => i.Recipe)
|
.Include(i => i.Recipe)
|
||||||
.ThenInclude(r => r.Instructions)
|
.ThenInclude(r => r.Instructions)
|
||||||
.FirstOrDefaultAsync(i => i.Id == instructionId);
|
.FirstOrDefaultAsync(i => i.Id == instructionId);
|
||||||
|
|
||||||
if (instruction == null)
|
if (instruction?.Recipe == null)
|
||||||
{
|
{
|
||||||
throw new InvalidDataException($"Instruction with ID {instructionId} not found.");
|
throw new InvalidDataException($"Instruction with ID {instructionId} or its Recipe not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (instruction.Recipe == null)
|
return instruction.Recipe.Instructions.ToList();
|
||||||
{
|
|
||||||
throw new InvalidDataException($"The Recipe for Instruction with ID {instructionId} is not loaded or does not exist.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return instruction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SwapInstructionOrderAsync(Instruction a, Instruction b)
|
public async Task SwapInstructionOrderAsync(Instruction a, Instruction b)
|
||||||
|
|||||||
@@ -5,5 +5,7 @@
|
|||||||
Task MoveInstructionUpAsync(Guid instructionId);
|
Task MoveInstructionUpAsync(Guid instructionId);
|
||||||
|
|
||||||
Task MoveInstructionDownAsync(Guid instructionId);
|
Task MoveInstructionDownAsync(Guid instructionId);
|
||||||
|
|
||||||
|
Task MoveInstructionAsync(Guid instructionId, bool moveUp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,39 +11,33 @@ namespace Francesco.Recipes.World.Services.Instruction
|
|||||||
_instructionRepository = instructionRepository;
|
_instructionRepository = instructionRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task MoveInstructionDownAsync(Guid instructionId)
|
public Task MoveInstructionUpAsync(Guid instructionId)
|
||||||
|
=> MoveInstructionAsync(instructionId, moveUp: true);
|
||||||
|
|
||||||
|
public Task MoveInstructionDownAsync(Guid instructionId)
|
||||||
|
=> MoveInstructionAsync(instructionId, moveUp: false);
|
||||||
|
|
||||||
|
private async Task MoveInstructionAsync(Guid instructionId, bool moveUp)
|
||||||
{
|
{
|
||||||
var instruction = await _instructionRepository.GetInstructionWithRecipeAsync(instructionId);
|
var instructions = await _instructionRepository.GetInstructionsByInstructionIdAsync(instructionId);
|
||||||
|
|
||||||
var instructions = await _instructionRepository.GetInstructionsByRecipeIdAsync(instruction.Recipe.Id);
|
var instruction = instructions.FirstOrDefault(i => i.Id == instructionId);
|
||||||
|
|
||||||
|
if (instruction == null)
|
||||||
|
{
|
||||||
|
throw new InvalidDataException($"Instruction with ID {instructionId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var minStep = 1;
|
||||||
var maxStep = instructions.Max(i => i.Number);
|
var maxStep = instructions.Max(i => i.Number);
|
||||||
|
|
||||||
if (instruction.Number >= maxStep)
|
if ((moveUp && instruction.Number == minStep) || (!moveUp && instruction.Number >= maxStep))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var neighbor = instructions.FirstOrDefault(i => i.Number == instruction.Number + 1);
|
var targetNumber = moveUp ? instruction.Number - 1 : instruction.Number + 1;
|
||||||
|
var neighbor = instructions.FirstOrDefault(i => i.Number == targetNumber);
|
||||||
if (neighbor != null)
|
|
||||||
{
|
|
||||||
await _instructionRepository.SwapInstructionOrderAsync(instruction, neighbor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task MoveInstructionUpAsync(Guid instructionId)
|
|
||||||
{
|
|
||||||
var instruction = await _instructionRepository.GetInstructionWithRecipeAsync(instructionId);
|
|
||||||
|
|
||||||
if (instruction.Number == 1)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var instructions = await _instructionRepository.GetInstructionsByRecipeIdAsync(instruction.Recipe.Id);
|
|
||||||
|
|
||||||
var neighbor = instructions.FirstOrDefault(i => i.Number == instruction.Number - 1);
|
|
||||||
|
|
||||||
if (neighbor != null)
|
if (neighbor != null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user