fix error messages and replaced complex code
This commit is contained in:
parent
66cc89f31b
commit
277de13028
1 changed files with 15 additions and 24 deletions
|
|
@ -40,6 +40,11 @@ public class ServerUtils {
|
||||||
.build();
|
.build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
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>>() {
|
return objectMapper.readValue(response.body(), new TypeReference<List<Recipe>>() {
|
||||||
});// JSON string-> List<Recipe> (Jackson)
|
});// JSON string-> List<Recipe> (Jackson)
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +62,7 @@ public class ServerUtils {
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if(response.statusCode() != statusOK){
|
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);
|
return objectMapper.readValue(response.body(),Recipe.class);
|
||||||
|
|
@ -72,28 +77,14 @@ public class ServerUtils {
|
||||||
//Make sure the name of the newRecipe is unique
|
//Make sure the name of the newRecipe is unique
|
||||||
List<Recipe> allRecipes = getRecipes();
|
List<Recipe> allRecipes = getRecipes();
|
||||||
|
|
||||||
boolean exists = allRecipes.stream()
|
|
||||||
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
|
|
||||||
|
|
||||||
if(exists){
|
int version = 1;
|
||||||
int version = 1;
|
String originalName = newRecipe.getName();
|
||||||
String newName;
|
while (allRecipes.stream().anyMatch(r -> r.getName().equals(newRecipe.getName()))) {
|
||||||
|
String newName = originalName + "(" + version++ + ")";
|
||||||
// Giving the recipe a unique name
|
newRecipe.setName(newName);
|
||||||
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++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String json = objectMapper.writeValueAsString(newRecipe);
|
String json = objectMapper.writeValueAsString(newRecipe);
|
||||||
|
|
||||||
//Recipe to backend
|
//Recipe to backend
|
||||||
|
|
@ -105,7 +96,7 @@ public class ServerUtils {
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if(response.statusCode() != statusOK){
|
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);
|
return objectMapper.readValue(response.body(),Recipe.class);
|
||||||
|
|
@ -123,7 +114,7 @@ public class ServerUtils {
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
if(response.statusCode() != statusOK){
|
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
|
//200 is the status code for success, other codes can mean there is no recipe to clone
|
||||||
if(response.statusCode() != statusOK){
|
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 exists so you can make a "new" recipe aka the clone
|
||||||
Recipe recipe = objectMapper.readValue(response.body(), Recipe.class);
|
Recipe recipe = objectMapper.readValue(response.body(), Recipe.class);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue