Implementatation cloneRecipe method
This commit is contained in:
parent
57d817970e
commit
b1a8003f9b
1 changed files with 50 additions and 8 deletions
|
|
@ -1,11 +1,11 @@
|
||||||
package client;
|
package client;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
|
|
@ -21,16 +21,14 @@ public class DetailView {
|
||||||
client = HttpClient.newHttpClient();
|
client = HttpClient.newHttpClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all the recipes from the backend
|
* Gets all the recipes from the backend
|
||||||
* @return a JSON string with all the recipes
|
* @return a JSON string with all the recipes
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public List<Recipe> findAll() throws IOException, InterruptedException {
|
public List<Recipe> findAll() throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(BASE_URL+ "/recipes"))
|
|
||||||
.GET()
|
.GET()
|
||||||
|
.uri(URI.create(BASE_URL+ "/recipes"))
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
|
|
@ -39,7 +37,6 @@ public class DetailView {
|
||||||
return recipes;
|
return recipes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a single recipe based on its id
|
* Gets a single recipe based on its id
|
||||||
* @param id every recipe has it's unique 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 {
|
public Recipe findId(long id) throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(BASE_URL+"/recipes/" + id))
|
|
||||||
.GET()
|
.GET()
|
||||||
|
.uri(URI.create(BASE_URL+"/recipes/" + id))
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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);
|
Recipe recipe = objectMapper.readValue(response.body(),Recipe.class);
|
||||||
|
|
||||||
return recipe;
|
return recipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a recipe
|
||||||
|
* @param id the recipe that get deleted
|
||||||
|
*/
|
||||||
public void deleteRecipe(long id) throws IOException, InterruptedException {
|
public void deleteRecipe(long id) throws IOException, InterruptedException {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create(BASE_URL + "/recipes/" + id))
|
|
||||||
.DELETE()
|
.DELETE()
|
||||||
|
.uri(URI.create(BASE_URL + "/recipes/" + id))
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue