diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index 2a65168..be1eda7 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -130,7 +130,7 @@ public class ServerUtils { //Get the recipe HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(SERVER + "/recipe/" + id)) - .GET() + .GET() //Needs to be changed to POST() when api is changed .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -172,6 +172,23 @@ public class ServerUtils { ingredients.add(ingredient); 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 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); } } \ No newline at end of file