Compare commits

...

2 Commits

Author SHA1 Message Date
Jonas Hinterdorfer
a424c04937 implemented deleteion without validataion 2025-05-12 12:56:29 +02:00
Jonas Hinterdorfer
02faca0aa6 implement questioncatalog Service 2025-05-12 12:18:56 +02:00
7 changed files with 204 additions and 18 deletions

View File

@ -1,27 +1,87 @@
package at.ionas999.questioncatalog.controller;
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.ServiceBase;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ViewController{
public Button editButton;
public Button deleteButton;
public TextField answerField;
public ListView questionListView;
public TextField questionField;
public class ViewController {
@FXML
private ComboBox selectBox;
@FXML
private Button editButton;
@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
private void initialize() throws SQLException {
List<Question> questions = QuestionService.GetQuestionsFromCatalog(1);
System.out.println(questions);
this.catalogs = FXCollections.observableList(QuestionCatalogService.GetCatalogsWithoutQuestions());
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

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

View File

@ -11,7 +11,8 @@ public class QuestionCatalog {
private ArrayList<Question> questions;
private String name;
public QuestionCatalog(String name, ArrayList<Question> questions) {
public QuestionCatalog(int id, String name, ArrayList<Question> questions) {
this.id = id;
this.name = name;
this.questions = questions;
}
@ -20,8 +21,20 @@ public class QuestionCatalog {
return FXCollections.observableList(questions);
}
public void setQuestions(ArrayList<Question> questions){
this.questions = questions;
}
public int getId(){
return this.id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return this.getName();
}
}

View File

@ -0,0 +1,75 @@
package at.ionas999.questioncatalog.services;
import at.ionas999.questioncatalog.model.Question;
import at.ionas999.questioncatalog.model.QuestionCatalog;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class QuestionCatalogService {
public static boolean AddQuestionCatalogToDb(QuestionCatalog catalog) throws SQLException {
try (Connection connection = ServiceBase.GetJDBCConnection()) {
String insertStmt = "insert into QUESTIONCATALOG (NAME) VALUES (?)";
try (PreparedStatement statement = connection.prepareStatement(insertStmt)) {
statement.setString(2, catalog.getName());
statement.execute();
}
}
int id = GetLastInsertedCatalogWithoutQuestions(catalog.getName()).getId();
for(Question q : catalog.getQuestions()) {
q.setQuestionCatalogId(id);
QuestionService.AddQuestionToDb(q);
}
return true;
}
public static QuestionCatalog GetLastInsertedCatalogWithoutQuestions(String name) throws SQLException {
String stmtString = "select id, name from QUESTIONCATALOG order by ID desc fetch first row only";
QuestionCatalog catalog;
try (Connection connection = ServiceBase.GetJDBCConnection()) {
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
ResultSet rs = statement.executeQuery();
List<Question> questions = new ArrayList<Question>();
rs.next();
catalog = new QuestionCatalog(
rs.getInt(1),
rs.getString(2),
null
);
}
}
return catalog;
}
public static ArrayList<QuestionCatalog> GetCatalogsWithoutQuestions() throws SQLException {
String stmtString = "select id, name from QUESTIONCATALOG";
try (Connection connection = ServiceBase.GetJDBCConnection()) {
try (PreparedStatement statement = connection.prepareStatement(stmtString)) {
ResultSet rs = statement.executeQuery();
ArrayList<QuestionCatalog> catalogs = new ArrayList<>();
while (rs.next()) {
QuestionCatalog catalog = new QuestionCatalog(
rs.getInt(1),
rs.getString(2),
null
);
catalogs.add(catalog);
}
return catalogs;
}
}
}
}

View File

@ -26,7 +26,7 @@ public class QuestionService {
return true;
}
public static List<Question> GetQuestionsFromCatalog(int catalogId) throws SQLException {
public static ArrayList<Question> GetQuestionsFromCatalog(int catalogId) throws SQLException {
String stmtString = "select id, questionCatalogId, question, answer from Question where questionCatalogId = ?";
try(Connection connection = ServiceBase.GetJDBCConnection()){
@ -34,7 +34,7 @@ public class QuestionService {
try (PreparedStatement statement = connection.prepareStatement(stmtString)){
statement.setInt(1, catalogId);
ResultSet rs = statement.executeQuery();
List<Question> questions = new ArrayList<Question>();
ArrayList<Question> questions = new ArrayList<Question>();
while (rs.next()){
Question question = new Question(
@ -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

@ -20,7 +20,7 @@ public abstract class ServiceBase {
create table QuestionCatalog
(
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name varchar(10) not null
name varchar(10) not null unique
)
""";
try (Statement stmt = connection.createStatement()) {

View File

@ -7,6 +7,15 @@
fx:controller="at.ionas999.questioncatalog.controller.ViewController"
prefHeight="400.0" prefWidth="600.0">
<top>
<HBox alignment="TOP_RIGHT" spacing="10.0">
<padding>
<Insets top="10.0" right="10.0" bottom="0.0" left="10.0"/>
</padding>
<ComboBox fx:id="selectBox" promptText="Select an Catalog" />
</HBox>
</top>
<left>
<ListView fx:id="questionListView" prefWidth="200.0" />
</left>
@ -19,8 +28,8 @@
<TextField fx:id="questionField" editable="false" />
<TextField fx:id="answerField" promptText="Answer" editable="false" />
<HBox spacing="10.0" alignment="BOTTOM_RIGHT">
<Button fx:id="deleteButton" text="Delete" />
<Button fx:id="editButton" text="Edit" />
<Button fx:id="deleteButton" onAction="#deleteQuestion" text="Delete" />
<Button fx:id="editButton" text="Edit" onAction="#editQuestion" />
</HBox>
</VBox>
</center>