switch to createOrUpdate endpoint

This commit is contained in:
Anika Raemer 2025-10-10 19:42:16 +02:00
parent 8027fce80d
commit 47ffe8c74d
3 changed files with 9 additions and 31 deletions

View file

@ -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<RecipeDto> {
}
/**
* 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<RecipeDto> {
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<RecipeDto> {
const res = await putJson(`${RECIPE_URL}/${recipe.id}`, JSON.stringify(recipe));
export async function createOrUpdateRecipe(recipe: RecipeDto): Promise<RecipeDto> {
const res = await postJson(RECIPE_URL + "/create-or-update", JSON.stringify(recipe));
return res.json();
}

View file

@ -9,7 +9,7 @@ export async function get(url: string) : Promise<Response>{
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<Response>{
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<Response>{
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;
}

View file

@ -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)