From b985aa283f9ddd4e9d5e0308bed871d981725bf8 Mon Sep 17 00:00:00 2001 From: Rithvik Sriram Date: Thu, 15 Jan 2026 15:24:21 +0100 Subject: [PATCH] added a special warning message when user tries to add duplicate ingredients --- .../client/scenes/FoodpalApplicationCtrl.java | 19 ++++++++++++------- .../scenes/recipe/IngredientsPopupCtrl.java | 9 ++++++++- .../java/client/utils/server/ServerUtils.java | 5 +++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index 3242d21..d553712 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -534,13 +534,18 @@ public class FoodpalApplicationCtrl implements LocaleAware { // Wait for the user to enter a value Optional 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"); + } }); } diff --git a/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java index de27245..7d6a4f3 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java +++ b/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java @@ -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()); } } diff --git a/client/src/main/java/client/utils/server/ServerUtils.java b/client/src/main/java/client/utils/server/ServerUtils.java index 5330fdd..2d4bcc0 100644 --- a/client/src/main/java/client/utils/server/ServerUtils.java +++ b/client/src/main/java/client/utils/server/ServerUtils.java @@ -278,6 +278,11 @@ public class ServerUtils { .build(); HttpResponse 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()); }