#22 story/medikamente-modul-grundimplementierung #34
1 changed files with 25 additions and 2 deletions
|
@ -3,7 +3,11 @@ package de.hitec.nhplus.medication.database;
|
||||||
import de.hitec.nhplus.datastorage.Dao;
|
import de.hitec.nhplus.datastorage.Dao;
|
||||||
import de.hitec.nhplus.medication.Ingredient;
|
import de.hitec.nhplus.medication.Ingredient;
|
||||||
import de.hitec.nhplus.medication.Medication;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -74,7 +78,7 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
int lastMedicationId = -1;
|
int lastMedicationId = -1;
|
||||||
while (result.next()) {
|
while (result.next()) {
|
||||||
currentMedicationId = result.getInt(1);
|
currentMedicationId = result.getInt(1);
|
||||||
if(currentMedicationId != lastMedicationId) {
|
if (currentMedicationId != lastMedicationId) {
|
||||||
Medication medication = new Medication(
|
Medication medication = new Medication(
|
||||||
result.getInt(1),
|
result.getInt(1),
|
||||||
result.getString(2),
|
result.getString(2),
|
||||||
|
@ -118,6 +122,25 @@ public class MedicationDao implements Dao<Medication> {
|
||||||
preparedStatement.setInt(5, medication.getCurrentStock());
|
preparedStatement.setInt(5, medication.getCurrentStock());
|
||||||
preparedStatement.setInt(6, medication.getId());
|
preparedStatement.setInt(6, medication.getId());
|
||||||
preparedStatement.executeUpdate();
|
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
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue