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 {
|
||||
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
|
||||
// while(true){
|
||||
// if(){
|
||||
// System.out.println("Card name already exists pls enter a new one");
|
||||
// }
|
||||
// }
|
||||
boolean exists = allRecipes.stream()
|
||||
.anyMatch(r -> r.getName().equals(newRecipe.getName()));
|
||||
|
||||
|
||||
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
|
||||
|
|
@ -126,26 +147,7 @@ public class ServerUtils {
|
|||
// recipe exists so you can make a "new" recipe aka the clone
|
||||
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.setName(newName);
|
||||
|
||||
return addRecipe(recipe);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue