diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index 113ed01..606afd5 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -216,9 +216,13 @@ public class ServerUtils { updateRecipe(recipe); } - - // how many ingredients are getting used in recipes - + /** + * Gets the amount of recipes this ingredient is being used in. + * @param ingredientId The queried ingredient's ID. + * @return The amount of recipes the ingredient is used in. + * @throws IOException if server query failed. + * @throws InterruptedException if operation is interrupted. + */ public long getIngredientUsage(long ingredientId) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(SERVER + "/ingredients/" + ingredientId + "/usage")) @@ -292,6 +296,22 @@ public class ServerUtils { return objectMapper.readValue(response.body(), Ingredient.class); } + public Ingredient updateIngredient(Ingredient newIngredient) throws IOException, InterruptedException { + logger.info("PATCH ingredient with id: " + newIngredient.getId()); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(SERVER + "/ingredients/" + newIngredient.getId())) + .header("Content-Type", "application/json") + .method( + "PATCH", + HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(newIngredient))) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() != statusOK) { + throw new IOException("Failed to update ingredient with id: " + newIngredient.getId() + " body: " + response.body()); + } + return objectMapper.readValue(response.body(), Ingredient.class); + } +