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<List<Instruction>> GetInstructionsByRecipeIdAsync(Guid recipeId);
|
||||
|
||||
Task RemoveInstructionFromRecipeAsync(Guid recipeId, Guid instructionId);
|
||||
|
||||
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)
|
||||
{
|
||||
return await _context.Instructions
|
||||
.Include(i => i.Recipe)
|
||||
.Where(i => i.Recipe.Id == recipeId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Instruction> GetInstructionWithRecipeAsync(Guid instructionId)
|
||||
public async Task<List<Instruction>> GetInstructionsByInstructionIdAsync(Guid instructionId)
|
||||
{
|
||||
var instruction = await _context.Instructions
|
||||
.Include(i => i.Recipe)
|
||||
.ThenInclude(r => r.Instructions)
|
||||
.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)
|
||||
{
|
||||
throw new InvalidDataException($"The Recipe for Instruction with ID {instructionId} is not loaded or does not exist.");
|
||||
}
|
||||
|
||||
return instruction;
|
||||
return instruction.Recipe.Instructions.ToList();
|
||||
}
|
||||
|
||||
public async Task SwapInstructionOrderAsync(Instruction a, Instruction b)
|
||||
|
||||
Reference in New Issue
Block a user