implemented deleteion without validataion

This commit is contained in:
Jonas Hinterdorfer 2025-05-12 12:56:29 +02:00
parent 02faca0aa6
commit a424c04937
6 changed files with 105 additions and 17 deletions

View File

@ -1,27 +1,87 @@
package at.ionas999.questioncatalog.controller; package at.ionas999.questioncatalog.controller;
import at.ionas999.questioncatalog.model.Question; import at.ionas999.questioncatalog.model.Question;
import at.ionas999.questioncatalog.model.QuestionCatalog;
import at.ionas999.questioncatalog.services.QuestionCatalogService;
import at.ionas999.questioncatalog.services.QuestionService; import at.ionas999.questioncatalog.services.QuestionService;
import at.ionas999.questioncatalog.services.ServiceBase;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ViewController{ public class ViewController {
public Button editButton; @FXML
public Button deleteButton; private ComboBox selectBox;
public TextField answerField; @FXML
public ListView questionListView; private Button editButton;
public TextField questionField; @FXML
private Button deleteButton;
@FXML
private TextField answerField;
@FXML
private ListView questionListView;
@FXML
private TextField questionField;
private Question currentQuestion = null;
private ObservableList<QuestionCatalog> catalogs;
private ObservableList<Question> questions;
@FXML @FXML
private void initialize() throws SQLException { private void initialize() throws SQLException {
List<Question> questions = QuestionService.GetQuestionsFromCatalog(1); this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
System.out.println(questions); selectBox.setItems(catalogs);
addObserverToSelectBox();
addListenerToListView();
}
private void addListenerToListView() {
questionListView.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
currentQuestion = (Question) newValue;
if (newValue != null) {
Question q = (Question) newValue;
questionField.setText(q.getQuestion());
answerField.setText(q.getAnswer());
}
});
}
private void addObserverToSelectBox() {
selectBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null) {
return;
}
try {
setQuestionListView(((QuestionCatalog) newValue).getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
});
}
private void setQuestionListView(int catalogId) throws SQLException {
this.questions = FXCollections.observableList(QuestionService.GetQuestionsFromCatalog(catalogId));
questionListView.setItems(questions);
}
@FXML
private void deleteQuestion(ActionEvent actionEvent) {
questions.remove(currentQuestion);
QuestionService.DeleteQuestion(currentQuestion.getId());
this.questionListView.refresh();
this.currentQuestion = null;
}
@FXML
private void editQuestion(ActionEvent actionEvent) {
} }
} }

View File

@ -36,4 +36,13 @@ public class Question {
public void setQuestionCatalogId(int questionCatalogId) { public void setQuestionCatalogId(int questionCatalogId) {
this.questionCatalogId = questionCatalogId; this.questionCatalogId = questionCatalogId;
} }
public int getId()
{
return id;
}
@Override
public String toString() {
return question;
}
} }

View File

@ -33,4 +33,8 @@ public class QuestionCatalog {
return name; return name;
} }
@Override
public String toString() {
return this.getName();
}
} }

View File

@ -50,13 +50,12 @@ public class QuestionCatalogService {
return catalog; return catalog;
} }
public static ArrayList<QuestionCatalog> GetCatalogsWithoutQuestions(int catalogId) throws SQLException { public static ArrayList<QuestionCatalog> GetCatalogsWithoutQuestions() throws SQLException {
String stmtString = "select id, name from QUESTIONCATALOG where ID = ?"; String stmtString = "select id, name from QUESTIONCATALOG";
try (Connection connection = ServiceBase.GetJDBCConnection()) { try (Connection connection = ServiceBase.GetJDBCConnection()) {
try (PreparedStatement statement = connection.prepareStatement(stmtString)) { try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
statement.setInt(1, catalogId);
ResultSet rs = statement.executeQuery(); ResultSet rs = statement.executeQuery();
ArrayList<QuestionCatalog> catalogs = new ArrayList<>(); ArrayList<QuestionCatalog> catalogs = new ArrayList<>();

View File

@ -50,4 +50,20 @@ public class QuestionService {
} }
} }
} }
public static boolean DeleteQuestion(int id){
try(Connection connection = ServiceBase.GetJDBCConnection()){
String insertStmt = "delete from QUESTION where ID = ?";
try (PreparedStatement statement = connection.prepareStatement(insertStmt)){
statement.setInt(1, id);
statement.execute();
}
}
catch (SQLException _){
return false;
}
return true;
}
} }

View File

@ -28,8 +28,8 @@
<TextField fx:id="questionField" editable="false" /> <TextField fx:id="questionField" editable="false" />
<TextField fx:id="answerField" promptText="Answer" editable="false" /> <TextField fx:id="answerField" promptText="Answer" editable="false" />
<HBox spacing="10.0" alignment="BOTTOM_RIGHT"> <HBox spacing="10.0" alignment="BOTTOM_RIGHT">
<Button fx:id="deleteButton" text="Delete" /> <Button fx:id="deleteButton" onAction="#deleteQuestion" text="Delete" />
<Button fx:id="editButton" text="Edit" /> <Button fx:id="editButton" text="Edit" onAction="#editQuestion" />
</HBox> </HBox>
</VBox> </VBox>
</center> </center>