implemented the question edit

This commit is contained in:
Jonas Hinterdorfer 2025-05-12 14:46:15 +02:00
parent a424c04937
commit af87631ff1
2 changed files with 71 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import javafx.scene.control.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class ViewController {
@FXML
@ -32,6 +33,7 @@ public class ViewController {
private Question currentQuestion = null;
private ObservableList<QuestionCatalog> catalogs;
private ObservableList<Question> questions;
private boolean isInEditState = false;
@FXML
private void initialize() throws SQLException {
@ -43,8 +45,9 @@ public class ViewController {
private void addListenerToListView() {
questionListView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
currentQuestion = (Question) newValue;
if (newValue != null) {
currentQuestion = (Question) newValue;
System.out.println("changed " + currentQuestion);
Question q = (Question) newValue;
questionField.setText(q.getQuestion());
answerField.setText(q.getAnswer());
@ -74,14 +77,60 @@ public class ViewController {
@FXML
private void deleteQuestion(ActionEvent actionEvent) {
questions.remove(currentQuestion);
if(!showConfirmationButton("Do you want to proceed with deletion of the question: \n" + currentQuestion.getQuestion()))
{
return;
}
QuestionService.DeleteQuestion(currentQuestion.getId());
questions.remove(currentQuestion);
this.questionListView.refresh();
this.currentQuestion = null;
}
@FXML
private void editQuestion(ActionEvent actionEvent) {
private void editQuestion(ActionEvent actionEvent) throws SQLException {
if(this.isInEditState){
this.currentQuestion.setQuestion(this.questionField.getText());
this.currentQuestion.setAnswer(this.answerField.getText());
QuestionService.UpdateQuestion(this.currentQuestion);
this.editButton.setText("Edit");
questionListView.refresh();
this.isInEditState = false;
this.setEditableAndSetDisabled(false);
}
else {
this.setEditableAndSetDisabled(true);
this.editButton.setText("Save");
this.isInEditState = true;
}
}
private void setEditableAndSetDisabled(boolean value)
{
questionField.setEditable(value);
answerField.setEditable(value);
this.questionListView.setDisable(value);
this.deleteButton.setDisable(value);
}
private boolean showConfirmationButton(String content) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Are you sure?");
alert.setContentText(content);
ButtonType yesButton = new ButtonType("Yes");
ButtonType cancelButton = new ButtonType("Cancel");
alert.getButtonTypes().setAll(yesButton, cancelButton);
Optional<ButtonType> result = alert.showAndWait();
return result.filter(buttonType -> buttonType == yesButton).isPresent();
}
}

View File

@ -66,4 +66,23 @@ public class QuestionService {
return true;
}
public static void UpdateQuestion(Question question) throws SQLException {
try(Connection connection = ServiceBase.GetJDBCConnection()){
String insertStmt = "update QUESTION set " +
"QUESTION = ?," +
"ANSWER=?," +
"QUESTIONCATALOGID=?" +
"where ID = ?";
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
statement.setInt(3, question.getQuestionCatalogId());
statement.setInt(4, question.getId());
statement.setString(1, question.getQuestion());
statement.setString(2, question.getAnswer());
statement.execute();
}
}
}
}