added a special warning message when user tries to add duplicate ingredients

This commit is contained in:
Rithvik Sriram 2026-01-15 15:24:21 +01:00
commit b985aa283f
3 changed files with 25 additions and 8 deletions

View file

@ -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");
}
});
}

View file

@ -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());
}
}

View file

@ -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());
}