Compare commits

..

2 commits

Author SHA1 Message Date
e881adcc04 Merge pull request 'Patch MedicationDao' (#39) from patch/medication-dao into story/medikamente-modul-medikamente-anlegen
All checks were successful
Quality Check / Linting Check (push) Successful in 12s
Quality Check / Javadoc Check (push) Successful in 21s
Reviewed-on: #39
Reviewed-by: SZUT-Ole <ole.kueck@hmmh.de>
2024-05-17 06:47:02 +00:00
942577a86a
NOTICKET: Fix MedicationDao
All checks were successful
Quality Check / Linting Check (push) Successful in 17s
Quality Check / Linting Check (pull_request) Successful in 21s
Quality Check / Javadoc Check (push) Successful in 36s
Quality Check / Javadoc Check (pull_request) Successful in 34s
2024-05-17 08:43:46 +02:00
2 changed files with 14 additions and 7 deletions

View file

@ -67,7 +67,6 @@ public class MedicationFixture implements Fixture<Medication> {
Ingredient warfarinnatrium = new Ingredient("Warfarinnatrium");
medications.add(new Medication(
1,
"Metformin",
"AstraZeneca",
List.of(
@ -81,7 +80,6 @@ public class MedicationFixture implements Fixture<Medication> {
100
));
medications.add(new Medication(
2,
"Lisinopril",
"Teva Pharmaceuticals",
List.of(
@ -95,7 +93,6 @@ public class MedicationFixture implements Fixture<Medication> {
150
));
medications.add(new Medication(
3,
"Simvastatin",
"Mylan",
List.of(
@ -109,7 +106,6 @@ public class MedicationFixture implements Fixture<Medication> {
80
));
medications.add(new Medication(
4,
"Enoxaparin",
"Sanofi",
List.of(
@ -122,7 +118,6 @@ public class MedicationFixture implements Fixture<Medication> {
120
));
medications.add(new Medication(
5,
"Levothyroxin",
"Sandoz",
List.of(
@ -136,7 +131,6 @@ public class MedicationFixture implements Fixture<Medication> {
90
));
medications.add(new Medication(
6,
"Warfarin",
"Apotex Inc.",
List.of(

View file

@ -29,6 +29,7 @@ public class MedicationDao implements Dao<Medication> {
@Override
public void create(Medication medication) throws SQLException {
connection.setAutoCommit(false); //Switch to Manual Commit, to do an SQL Transaction
final String medicationSQL = """
INSERT INTO medication
(name, manufacturer, possibleSideEffects, administrationMethod, currentStock)
@ -42,6 +43,15 @@ public class MedicationDao implements Dao<Medication> {
medicationStatement.setInt(5, medication.getCurrentStock());
medicationStatement.execute();
ResultSet generatedKeys = connection.createStatement().executeQuery("SELECT last_insert_rowid()");
connection.commit(); //Finish SQL Transaction
connection.setAutoCommit(true); //Switch back Mode
if (!generatedKeys.next()) {
return;
}
int newId = generatedKeys.getInt(1);
final String ingredientSQL = """
INSERT INTO medication_ingredient
(id, name)
@ -49,7 +59,7 @@ public class MedicationDao implements Dao<Medication> {
""";
for (Ingredient ingredient : medication.getIngredients()) {
PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientSQL);
ingredientStatement.setInt(1, medication.getId());
ingredientStatement.setInt(1, newId);
ingredientStatement.setString(2, ingredient.getName());
ingredientStatement.execute();
}
@ -104,6 +114,9 @@ public class MedicationDao implements Dao<Medication> {
lastMedicationId = currentMedicationId;
}
for (Medication medication : medications) {
if(!ingredientMap.containsKey(medication.getId())){
continue;
}
medication.setIngredients(ingredientMap.get(medication.getId()));
}