fix: resolve merge conflicts
This commit is contained in:
parent
5388c52aaa
commit
61ad8a1ea9
10 changed files with 151 additions and 77 deletions
|
|
@ -23,7 +23,7 @@ import client.scenes.recipe.IngredientListCtrl;
|
|||
import client.scenes.recipe.RecipeStepListCtrl;
|
||||
import client.utils.ConfigService;
|
||||
import client.utils.LocaleManager;
|
||||
import client.utils.ServerUtils;
|
||||
import client.utils.server.ServerUtils;
|
||||
import client.utils.WebSocketDataService;
|
||||
import client.utils.WebSocketUtils;
|
||||
import com.google.inject.Binder;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package client;
|
|||
|
||||
import client.scenes.FoodpalApplicationCtrl;
|
||||
import client.scenes.MainCtrl;
|
||||
import client.utils.ServerUtils;
|
||||
import client.utils.server.ServerUtils;
|
||||
import com.google.inject.Injector;
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.image.Image;
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import client.utils.ConfigService;
|
|||
import client.utils.DefaultValueFactory;
|
||||
import client.utils.LocaleAware;
|
||||
import client.utils.LocaleManager;
|
||||
import client.utils.ServerUtils;
|
||||
import client.utils.server.ServerUtils;
|
||||
import client.utils.WebSocketDataService;
|
||||
import client.utils.WebSocketUtils;
|
||||
import commons.Ingredient;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package client.scenes;
|
|||
import client.utils.ConfigService;
|
||||
import client.utils.LocaleAware;
|
||||
import client.utils.LocaleManager;
|
||||
import client.utils.ServerUtils;
|
||||
import client.utils.server.ServerUtils;
|
||||
import com.google.inject.Inject;
|
||||
import commons.Recipe;
|
||||
import javafx.animation.PauseTransition;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package client.scenes.recipe;
|
||||
|
||||
import client.utils.ServerUtils;
|
||||
import client.utils.server.ServerUtils;
|
||||
import commons.Ingredient;
|
||||
import jakarta.inject.Inject;
|
||||
import javafx.fxml.FXML;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import client.utils.ConfigService;
|
|||
import client.utils.LocaleAware;
|
||||
import client.utils.LocaleManager;
|
||||
import client.utils.PrintExportService;
|
||||
import client.utils.ServerUtils;
|
||||
import client.utils.server.ServerUtils;
|
||||
import client.utils.WebSocketDataService;
|
||||
import com.google.inject.Inject;
|
||||
import commons.Recipe;
|
||||
|
|
|
|||
87
client/src/main/java/client/utils/server/Endpoints.java
Normal file
87
client/src/main/java/client/utils/server/Endpoints.java
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package client.utils.server;
|
||||
|
||||
import client.utils.ConfigService;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.util.List;
|
||||
|
||||
public class Endpoints {
|
||||
private final ConfigService configService;
|
||||
|
||||
@Inject
|
||||
public Endpoints(ConfigService configService) {
|
||||
this.configService = configService;
|
||||
}
|
||||
|
||||
private HttpRequest.Builder http(String url) {
|
||||
return HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("Content-Type", "application/json");
|
||||
}
|
||||
|
||||
public String baseUrl() {
|
||||
return this.configService.getConfig().getServerUrl();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder fetchAllRecipes(List<String> locales) {
|
||||
String url = this.baseUrl() + "/recipes" + "?locales=" + String.join(",", locales);
|
||||
|
||||
return this.http(url).GET();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder fetchRecipe(long recipeId) {
|
||||
String url = this.baseUrl() + "/recipe/" + recipeId;
|
||||
|
||||
return this.http(url).GET();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder createNewRecipe(HttpRequest.BodyPublisher body) {
|
||||
String url = this.baseUrl() + "/recipe/new";
|
||||
|
||||
return this.http(url).PUT(body);
|
||||
}
|
||||
|
||||
public HttpRequest.Builder updateRecipe(long recipeId, HttpRequest.BodyPublisher body) {
|
||||
String url = this.baseUrl() + "/recipe/" + recipeId;
|
||||
|
||||
return this.http(url).POST(body);
|
||||
}
|
||||
|
||||
public HttpRequest.Builder deleteRecipe(long recipeId) {
|
||||
String url = this.baseUrl() + "/recipe/" + recipeId;
|
||||
|
||||
return this.http(url).DELETE();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder fetchIngredientUsage(long ingredientId) {
|
||||
String url = this.baseUrl() + "/ingredients/" + ingredientId + "/usage";
|
||||
|
||||
return this.http(url).GET();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder deleteIngredient(long ingredientId) {
|
||||
String url = this.baseUrl() + "/ingredients/" + ingredientId;
|
||||
|
||||
return this.http(url).DELETE();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder getIngredients() {
|
||||
String url = this.baseUrl() + "/ingredients";
|
||||
|
||||
return this.http(url).GET();
|
||||
}
|
||||
|
||||
public HttpRequest.Builder createIngredient(HttpRequest.BodyPublisher body) {
|
||||
String url = this.baseUrl() + "/ingredients";
|
||||
|
||||
return this.http(url).POST(body);
|
||||
}
|
||||
|
||||
public HttpRequest.Builder updateIngredient(long ingredientId, HttpRequest.BodyPublisher body) {
|
||||
String url = this.baseUrl() + "/ingredients/" + ingredientId;
|
||||
|
||||
return this.http(url).method("PATCH", body);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package client.utils;
|
||||
package client.utils.server;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
|
@ -13,7 +13,6 @@ import org.glassfish.jersey.client.ClientConfig;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
|
@ -25,15 +24,17 @@ import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
|
|||
|
||||
|
||||
public class ServerUtils {
|
||||
private static final String SERVER = "http://localhost:8080/api";
|
||||
private Logger logger = Logger.getLogger(ServerUtils.class.getName());
|
||||
private final HttpClient client;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final int statusOK = 200;
|
||||
|
||||
private final Endpoints endpoints;
|
||||
|
||||
@Inject
|
||||
public ServerUtils(HttpClient client) {
|
||||
public ServerUtils(HttpClient client, Endpoints endpoints) {
|
||||
this.client = client;
|
||||
this.endpoints = endpoints;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -41,25 +42,16 @@ public class ServerUtils {
|
|||
* @return a JSON string with all the recipes
|
||||
*/
|
||||
public List<Recipe> getRecipes(List<String> locales) throws IOException, InterruptedException {
|
||||
HttpRequest request = this.endpoints.fetchAllRecipes(locales).build();
|
||||
|
||||
String uri =
|
||||
SERVER +
|
||||
"/recipes" +
|
||||
"?locales=" +
|
||||
String.join(",", locales);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(uri))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != statusOK){
|
||||
if (response.statusCode() != statusOK) {
|
||||
throw new IOException("No recipe to get. Server responds with " + response.body());
|
||||
}
|
||||
|
||||
List<Recipe> list = objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
|
||||
});
|
||||
List<Recipe> list = objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {});
|
||||
|
||||
logger.info("Received response from server: " + list);
|
||||
return list; // JSON string-> List<Recipe> (Jackson)
|
||||
}
|
||||
|
|
@ -75,10 +67,8 @@ public class ServerUtils {
|
|||
* @return a singe recipe with the given id
|
||||
*/
|
||||
public Recipe findId(long id) throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER +"/recipe/" + id))
|
||||
.GET()
|
||||
.build();
|
||||
HttpRequest request = this.endpoints.fetchRecipe(id).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != statusOK){
|
||||
|
|
@ -111,11 +101,9 @@ public class ServerUtils {
|
|||
String json = objectMapper.writeValueAsString(newRecipe);
|
||||
|
||||
//Recipe to backend
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/recipe/new"))
|
||||
.header("Content-Type", "application/json")
|
||||
.PUT(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
HttpRequest request =
|
||||
this.endpoints.createNewRecipe(HttpRequest.BodyPublishers.ofString(json)).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != statusOK){
|
||||
|
|
@ -129,10 +117,8 @@ public class ServerUtils {
|
|||
* @param id the recipe that get deleted
|
||||
*/
|
||||
public void deleteRecipe(long id) throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/recipe/" + id))
|
||||
.DELETE()
|
||||
.build();
|
||||
HttpRequest request = this.endpoints.deleteRecipe(id).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != statusOK){
|
||||
|
|
@ -147,10 +133,8 @@ public class ServerUtils {
|
|||
*/
|
||||
public Recipe cloneRecipe(long id) throws IOException, InterruptedException {
|
||||
//Get the recipe
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/recipe/" + id))
|
||||
.GET()
|
||||
.build();
|
||||
HttpRequest request = this.endpoints.fetchRecipe(id).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
//200 is the status code for success, other codes can mean there is no recipe to clone
|
||||
|
|
@ -165,7 +149,7 @@ public class ServerUtils {
|
|||
public boolean isServerAvailable() {
|
||||
try {
|
||||
ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER) //
|
||||
.target(this.endpoints.baseUrl()) //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get();
|
||||
} catch (ProcessingException e) {
|
||||
|
|
@ -194,11 +178,9 @@ public class ServerUtils {
|
|||
public void 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
|
||||
HttpRequest request = this.endpoints.updateRecipe(recipe.getId(), HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if(response.statusCode() != statusOK){
|
||||
|
|
@ -224,10 +206,7 @@ public class ServerUtils {
|
|||
* @throws InterruptedException if operation is interrupted.
|
||||
*/
|
||||
public long getIngredientUsage(long ingredientId) throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/ingredients/" + ingredientId + "/usage"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpRequest request = this.endpoints.fetchIngredientUsage(ingredientId).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() != statusOK) {
|
||||
|
|
@ -245,10 +224,7 @@ public class ServerUtils {
|
|||
|
||||
public void deleteIngredient(long ingredientId) throws IOException, InterruptedException {
|
||||
// Send delete request to remove the ingredient
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/ingredients/" + ingredientId))
|
||||
.DELETE()
|
||||
.build();
|
||||
HttpRequest request = this.endpoints.deleteIngredient(ingredientId).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
|
|
@ -263,10 +239,7 @@ public class ServerUtils {
|
|||
//retrieves the list of ingredients saved to backend
|
||||
|
||||
public List<Ingredient> getIngredients() throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/ingredients"))
|
||||
.GET()
|
||||
.build();
|
||||
HttpRequest request = this.endpoints.getIngredients().build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() != statusOK) {
|
||||
|
|
@ -282,10 +255,7 @@ public class ServerUtils {
|
|||
Ingredient ingredient = new Ingredient(name, 0.0, 0.0, 0.0);
|
||||
String json = objectMapper.writeValueAsString(ingredient);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(SERVER + "/ingredients"))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
HttpRequest request = this.endpoints.createIngredient(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
|
@ -298,21 +268,15 @@ public class ServerUtils {
|
|||
|
||||
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();
|
||||
HttpRequest request = this.endpoints.updateIngredient(
|
||||
newIngredient.getId(),
|
||||
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