diff --git a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java index 3eaac72..4170582 100644 --- a/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java +++ b/src/main/java/de/hitec/nhplus/medication/database/MedicationDao.java @@ -3,7 +3,11 @@ package de.hitec.nhplus.medication.database; import de.hitec.nhplus.datastorage.Dao; import de.hitec.nhplus.medication.Ingredient; import de.hitec.nhplus.medication.Medication; -import java.sql.*; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -74,7 +78,7 @@ public class MedicationDao implements Dao { int lastMedicationId = -1; while (result.next()) { currentMedicationId = result.getInt(1); - if(currentMedicationId != lastMedicationId) { + if (currentMedicationId != lastMedicationId) { Medication medication = new Medication( result.getInt(1), result.getString(2), @@ -118,6 +122,25 @@ public class MedicationDao implements Dao { preparedStatement.setInt(5, medication.getCurrentStock()); preparedStatement.setInt(6, medication.getId()); preparedStatement.executeUpdate(); + + final String ingredientDeleteSQL = """ + DELETE FROM medication_ingredient WHERE id = ? + """; + PreparedStatement ingredientStatement = this.connection.prepareStatement(ingredientDeleteSQL); + ingredientStatement.setInt(1, medication.getId()); + ingredientStatement.executeUpdate(); + + final String ingredientCreateSQL = """ + INSERT INTO medication_ingredient + (id, name) + VALUES (?, ?); + """; + for (Ingredient ingredient : medication.getIngredients()) { + PreparedStatement statement = this.connection.prepareStatement(ingredientCreateSQL); + statement.setInt(1, medication.getId()); + statement.setString(2, ingredient.getName()); + statement.execute(); + } } @Override