Add update functionality for recipes and create edit page
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user