add: create and edit children
This commit is contained in:
		| @@ -0,0 +1,142 @@ | ||||
| package com.bib.essensbestellungsverwaltung; | ||||
|  | ||||
| import javafx.collections.FXCollections; | ||||
| import javafx.collections.ObservableList; | ||||
| import javafx.event.ActionEvent; | ||||
| import javafx.fxml.FXML; | ||||
| import javafx.scene.control.*; | ||||
| import org.controlsfx.control.CheckComboBox; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| public class ChildController { | ||||
|     public TextField firstName; | ||||
|     public ChoiceBox childChoiceBox; | ||||
|     public TextField lastName; | ||||
|     public Button kindHinzufügenButton; | ||||
|     public CheckComboBox allergienComboBox; | ||||
|     public Button kindLoeschenButton; | ||||
|  | ||||
|     private Child currentChild = null; | ||||
|  | ||||
|     private long selectedChildId; | ||||
|  | ||||
|     @FXML | ||||
|     public void initialize() { | ||||
|         kindLoeschenButton.setDisable(true); | ||||
|         updateChildChoiceBoxItems(); | ||||
|  | ||||
|         List<String> a = Database.getTable("allergy"); | ||||
|         ObservableList<String> allergies = FXCollections.observableArrayList(); | ||||
|         for (String allergie : a) { | ||||
|             allergies.add(allergie.split(":")[0] + ": " + allergie.split(":")[1]); | ||||
|         } | ||||
|         allergienComboBox.getItems().addAll(allergies); | ||||
|     } | ||||
|  | ||||
|     public void onSelectChild(ActionEvent mouseEvent) { | ||||
|         selectedChildId = Integer.parseInt(childChoiceBox.getValue().toString().split(":")[0]); | ||||
|         System.out.println("Selected Child: " + selectedChildId); | ||||
|  | ||||
|         clearInputs(); | ||||
|  | ||||
|         if(selectedChildId == 0) { | ||||
|             kindHinzufügenButton.setText("Kind Hinzufügen"); | ||||
|             kindLoeschenButton.setDisable(true); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // Get selected child and update fields | ||||
|         currentChild = AccountMgr.getChildById(selectedChildId); | ||||
|         firstName.setText(currentChild.getFirstname()); | ||||
|         lastName.setText(currentChild.getName()); | ||||
|         for (AllergySeverity a : currentChild.getAllergies()){ | ||||
|             allergienComboBox.getCheckModel().check((int) a.getAllergy().getId() - 1); | ||||
|         } | ||||
|         kindHinzufügenButton.setText("Kind updaten"); | ||||
|         kindLoeschenButton.setDisable(false); | ||||
|     } | ||||
|  | ||||
|     public void onKindHinzufügen(ActionEvent actionEvent) { | ||||
|         if(childChoiceBox.getValue() == null){ | ||||
|             Alert alert = new Alert(Alert.AlertType.ERROR); | ||||
|             alert.setTitle("Ungültige Eingabe"); | ||||
|             alert.setHeaderText("Sie müssen ein Kind auswählen"); | ||||
|             alert.showAndWait(); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         if(lastName.getText().isBlank() || firstName.getText().isBlank()){ | ||||
|             Alert alert = new Alert(Alert.AlertType.ERROR); | ||||
|             alert.setTitle("Ungültige Eingabe"); | ||||
|             alert.setHeaderText("Es wurden nicht alle Felder ausgefüllt"); | ||||
|             alert.showAndWait(); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         Address adress = AccountMgr.currentUser.getAddress(); | ||||
|         List<AllergySeverity> allergies = new ArrayList<>(); | ||||
|         allergienComboBox.getCheckModel().getCheckedItems().stream().forEach(a -> { | ||||
|             long id = Integer.parseInt(a.toString().split(":")[0]); | ||||
|             String name = a.toString().split(":")[1].trim(); | ||||
|             // TODO: Allergy Severity | ||||
|             allergies.add(new AllergySeverity(new Allergy(id, name, ""), 2, null)); | ||||
|         }); | ||||
|  | ||||
|         if(selectedChildId == 0){ | ||||
|             // TODO: Create child | ||||
|             long id = AccountMgr.createChild(new Child(lastName.getText(), firstName.getText(), adress, allergies)); | ||||
|             System.out.println("Child created: " + id); | ||||
|  | ||||
|             if(id <= 0){ | ||||
|                 Alert alert = new Alert(Alert.AlertType.ERROR); | ||||
|                 alert.setTitle("Es ist ein fehler beim erstellen des Kinds aufgetreten"); | ||||
|                 alert.showAndWait(); | ||||
|                 return; | ||||
|             } | ||||
|             AccountMgr.matchParentChild(String.valueOf(AccountMgr.currentUser.getId()), String.valueOf(id)); | ||||
|  | ||||
|             childChoiceBox.getItems().add(id + ": " + firstName.getText() + " " + lastName.getText()); | ||||
|             childChoiceBox.setValue(childChoiceBox.getItems().get(childChoiceBox.getItems().size() - 1)); | ||||
|             clearInputs(); | ||||
|             onSelectChild(null); | ||||
|  | ||||
|             Alert alert = new Alert(Alert.AlertType.CONFIRMATION); | ||||
|             alert.setTitle("Kind erstellt"); | ||||
|             alert.setHeaderText("Es wurde ein Kind erstellt"); | ||||
|             alert.showAndWait(); | ||||
|         }else { | ||||
|             long id = AccountMgr.updateChild(new Child(currentChild.getId(), lastName.getText(), firstName.getText(), adress, allergies)); | ||||
|             System.out.println("Updated Child: " + id); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void clearInputs(){ | ||||
|         firstName.setText(""); | ||||
|         lastName.setText(""); | ||||
|         allergienComboBox.getCheckModel().clearChecks(); | ||||
|     } | ||||
|  | ||||
|     public void updateChildChoiceBoxItems(){ | ||||
|         List<Child> childList = AccountMgr.getAllChildrenFromParentWithId(AccountMgr.currentUser.getId()); | ||||
|  | ||||
|         ObservableList<Object> childOptions = FXCollections.observableArrayList(childList.stream().map(c -> c.getId() + ": " + c.getFirstname() + " " + c.getName()).toList().toArray(new String[0])); | ||||
|         childOptions.add(0, "0: neues Kind erstellen"); | ||||
|         childChoiceBox.setItems(childOptions); | ||||
|     } | ||||
|  | ||||
|     public void onKindLoeschen(ActionEvent actionEvent) { | ||||
|         AccountMgr.deleteChildWithId(currentChild.getId()); | ||||
|         System.out.println("Deleted Child: " + currentChild.getId()); | ||||
|  | ||||
|         childChoiceBox.getItems().remove(childChoiceBox.getItems().stream().filter(v -> v.equals(currentChild.getId() + ": " + currentChild.getFirstname() + " " + currentChild.getName())).findFirst().get()); | ||||
|         childChoiceBox.setValue(childChoiceBox.getItems().get(1)); | ||||
|  | ||||
|         onSelectChild(null); | ||||
|  | ||||
|         Alert alert = new Alert(Alert.AlertType.CONFIRMATION); | ||||
|         alert.setTitle("Kind wurde gelöscht"); | ||||
|         alert.showAndWait(); | ||||
|     } | ||||
| } | ||||
| @@ -12,7 +12,6 @@ import java.io.IOException; | ||||
| public class WorkerMenuController { | ||||
|     @FXML | ||||
|     public BorderPane contentView; | ||||
|  | ||||
|     @FXML | ||||
|     Button tagesbestellungButton; | ||||
|     @FXML | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Johannes Kantz
					Johannes Kantz