Add update functionality for recipes and create edit page

This commit is contained in:
Nils-Johan Gynther
2026-04-10 17:45:24 +02:00
parent fb68f926b1
commit a1f8fe228c
4 changed files with 354 additions and 2 deletions
+34
View File
@@ -302,6 +302,40 @@ export class RecipesService {
return recipe;
}
async update(id: number, updateRecipeDto: CreateRecipeDto) {
// Först, ta bort gamla ingredienser
await this.prisma.recipeIngredient.deleteMany({
where: { recipeId: id },
});
// Uppdatera receptet och lägg till nya ingredienser
const recipe = await this.prisma.recipe.update({
where: { id },
data: {
name: updateRecipeDto.name,
description: updateRecipeDto.description || null,
instructions: updateRecipeDto.instructions || null,
ingredients: {
create: updateRecipeDto.ingredients.map((ingredient) => ({
productId: ingredient.productId,
quantity: ingredient.quantity.toString(),
unit: ingredient.unit,
note: ingredient.note || null,
})),
},
},
include: {
ingredients: {
include: {
product: true,
},
},
},
});
return recipe;
}
async create(createRecipeDto: CreateRecipeDto) {
const recipe = await this.prisma.recipe.create({
data: {