fix error messages and replaced complex code

This commit is contained in:
Mei Chang van der Werff 2025-11-28 17:16:02 +01:00
commit 277de13028

View file

@ -40,6 +40,11 @@ public class ServerUtils {
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != statusOK){
throw new IOException("No recipe to get. Server responds with " + response.statusCode());
}
return objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
});// JSON string-> List<Recipe> (Jackson)
}
@ -57,7 +62,7 @@ public class ServerUtils {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != statusOK){
throw new IOException("No recipe found with this id");
throw new IOException("failed finding recipe with id: "+ id + " body: " + response.statusCode());
}
return objectMapper.readValue(response.body(),Recipe.class);
@ -72,28 +77,14 @@ public class ServerUtils {
//Make sure the name of the newRecipe is unique
List<Recipe> allRecipes = getRecipes();
boolean exists = allRecipes.stream()
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
if(exists){
int version = 1;
String newName;
// Giving the recipe a unique name
while (true) {
newName = newRecipe.getName() + "(" + version + ")";
String finalNewName = newName;
boolean nameExists = allRecipes.stream()
.anyMatch(r -> r.getName().equals(finalNewName));
if (!nameExists){
String originalName = newRecipe.getName();
while (allRecipes.stream().anyMatch(r -> r.getName().equals(newRecipe.getName()))) {
String newName = originalName + "(" + version++ + ")";
newRecipe.setName(newName);
break;
}else {
version++;
}
}
}
String json = objectMapper.writeValueAsString(newRecipe);
//Recipe to backend
@ -105,7 +96,7 @@ public class ServerUtils {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != statusOK){
throw new IOException("No recipe to add");
throw new IOException("Failed to add Recipe: " + newRecipe.getId() + "body: " + response.statusCode());
}
return objectMapper.readValue(response.body(),Recipe.class);
@ -123,7 +114,7 @@ public class ServerUtils {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if(response.statusCode() != statusOK){
throw new IOException("No recipe to delete");
throw new IOException("Failed removing recipe with id: " + id + "body: " + response.statusCode());
}
}
@ -142,7 +133,7 @@ public class ServerUtils {
//200 is the status code for success, other codes can mean there is no recipe to clone
if(response.statusCode() != statusOK){
throw new IOException("No recipe to clone");
throw new IOException("Failed to get recipe to clone with id: " + id + "body: " + response.statusCode());
}
// recipe exists so you can make a "new" recipe aka the clone
Recipe recipe = objectMapper.readValue(response.body(), Recipe.class);