131 lines
4.3 KiB
Java
131 lines
4.3 KiB
Java
package client.Ingredient;
|
|
|
|
import commons.Ingredient;
|
|
import javafx.scene.control.Alert;
|
|
import javafx.scene.control.ButtonType;
|
|
import javafx.scene.control.Alert.AlertType;
|
|
import javafx.event.ActionEvent;
|
|
import javafx.fxml.FXML;
|
|
import javafx.scene.control.ListView;
|
|
import javafx.scene.control.Button;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
public class IngredientController {
|
|
|
|
@FXML
|
|
private ListView<Ingredient> ingredientListView;
|
|
|
|
@FXML
|
|
private Button deleteButton;
|
|
|
|
private final RestTemplate restTemplate = new RestTemplate(); // Simplified REST client
|
|
|
|
@FXML
|
|
private void handleDeleteIngredient(ActionEvent event) {
|
|
// Get selected ingredient
|
|
Ingredient selectedIngredient = ingredientListView.getSelectionModel().getSelectedItem();
|
|
if (selectedIngredient == null) {
|
|
showError("No ingredient selected", "Please select an ingredient to delete.");
|
|
return;
|
|
}
|
|
|
|
// Check if the ingredient is used in any recipe
|
|
checkIngredientUsage(selectedIngredient);
|
|
}
|
|
|
|
private void checkIngredientUsage(Ingredient ingredient) {
|
|
String url = "http://localhost:8080/api/ingredients/" + ingredient.getId() + "/usage";
|
|
|
|
ResponseEntity<IngredientUsageResponse> response = restTemplate.getForEntity(url, IngredientUsageResponse.class);
|
|
|
|
if (response.getStatusCode() == HttpStatus.NOT_FOUND) {
|
|
showError("Ingredient not found", "The ingredient does not exist.");
|
|
return;
|
|
}
|
|
|
|
IngredientUsageResponse usageResponse = response.getBody();
|
|
long usedInRecipes = usageResponse != null ? usageResponse.getUsedInRecipes() : 0;
|
|
|
|
if (usedInRecipes > 0) {
|
|
// If ingredient is in use, show warning to the user
|
|
showWarningDialog(ingredient, usedInRecipes);
|
|
} else {
|
|
// delete if not in use
|
|
deleteIngredient(ingredient);
|
|
}
|
|
}
|
|
|
|
private void showWarningDialog(Ingredient ingredient, long usedInRecipes) {
|
|
Alert alert = new Alert(AlertType.WARNING);
|
|
alert.setTitle("Warning");
|
|
alert.setHeaderText("Ingredient in Use");
|
|
alert.setContentText("The ingredient '" + ingredient.getName() + "' is used in " + usedInRecipes + " recipe(s). Are you sure you want to delete it?");
|
|
|
|
ButtonType deleteButton = new ButtonType("Delete Anyway");
|
|
ButtonType cancelButton = new ButtonType("Cancel");
|
|
|
|
alert.getButtonTypes().setAll(deleteButton, cancelButton);
|
|
|
|
alert.showAndWait().ifPresent(response -> {
|
|
if (response == deleteButton) {
|
|
// delete if the user confirms
|
|
deleteIngredient(ingredient);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void deleteIngredient(Ingredient ingredient) {
|
|
String url = "http://localhost:8080/api/ingredients/" + ingredient.getId();
|
|
|
|
restTemplate.delete(url);
|
|
showConfirmation("Deletion Successful", "The ingredient '" + ingredient.getName() + "' has been successfully deleted.");
|
|
|
|
// refresh
|
|
refreshIngredientList();
|
|
}
|
|
|
|
private void showError(String title, String message) {
|
|
Alert alert = new Alert(AlertType.ERROR);
|
|
alert.setTitle(title);
|
|
alert.setHeaderText(null);
|
|
alert.setContentText(message);
|
|
alert.showAndWait();
|
|
}
|
|
|
|
private void showConfirmation(String title, String message) {
|
|
Alert alert = new Alert(AlertType.INFORMATION);
|
|
alert.setTitle(title);
|
|
alert.setHeaderText(null);
|
|
alert.setContentText(message);
|
|
alert.showAndWait();
|
|
}
|
|
|
|
private void refreshIngredientList() {
|
|
// Refresh
|
|
ingredientListView.getItems().clear();
|
|
}
|
|
|
|
// Inner class for usage response
|
|
public static class IngredientUsageResponse {
|
|
private long ingredientId;
|
|
private long usedInRecipes;
|
|
|
|
public long getIngredientId() {
|
|
return ingredientId;
|
|
}
|
|
|
|
public void setIngredientId(long ingredientId) {
|
|
this.ingredientId = ingredientId;
|
|
}
|
|
|
|
public long getUsedInRecipes() {
|
|
return usedInRecipes;
|
|
}
|
|
|
|
public void setUsedInRecipes(long usedInRecipes) {
|
|
this.usedInRecipes = usedInRecipes;
|
|
}
|
|
}
|
|
}
|