From 47ffe8c74ddee5ca75db3704fc9784f3e6d92376 Mon Sep 17 00:00:00 2001 From: Anika Raemer Date: Fri, 10 Oct 2025 19:42:16 +0200 Subject: [PATCH] switch to createOrUpdate endpoint --- frontend/src/api/points/RecipePoint.ts | 20 +++++-------------- frontend/src/api/utils/requests.ts | 12 ++--------- .../src/components/recipes/RecipeEditPage.tsx | 8 ++------ 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/frontend/src/api/points/RecipePoint.ts b/frontend/src/api/points/RecipePoint.ts index b437d0f..35b833e 100644 --- a/frontend/src/api/points/RecipePoint.ts +++ b/frontend/src/api/points/RecipePoint.ts @@ -1,5 +1,5 @@ import type { RecipeDto } from "../dtos/RecipeDto"; -import { get, postJson, putJson } from "../utils/requests"; +import { get, postJson } from "../utils/requests"; /** @@ -24,21 +24,11 @@ export async function fetchRecipe(id: string): Promise { } /** - * Create new Recipe - * @param recipe Recipe to create + * Either create a new recipe or update an existing one + * @param recipe Recipe to create or update * @returns Saved recipe */ -export async function createRecipe(recipe: RecipeDto): Promise { - const res = await postJson(RECIPE_URL, JSON.stringify(recipe)); - return res.json(); -} - -/** - * Save an existing recipe - * @param recipe Recipe to save. This recipe must have an ID! - * @returns Saved recipe - */ -export async function updateRecipe(recipe: RecipeDto): Promise { - const res = await putJson(`${RECIPE_URL}/${recipe.id}`, JSON.stringify(recipe)); +export async function createOrUpdateRecipe(recipe: RecipeDto): Promise { + const res = await postJson(RECIPE_URL + "/create-or-update", JSON.stringify(recipe)); return res.json(); } diff --git a/frontend/src/api/utils/requests.ts b/frontend/src/api/utils/requests.ts index 7a335ee..da8cffe 100644 --- a/frontend/src/api/utils/requests.ts +++ b/frontend/src/api/utils/requests.ts @@ -9,7 +9,7 @@ export async function get(url: string) : Promise{ headers: requestHeaders }) if(!response.ok){ - throw new Error("GET to " + url + "failed!") + throw new Error("GET to " + url + " failed with " + response.status + " " + response.statusText); } return response; } @@ -22,14 +22,6 @@ export async function postJson(url: string, requestBody: string, logBody = true) return await persistJson(url, requestBody, "POST"); } -export async function putJson(url: string, requestBody: string, logBody = true) : Promise{ - console.log("PUT to " + url); - if(logBody){ - console.log("body: " + requestBody); - } - return persistJson(url, requestBody, "PUT"); -} - async function persistJson(url: string, requestBody: string, requestMethod: string) : Promise{ const requestHeaders = createBasicHeader(); setContentTypeHeaderJson(requestHeaders); @@ -43,7 +35,7 @@ async function persistJson(url: string, requestBody: string, requestMethod: stri const response = await fetch(request); console.log("response.ok", response.ok) if(!response.ok){ - throw new Error(requestMethod + " to " + url + " failed!") + throw new Error(requestMethod + " to " + url + " failed " + response.status + " " + response.statusText); } return response; } diff --git a/frontend/src/components/recipes/RecipeEditPage.tsx b/frontend/src/components/recipes/RecipeEditPage.tsx index 5660249..3732056 100644 --- a/frontend/src/components/recipes/RecipeEditPage.tsx +++ b/frontend/src/components/recipes/RecipeEditPage.tsx @@ -2,7 +2,7 @@ import { useParams, useNavigate } from "react-router-dom" import { useEffect, useState } from "react" import type { RecipeModel } from "../../models/RecipeModel" import RecipeEditor from "./RecipeEditor" -import { fetchRecipe, createRecipe, updateRecipe } from "../../api/points/RecipePoint" +import { fetchRecipe, createOrUpdateRecipe } from "../../api/points/RecipePoint" import { getRecipeDetailUrl, getRecipeListUrl } from "../../routes" import { mapRecipeDtoToModel, mapRecipeModelToDto } from "../../mappers/recipeMapper" import type { RecipeDto } from "../../api/dtos/RecipeDto" @@ -45,11 +45,7 @@ export default function RecipeEditPage() { const handleSave = async (updated: RecipeModel) => { try { const dto = mapRecipeModelToDto(updated); - if (updated.id) { - await updateRecipe(dto) - } else { - await createRecipe(dto) - } + await createOrUpdateRecipe(dto); navigateBack(); } catch (err) { console.error(err)