Making unique name is now in addRecipe instead
This commit is contained in:
parent
3f3c761356
commit
0df654df3c
1 changed files with 29 additions and 27 deletions
|
|
@ -65,14 +65,35 @@ public class ServerUtils {
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException {
|
public Recipe addRecipe(Recipe newRecipe) throws IOException, InterruptedException {
|
||||||
String json = objectMapper.writeValueAsString(newRecipe);
|
//Make sure the name of the newRecipe is unque
|
||||||
|
List<Recipe> allRecipes = getRecipes();
|
||||||
|
|
||||||
// TODO : check whether the name is unique, else add a number after it
|
boolean exists = allRecipes.stream()
|
||||||
// while(true){
|
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
|
||||||
// if(){
|
|
||||||
// System.out.println("Card name already exists pls enter a new one");
|
|
||||||
// }
|
if(exists){
|
||||||
// }
|
int version = 1;
|
||||||
|
String newName;
|
||||||
|
|
||||||
|
// Giving the "new" 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
String json = objectMapper.writeValueAsString(newRecipe);
|
||||||
|
|
||||||
|
|
||||||
//Recipe to backend
|
//Recipe to backend
|
||||||
|
|
@ -121,31 +142,12 @@ 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("No recipe to clone");
|
||||||
}
|
}
|
||||||
// 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);
|
||||||
|
|
||||||
List<Recipe> allRecipes = getRecipes();
|
|
||||||
int version = 1;
|
|
||||||
String newName;
|
|
||||||
|
|
||||||
// Giving the "new" recipe a unique name
|
|
||||||
while (true) {
|
|
||||||
newName = recipe.getName() + "(" + version + ")";
|
|
||||||
String finalNewName = newName;
|
|
||||||
boolean exists = allRecipes.stream()
|
|
||||||
.anyMatch(r -> r.getName().equals(finalNewName));
|
|
||||||
|
|
||||||
if (!exists){
|
|
||||||
break;
|
|
||||||
}else {
|
|
||||||
version++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
recipe.setId(null); // otherwise the id is the same as the original, and that's wrong
|
recipe.setId(null); // otherwise the id is the same as the original, and that's wrong
|
||||||
recipe.setName(newName);
|
|
||||||
|
|
||||||
return addRecipe(recipe);
|
return addRecipe(recipe);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue