From b1a8003f9b5e57767a2d0425608a4ee5ddf377d8 Mon Sep 17 00:00:00 2001 From: Mei Chang van der Werff Date: Thu, 27 Nov 2025 03:27:03 +0100 Subject: [PATCH] Implementatation cloneRecipe method --- client/src/main/java/client/DetailView.java | 58 ++++++++++++++++++--- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/client/src/main/java/client/DetailView.java b/client/src/main/java/client/DetailView.java index 5b3cbf0..5e23927 100644 --- a/client/src/main/java/client/DetailView.java +++ b/client/src/main/java/client/DetailView.java @@ -1,11 +1,11 @@ package client; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import commons.Recipe; import java.io.IOException; +import java.io.StringWriter; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -21,16 +21,14 @@ public class DetailView { client = HttpClient.newHttpClient(); } - /** * Gets all the recipes from the backend * @return a JSON string with all the recipes - */ public List findAll() throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(BASE_URL+ "/recipes")) .GET() + .uri(URI.create(BASE_URL+ "/recipes")) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -39,7 +37,6 @@ public class DetailView { return recipes; } - /** * Gets a single recipe based on its id * @param id every recipe has it's unique id @@ -47,23 +44,68 @@ public class DetailView { */ public Recipe findId(long id) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(BASE_URL+"/recipes/" + id)) .GET() + .uri(URI.create(BASE_URL+"/recipes/" + id)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if(response.statusCode() != 200){ + System.out.println("No recipe found with this id"); + return null; + } Recipe recipe = objectMapper.readValue(response.body(),Recipe.class); + return recipe; } + /** + * Deletes a recipe + * @param id the recipe that get deleted + */ public void deleteRecipe(long id) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(BASE_URL + "/recipes/" + id)) .DELETE() + .uri(URI.create(BASE_URL + "/recipes/" + id)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); } + /** + * Clones a recipe + * @param id the id of the recipe to be cloned + * @return a duplicated recipe of the given recipe + */ + public Recipe cloneRecipe(long id) throws IOException, InterruptedException { + //Get the recipe + HttpRequest request = HttpRequest.newBuilder() + .GET() + .uri(URI.create(BASE_URL + "/recipes/" + id)) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + Recipe OGRecipe = objectMapper.readValue(response.body(),Recipe.class); -} + //200 is the status code for success, other codes can mean there is no recipe to clone + if(response.statusCode() != 200){ + return null; + } + + // recipe exists so you can make a "new" recipe aka the clone + Recipe clone = new Recipe(); + String cloneName = OGRecipe.getName() + "(clone)"; // I suppose we want unique names? + clone.setName(cloneName); + clone.setIngredients(OGRecipe.getIngredients()); + clone.setPreparationSteps(OGRecipe.getPreparationSteps()); + + //From String to Json + StringWriter s = new StringWriter(); + objectMapper.writeValue(s, clone); + + // Post the clone to backend + HttpRequest request1 = HttpRequest.newBuilder() + .POST(HttpRequest.BodyPublishers.ofString(s.toString())) + .uri(URI.create(BASE_URL + "/recipes/" + id)) + .build(); + return clone; + } +} \ No newline at end of file