added a special warning message when user tries to add duplicate ingredients
This commit is contained in:
parent
274a48c5c2
commit
b985aa283f
3 changed files with 25 additions and 8 deletions
|
|
@ -534,13 +534,18 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
// Wait for the user to enter a value
|
// Wait for the user to enter a value
|
||||||
Optional<String> result = dialog.showAndWait();
|
Optional<String> result = dialog.showAndWait();
|
||||||
|
|
||||||
result.ifPresent(name -> {
|
result.ifPresent(recipeName ->{
|
||||||
// Create a new Ingredient object
|
String trim = recipeName.trim();
|
||||||
Ingredient newIngredient = new Ingredient();
|
if(trim.isEmpty()){
|
||||||
newIngredient.setName(name);
|
showError("Invalid Input", "Cannot have empty ingredient name");
|
||||||
|
return;
|
||||||
// Add the new ingredient to the ListView
|
}
|
||||||
ingredientListView.getItems().add(newIngredient);
|
if(ingredientListView.getItems().stream()
|
||||||
|
.map(Ingredient::getName)
|
||||||
|
.anyMatch(ingName -> ingName.equalsIgnoreCase(trim))){
|
||||||
|
showError("Duplicate Ingredient", "Cannot have duplicate ingredients," +
|
||||||
|
" Please provide unique Ingredient names");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,14 @@ public class IngredientsPopupCtrl {
|
||||||
try {
|
try {
|
||||||
server.createIngredient(name); // calls POST /api/ingredients
|
server.createIngredient(name); // calls POST /api/ingredients
|
||||||
refresh(); // reload list from server
|
refresh(); // reload list from server
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException e) {
|
||||||
|
if (e.getMessage() != null && e.getMessage().startsWith("DUPLICATE:")) {
|
||||||
|
showError("An ingredient with the name" + name + " already exists." +
|
||||||
|
" Please provide a different name.");
|
||||||
|
} else {
|
||||||
|
showError("Failed to create ingredient: " + e.getMessage());
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e){
|
||||||
showError("Failed to create ingredient: " + e.getMessage());
|
showError("Failed to create ingredient: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -278,6 +278,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() == 409) {
|
||||||
|
throw new IOException("An ingredient with the name '" + name + "' already exists.");
|
||||||
|
}
|
||||||
|
|
||||||
if (response.statusCode() != statusOK) {
|
if (response.statusCode() != statusOK) {
|
||||||
throw new IOException("Failed to create ingredient. Server responds with: " + response.body());
|
throw new IOException("Failed to create ingredient. Server responds with: " + response.body());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue