diff --git a/client/src/main/java/client/utils/ServerUtils.java b/client/src/main/java/client/utils/ServerUtils.java index cd1023c..408ae11 100644 --- a/client/src/main/java/client/utils/ServerUtils.java +++ b/client/src/main/java/client/utils/ServerUtils.java @@ -40,6 +40,11 @@ public class ServerUtils { .build(); HttpResponse 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>() { });// JSON string-> List (Jackson) } @@ -57,7 +62,7 @@ public class ServerUtils { HttpResponse 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 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){ - newRecipe.setName(newName); - break; - }else { - version++; - } - } + int version = 1; + String originalName = newRecipe.getName(); + while (allRecipes.stream().anyMatch(r -> r.getName().equals(newRecipe.getName()))) { + String newName = originalName + "(" + version++ + ")"; + newRecipe.setName(newName); } + String json = objectMapper.writeValueAsString(newRecipe); //Recipe to backend @@ -105,7 +96,7 @@ public class ServerUtils { HttpResponse 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 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);