Add recipe existence check in update method and remove redundant quantity conversion
This commit is contained in:
@@ -303,7 +303,16 @@ export class RecipesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async update(id: number, updateRecipeDto: CreateRecipeDto) {
|
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({
|
await this.prisma.recipeIngredient.deleteMany({
|
||||||
where: { recipeId: id },
|
where: { recipeId: id },
|
||||||
});
|
});
|
||||||
@@ -318,7 +327,7 @@ export class RecipesService {
|
|||||||
ingredients: {
|
ingredients: {
|
||||||
create: updateRecipeDto.ingredients.map((ingredient) => ({
|
create: updateRecipeDto.ingredients.map((ingredient) => ({
|
||||||
productId: ingredient.productId,
|
productId: ingredient.productId,
|
||||||
quantity: ingredient.quantity.toString(),
|
quantity: ingredient.quantity,
|
||||||
unit: ingredient.unit,
|
unit: ingredient.unit,
|
||||||
note: ingredient.note || null,
|
note: ingredient.note || null,
|
||||||
})),
|
})),
|
||||||
@@ -345,7 +354,7 @@ export class RecipesService {
|
|||||||
ingredients: {
|
ingredients: {
|
||||||
create: createRecipeDto.ingredients.map((ingredient) => ({
|
create: createRecipeDto.ingredients.map((ingredient) => ({
|
||||||
productId: ingredient.productId,
|
productId: ingredient.productId,
|
||||||
quantity: ingredient.quantity.toString(),
|
quantity: ingredient.quantity,
|
||||||
unit: ingredient.unit,
|
unit: ingredient.unit,
|
||||||
note: ingredient.note || null,
|
note: ingredient.note || null,
|
||||||
})),
|
})),
|
||||||
|
|||||||
Reference in New Issue
Block a user