Implementatation cloneRecipe method

This commit is contained in:
Mei Chang van der Werff 2025-11-27 03:27:03 +01:00
commit b1a8003f9b

View file

@ -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<Recipe> findAll() throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL+ "/recipes"))
.GET()
.uri(URI.create(BASE_URL+ "/recipes"))
.build();
HttpResponse<String> 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<String> 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<String> 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<String> 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;
}
}