Updating recipes instead of making a new recipe, when adding ingredients
This commit is contained in:
parent
f93cf8f0aa
commit
717684e159
1 changed files with 19 additions and 2 deletions
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue