#22 story/medikamente-modul-grundimplementierung #34

Merged
SZUT-Dominik merged 4 commits from story/medikamente-modul-grundimplementierung into main 2024-05-07 12:58:49 +00:00
Showing only changes of commit b04fa5a938 - Show all commits

View file

@ -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<Medication> {
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<Medication> {
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