feat(client/utils): create update PATCH util function
This commit is contained in:
parent
6b851767e7
commit
b1bb4ad242
1 changed files with 23 additions and 3 deletions
|
|
@ -216,9 +216,13 @@ public class ServerUtils {
|
||||||
updateRecipe(recipe);
|
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 {
|
public long getIngredientUsage(long ingredientId) throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(SERVER + "/ingredients/" + ingredientId + "/usage"))
|
.uri(URI.create(SERVER + "/ingredients/" + ingredientId + "/usage"))
|
||||||
|
|
@ -292,6 +296,22 @@ public class ServerUtils {
|
||||||
return objectMapper.readValue(response.body(), Ingredient.class);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue