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
+9 -1
View File
@@ -1,4 +1,4 @@
import { Body, Controller, Get, Param, ParseIntPipe, Post } from '@nestjs/common';
import { Body, Controller, Get, Param, ParseIntPipe, Post, Patch } from '@nestjs/common';
import { RecipesService } from './recipes.service';
import { CreateRecipeDto } from './dto/create-recipe.dto';
@@ -25,4 +25,12 @@ export class RecipesController {
async create(@Body() createRecipeDto: CreateRecipeDto) {
return this.recipesService.create(createRecipeDto);
}
@Patch(':id')
async update(
@Param('id', ParseIntPipe) id: number,
@Body() createRecipeDto: CreateRecipeDto,
) {
return this.recipesService.update(id, createRecipeDto);
}
}
+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: {