#22: Update Ingredients with Medication
All checks were successful
Quality Check / Qualty Check (push) Successful in 11s
Quality Check / Qualty Check (pull_request) Successful in 12s

Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
Dominik Säume 2024-05-07 14:56:40 +02:00
parent daead5c79b
commit b04fa5a938
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C

View file

@ -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;
@ -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