diff --git a/client/src/main/java/client/exception/DuplicateIngredientException.java b/client/src/main/java/client/exception/DuplicateIngredientException.java new file mode 100644 index 0000000..af04346 --- /dev/null +++ b/client/src/main/java/client/exception/DuplicateIngredientException.java @@ -0,0 +1,20 @@ +package client.exception; + +public class DuplicateIngredientException extends Exception{ + private final String ingredientName; + + public DuplicateIngredientException(String ingredientName){ + super("An ingredient with name " + ingredientName + " already exists, please provide a different name"); + this.ingredientName = ingredientName; + } + + public DuplicateIngredientException(String ingredientName, String message){ + super(message); + this.ingredientName = ingredientName; + } + + public String getIngredientName() { + return ingredientName; + } + +} 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/Ingredient/IngredientListCtrl.java b/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java index 329eb56..38e6077 100644 --- a/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java +++ b/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java @@ -1,5 +1,6 @@ package client.scenes.Ingredient; +import client.exception.DuplicateIngredientException; import client.scenes.nutrition.NutritionDetailsCtrl; import client.utils.LocaleAware; import client.utils.LocaleManager; @@ -113,6 +114,8 @@ public class IngredientListCtrl implements LocaleAware { refresh(); // reload list from server } catch (IOException | InterruptedException e) { showError("Failed to create ingredient: " + e.getMessage()); + } catch (DuplicateIngredientException e) { + throw new RuntimeException(e); } } diff --git a/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java index de27245..091befd 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java +++ b/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java @@ -1,6 +1,7 @@ package client.scenes.recipe; import client.utils.server.ServerUtils; +import client.exception.DuplicateIngredientException; import commons.Ingredient; import jakarta.inject.Inject; import javafx.fxml.FXML; @@ -67,8 +68,15 @@ public class IngredientsPopupCtrl { server.createIngredient(name); // calls POST /api/ingredients refresh(); // reload list from server } catch (IOException | InterruptedException e) { + showError("Failed to create ingredient: " + e.getMessage()); + } catch (DuplicateIngredientException e) { + showError("An ingredient with the name " + name + " already exists." + + " Please provide a different name."); //checks if error received has the DUPLICATE string and creates a showError instance with an appropriate error message and description + } + + } diff --git a/client/src/main/java/client/utils/server/ServerUtils.java b/client/src/main/java/client/utils/server/ServerUtils.java index b82d478..8bd7f3b 100644 --- a/client/src/main/java/client/utils/server/ServerUtils.java +++ b/client/src/main/java/client/utils/server/ServerUtils.java @@ -1,6 +1,7 @@ package client.utils.server; import client.utils.ConfigService; +import client.exception.DuplicateIngredientException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.Inject; @@ -267,7 +268,7 @@ public class ServerUtils { //creates new ingredients in the ingredient list - public Ingredient createIngredient(String name) throws IOException, InterruptedException { + public Ingredient createIngredient(String name) throws IOException, InterruptedException, DuplicateIngredientException { Ingredient ingredient = new Ingredient(name, 0.0, 0.0, 0.0); String json = objectMapper.writeValueAsString(ingredient); @@ -275,6 +276,11 @@ public class ServerUtils { .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + final int DUPLICATE_STATUS_CODE = 409; + if (response.statusCode() == DUPLICATE_STATUS_CODE) { + throw new DuplicateIngredientException(name); + } + if (response.statusCode() != statusOK) { throw new IOException("Failed to create ingredient. Server responds with: " + response.body()); }