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
|
||||
Optional<String> result = dialog.showAndWait();
|
||||
|
||||
result.ifPresent(name -> {
|
||||
// Create a new Ingredient object
|
||||
Ingredient newIngredient = new Ingredient();
|
||||
newIngredient.setName(name);
|
||||
|
||||
// Add the new ingredient to the ListView
|
||||
ingredientListView.getItems().add(newIngredient);
|
||||
result.ifPresent(recipeName ->{
|
||||
String trim = recipeName.trim();
|
||||
if(trim.isEmpty()){
|
||||
showError("Invalid Input", "Cannot have empty ingredient name");
|
||||
return;
|
||||
}
|
||||
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 {
|
||||
server.createIngredient(name); // calls POST /api/ingredients
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -278,6 +278,11 @@ public class ServerUtils {
|
|||
.build();
|
||||
|
||||
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) {
|
||||
throw new IOException("Failed to create ingredient. Server responds with: " + response.body());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue