implement questioncatalog Service

This commit is contained in:
Jonas Hinterdorfer 2025-05-12 12:18:56 +02:00
parent cb18e47557
commit 02faca0aa6
6 changed files with 102 additions and 4 deletions

View File

@ -32,4 +32,8 @@ public class Question {
public void setAnswer(String answer) { public void setAnswer(String answer) {
this.answer = answer; this.answer = answer;
} }
public void setQuestionCatalogId(int questionCatalogId) {
this.questionCatalogId = questionCatalogId;
}
} }

View File

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

View File

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

View File

@ -20,7 +20,7 @@ public abstract class ServiceBase {
create table QuestionCatalog create table QuestionCatalog
( (
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 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()) { try (Statement stmt = connection.createStatement()) {

View File

@ -7,6 +7,15 @@
fx:controller="at.ionas999.questioncatalog.controller.ViewController" fx:controller="at.ionas999.questioncatalog.controller.ViewController"
prefHeight="400.0" prefWidth="600.0"> 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> <left>
<ListView fx:id="questionListView" prefWidth="200.0" /> <ListView fx:id="questionListView" prefWidth="200.0" />
</left> </left>