feat(client/utils): create update PATCH util function

This commit is contained in:
Zhongheng Liu 2026-01-15 17:08:45 +01:00
commit b1bb4ad242
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -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<String> 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);
}