Updating recipes instead of making a new recipe, when adding ingredients

This commit is contained in:
Mei Chang van der Werff 2025-12-02 16:50:20 +01:00
commit 717684e159

View file

@ -130,7 +130,7 @@ public class ServerUtils {
//Get the recipe //Get the recipe
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(SERVER + "/recipe/" + id)) .uri(URI.create(SERVER + "/recipe/" + id))
.GET() .GET() //Needs to be changed to POST() when api is changed
.build(); .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
@ -172,6 +172,23 @@ public class ServerUtils {
ingredients.add(ingredient); ingredients.add(ingredient);
recipe.setIngredients(ingredients); recipe.setIngredients(ingredients);
return addRecipe(recipe); return updateRecipe(recipe); //updates the recipe instead of creating an whole new recipe
}
public Recipe updateRecipe(Recipe recipe) throws IOException, InterruptedException {
String json = objectMapper.writeValueAsString(recipe);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(SERVER + "/recipe/" + recipe.getId()))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString((json))) // Needs to be changed to PUT() when api changed
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != statusOK){
throw new IOException("Failed to update recipe: " + recipe.toDetailedString() + "body: " + response.body());
}
return objectMapper.readValue(response.body(),Recipe.class);
} }
} }