Add recipe existence check in update method and remove redundant quantity conversion

This commit is contained in:
Nils-Johan Gynther
2026-04-10 18:22:47 +02:00
parent 2efb5b5627
commit 0588c0d3d8
+12 -3
View File
@@ -303,7 +303,16 @@ export class RecipesService {
}
async update(id: number, updateRecipeDto: CreateRecipeDto) {
// Först, ta bort gamla ingredienser
// Verifiera att receptet finns
const existingRecipe = await this.prisma.recipe.findUnique({
where: { id },
});
if (!existingRecipe) {
throw new NotFoundException(`Recipe with id ${id} not found`);
}
// Ta bort gamla ingredienser
await this.prisma.recipeIngredient.deleteMany({
where: { recipeId: id },
});
@@ -318,7 +327,7 @@ export class RecipesService {
ingredients: {
create: updateRecipeDto.ingredients.map((ingredient) => ({
productId: ingredient.productId,
quantity: ingredient.quantity.toString(),
quantity: ingredient.quantity,
unit: ingredient.unit,
note: ingredient.note || null,
})),
@@ -345,7 +354,7 @@ export class RecipesService {
ingredients: {
create: createRecipeDto.ingredients.map((ingredient) => ({
productId: ingredient.productId,
quantity: ingredient.quantity.toString(),
quantity: ingredient.quantity,
unit: ingredient.unit,
note: ingredient.note || null,
})),