diff --git a/opp/core/src/main/java/Address.java b/opp/core/src/main/java/Address.java index a6f7e39aff05cbf53c70460bfd93ced9920f269c..4ba3cb75afdbabbd1d80819202b43f70fc6addf3 100644 --- a/opp/core/src/main/java/Address.java +++ b/opp/core/src/main/java/Address.java @@ -1,9 +1,7 @@ -import com.fasterxml.jackson.annotation.JsonProperty; - public record Address( - @JsonProperty String street, - @JsonProperty String house, - @JsonProperty String postalCode, - @JsonProperty String city + String street, + String house, + String postalCode, + String city ) { } diff --git a/opp/core/src/main/java/Id.java b/opp/core/src/main/java/Id.java index f2f2920f0ad5428c74386f29292602a746be7ed0..8182ef89ddb9308eb01d147dd81f9cf418834288 100644 --- a/opp/core/src/main/java/Id.java +++ b/opp/core/src/main/java/Id.java @@ -1,38 +1,7 @@ -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; - -import java.io.IOException; - -@JsonSerialize(using = Id.Serializer.class) public record Id<T>(String value) { - - @Override public String toString(){ return value; } - - - public static class Serializer<T> extends StdSerializer<Id<T>> - { - - public Serializer(){ - this(null); - } - - public Serializer(Class<Id<T>> cl){ - super(cl); - } - - @Override - public void serialize( - Id<T> id, - JsonGenerator json, - SerializerProvider provider - ) - throws IOException { - json.writeString(id.value()); - } - + @Override + public String toString(){ + return value; } - } diff --git a/opp/core/src/main/java/OPStaffServiceImpl.java b/opp/core/src/main/java/OPStaffServiceImpl.java index 2c62064c10424a9a56b10c28c1ea2aaca7dfdc15..87065af74edfa2bb6becf5ac36407675d29ccc03 100644 --- a/opp/core/src/main/java/OPStaffServiceImpl.java +++ b/opp/core/src/main/java/OPStaffServiceImpl.java @@ -13,13 +13,13 @@ public class OPStaffServiceImpl implements OPStaffService{ @Override public OPStaff process(OPStaff.Command cmd) throws Exception { return switch (cmd){ - case OPStaff.CreateOPStaff cr -> createOPStaffimpl(cr); - case OPStaff.UpdateOPStaff up -> updateOPStaffimpl(up); - case OPStaff.DeleteOPStaff del -> deleteOPStaffimpl(del); + case OPStaff.CreateOPStaff cr -> create(cr); + case OPStaff.UpdateOPStaff up -> update(up); + case OPStaff.DeleteOPStaff del -> delete(del); }; } - public OPStaff createOPStaffimpl(OPStaff.CreateOPStaff cr) throws Exception { + public OPStaff create(OPStaff.CreateOPStaff cr) throws Exception { OPStaff opStaff = new OPStaff( repo.opStaffId(), @@ -27,12 +27,12 @@ public class OPStaffServiceImpl implements OPStaffService{ cr.specialty(), Instant.now()); - repo.saveOPStaff(opStaff); + repo.save(opStaff); return opStaff; } - public OPStaff updateOPStaffimpl(OPStaff.UpdateOPStaff up) throws Exception{ + public OPStaff update(OPStaff.UpdateOPStaff up) throws Exception{ OPStaff currentOPStaff = repo.findOPStaff(up.id()).get(); OPStaff opStaff = @@ -42,18 +42,14 @@ public class OPStaffServiceImpl implements OPStaffService{ up.specialty().orElse((currentOPStaff.specialty())), Instant.now() ); - repo.saveOPStaff(opStaff); + repo.save(opStaff); return opStaff; } - public OPStaff deleteOPStaffimpl(OPStaff.DeleteOPStaff del) throws Exception{ + public OPStaff delete(OPStaff.DeleteOPStaff del) throws Exception{ - OPStaff opStaff = repo.findOPStaff(del.id()).get(); - - repo.deleteOPStaff(del.id()); - - return opStaff; + return repo.deleteOPStaff(del.id()); } diff --git a/opp/core/src/main/java/ObjectModification.java b/opp/core/src/main/java/ObjectModification.java deleted file mode 100644 index 98bf4a62780aebae8899d90d699089643615d756..0000000000000000000000000000000000000000 --- a/opp/core/src/main/java/ObjectModification.java +++ /dev/null @@ -1,5 +0,0 @@ -public enum ObjectModification { - CREATE, - UPDATE, - DELETE -} diff --git a/opp/core/src/main/java/Operation.java b/opp/core/src/main/java/Operation.java index 5a16e44c7833b28fc7bd7f6689cb542d3da96cc0..15ec5bdbc003921bd21a1c76447a56a5a1268080 100644 --- a/opp/core/src/main/java/Operation.java +++ b/opp/core/src/main/java/Operation.java @@ -15,7 +15,7 @@ public record Operation( LocalDate date, LocalTime startTime, LocalTime endTime, - + Id<Patient> patientId, Id<OperationTeam> operationTeamId, Instant lastUpdate ) { @@ -48,7 +48,7 @@ public record Operation( LocalDate date, LocalTime startTime, LocalTime endTime, - + Id<Patient> patientId, Id<OperationTeam> operationTeamId ) implements Command {} @@ -57,8 +57,8 @@ public record Operation( Optional<LocalDate> date, Optional<LocalTime> startTime, Optional<LocalTime> endTime, - - Id<OperationTeam> operationTeamId + Optional<Id<Patient>> patientId, + Optional<Id<OperationTeam>> operationTeamId ) implements Command {} public static record Delete( diff --git a/opp/core/src/main/java/OperationServiceImpl.java b/opp/core/src/main/java/OperationServiceImpl.java index d89c0887724d34e03d460dd7717211007d59c82f..20ea47df336adbf16b1e74e03eb7e41df76508e3 100644 --- a/opp/core/src/main/java/OperationServiceImpl.java +++ b/opp/core/src/main/java/OperationServiceImpl.java @@ -40,6 +40,7 @@ public class OperationServiceImpl implements OperationService { cr.date(), cr.startTime(), cr.endTime(), + cr.patientId(), cr.operationTeamId(), Instant.now()); @@ -58,7 +59,8 @@ public class OperationServiceImpl implements OperationService { up.date().orElse(currentOperation.date()), up.startTime().orElse(currentOperation.startTime()), up.endTime().orElse(currentOperation.endTime()), - up.operationTeamId(), //TODO + up.patientId().orElse(currentOperation.patientId()), + up.operationTeamId().orElse(currentOperation.operationTeamId()), Instant.now()); repo.save(operation); @@ -68,10 +70,6 @@ public class OperationServiceImpl implements OperationService { public Operation delete(Operation.Delete del) throws Exception { - Operation operation = repo.findOperation(del.id()).get(); - - repo.deleteOperation(del.id()); - - return operation; + return repo.deleteOperation(del.id()); } } diff --git a/opp/core/src/main/java/OperationTeam.java b/opp/core/src/main/java/OperationTeam.java index dcc79708a20e363e03f11638423c26998c8cdd83..a42f5352f46cb9162750793edfa0c2b72870e098 100644 --- a/opp/core/src/main/java/OperationTeam.java +++ b/opp/core/src/main/java/OperationTeam.java @@ -1,8 +1,7 @@ import java.time.Instant; -import java.util.List; public record OperationTeam( - Id<OperationTeam> operationTeamId, + Id<OperationTeam> id, String teamName, Instant lastUpdate ) { diff --git a/opp/core/src/main/java/OperationTeamImpl.java b/opp/core/src/main/java/OperationTeamImpl.java index 94d5e42338f803b0cac2ac23c57d5183d5f7eb4b..061212e28cabd4eac58c4c2a6df202aa16f34717 100644 --- a/opp/core/src/main/java/OperationTeamImpl.java +++ b/opp/core/src/main/java/OperationTeamImpl.java @@ -15,24 +15,22 @@ public class OperationTeamImpl implements OperationTeamService{ @Override public OperationTeam process(OperationTeam.Command cmd) throws Exception { return switch(cmd){ - case OperationTeam.CreateTeam cr -> createOperationTeamimpl(cr); - case OperationTeam.DeleteTeam del -> deleteOperationTeamimpl(del); + case OperationTeam.CreateTeam cr -> create(cr); + case OperationTeam.DeleteTeam del -> delete(del); }; } @Override public Optional<OperationTeam> getOperationTeam(Id<OperationTeam> operationTeamId) throws SQLException { - return repo.getOperationTeamImp(operationTeamId); //repo.findOperationTeam(operationTeamId); + return repo.findOperationTeam(operationTeamId); } - private OperationTeam deleteOperationTeamimpl(OperationTeam.DeleteTeam del) throws SQLException { + private OperationTeam delete(OperationTeam.DeleteTeam del) throws SQLException { - OperationTeam operationTeam = repo.deleteOperationTeam(del.operationTeamId()); - - return operationTeam; + return repo.deleteOperationTeam(del.operationTeamId()); } - private OperationTeam createOperationTeamimpl(OperationTeam.CreateTeam cr) { + private OperationTeam create(OperationTeam.CreateTeam cr) { OperationTeam operationTeam = new OperationTeam( repo.operationTeamId(), @@ -45,7 +43,7 @@ public class OperationTeamImpl implements OperationTeamService{ @Override public List<OperationTeam> getOperationTeams() throws SQLException { - return repo.getOperationTeamsImp(); + return repo.findOperationTeams(); } diff --git a/opp/core/src/main/java/Patient.java b/opp/core/src/main/java/Patient.java index 71e747b98d5cec0a6a708a2a9a97440a6d44c561..6f1bce5076019427bec4b19d5879d3023b19b0ea 100644 --- a/opp/core/src/main/java/Patient.java +++ b/opp/core/src/main/java/Patient.java @@ -1,15 +1,6 @@ -import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.annotation.JsonProperty; - import java.time.LocalDate; public record Patient ( - Id<Patient> id, - Gender gender, - String givenName, - String familyName, - LocalDate birthDate, - Address address, - String healthInsurance + Id<Patient> id ) { } diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 7baa7b3cccbc23bd0b8989666dcd1bfab902c641..43f64324097142b8c69b1f2c58a9b2c46a82cc03 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -45,7 +45,7 @@ public interface Repository { * Deletes the Operation with the matching id. * @param id the id of the Operation to delete */ - void deleteOperation(Id<Operation> id) throws SQLException; + Operation deleteOperation(Id<Operation> id) throws SQLException; /** * Returns a new generated ID in the opstaff-SQLTable @@ -56,10 +56,10 @@ public interface Repository { /** * Saves the OPStaff into the database. - * @param + * @param opStaff the OPStaff to save into the database * @throws Exception while saving */ - void saveOPStaff(OPStaff opStaff) throws Exception; + void save(OPStaff opStaff) throws Exception; /** * Reads an OPStaff from the OPStaff-SQLTable with the given ID. @@ -73,26 +73,28 @@ public interface Repository { List<OPStaff> findOPStaffs(); - void deleteOPStaff(Id<OPStaff> id) throws SQLException; + OPStaff deleteOPStaff(Id<OPStaff> id) throws SQLException; Id<OperationTeam> operationTeamId(); - void saveOperationTeam(OperationTeam operationTeam) throws Exception; + void save(OperationTeam operationTeam) throws Exception; + + Optional<OperationTeam> findOperationTeam(Id<OperationTeam> id); + + List<OperationTeam> findOperationTeams() throws SQLException; OperationTeam deleteOperationTeam(Id<OperationTeam> id) throws SQLException; - Optional<OperationTeam> getOperationTeamImp(Id<OperationTeam> id) throws SQLException; - List<OperationTeam> getOperationTeamsImp() throws SQLException; Id<TeamMember> teamMemberId(); - void saveTeamMember(TeamMember teamMember) throws Exception; + void saveTeamMember(TeamMember TeamMember) throws Exception; List<OPStaff> getOperationTeamOPStaffImpl(Id<OperationTeam> operationTeamId); } diff --git a/opp/core/src/main/java/TeamMember.java b/opp/core/src/main/java/TeamMember.java index 7055ac5c2caf3c0e0ee41051dc2c02749a67b497..6d6064fc4a0c116cf0b79c7181706d76976744c7 100644 --- a/opp/core/src/main/java/TeamMember.java +++ b/opp/core/src/main/java/TeamMember.java @@ -1,6 +1,6 @@ import java.time.Instant; -public record TeamMember ( +public record TeamMember( Id<TeamMember> teamMemberId, diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index 9124a798d6b7995e71e717850c1caebcfdc07616..3f1e9b2525caef4b587f25243e47fd61e63c03a7 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -43,16 +43,76 @@ class JDBCRepository implements Repository } } + //region CREATE TABLES + + /** + * The structure of the operations table. + */ + private static final String CREATE_OPERATION_TABLE = """ + CREATE TABLE IF NOT EXISTS operations( + id VARCHAR(50) PRIMARY KEY, + date DATE NOT NULL, + startTime TIME NOT NULL, + endTime TIME NOT NULL, + patientId VARCHAR(50), + operationTeamId VARCHAR(50), + lastUpdate TIMESTAMP NOT NULL, + FOREIGN KEY (operationTeamId) REFERENCES operationTeams(id) + ); + """; + + /** + * The structure of the operationTeams table. + */ + private static final String CREATE_OPERATION_TEAM_TABLE = """ + CREATE TABLE IF NOT EXISTS operationTeams( + id VARCHAR(50) PRIMARY KEY, + teamName VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL + ); + """; + + /** + * The structure of the opStaffs table. + */ + private static final String CREATE_OP_STAFF_TABLE = """ + CREATE TABLE IF NOT EXISTS opStaffs( + id VARCHAR(50) PRIMARY KEY, + role VARCHAR(50) NOT NULL, + specialty VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL + ); + """; + + /** + * The structure of the opStaffsInOperationTeams table. + */ + private static final String CREATE_OP_STAFF_IN_OPERATION_TEAM_TABLE = """ + CREATE TABLE IF NOT EXISTS opStaffsInOperationTeams( + id VARCHAR(50) PRIMARY KEY, + operationTeamId VARCHAR(50), + opStaffId VARCHAR(50), + lastUpdate TIMESTAMP NOT NULL, + FOREIGN KEY (operationTeamId) REFERENCES operationTeams(id), + FOREIGN KEY (opStaffId) REFERENCES opStaffs(id) + ); + """; + + //endregion + + + //region SQL OPERATIONS + /** * Sets up the database tables. */ void setup(){ try (var stmt = conn.createStatement()){ + stmt.execute(CREATE_OPERATION_TEAM_TABLE); + stmt.execute(CREATE_OP_STAFF_TABLE); stmt.execute(CREATE_OPERATION_TABLE); - stmt.execute(CREATE_OPSTAFF_TABLE); - stmt.execute(CREATE_OPERATIONTEAM_TABLE); - stmt.execute(CREATE_TEAMMEMBER_TABLE); + stmt.execute(CREATE_OP_STAFF_IN_OPERATION_TEAM_TABLE); } catch (SQLException e){ throw new RuntimeException(e); @@ -84,158 +144,8 @@ class JDBCRepository implements Repository case Double n -> Double.toString(n); default -> quoted(obj.toString()); }; - - } - - /* - private static final String CREATE_PATIENT_TABLE = """ - CREATE TABLE IF NOT EXISTS patients( - id VARCHAR(50) PRIMARY KEY, - gender VARCHAR(10) NOT NULL, - givenName VARCHAR(100) NOT NULL, - familyName VARCHAR(100) NOT NULL, - birthDate DATE NOT NULL, - street VARCHAR(50) NOT NULL, - house VARCHAR(50) NOT NULL, - postalCode VARCHAR(50) NOT NULL, - city VARCHAR(50) NOT NULL, - healthInsurance VARCHAR(40) NOT NULL - ); - """; - - private static Patient readPatientFromRow(ResultSet rs) throws SQLException { - return new Patient( - new Id<>(rs.getString("id")), - Gender.valueOf(rs.getString("gender")), - rs.getString("givenName"), - rs.getString("familyName"), - rs.getDate("birthDate").toLocalDate(), - new Address( - rs.getString("street"), - rs.getString("house"), - rs.getString("postalCode"), - rs.getString("city") - ), - rs.getString("healthInsurance") - ); - } - - - private static String insertSQL(Patient patient){ - return - "INSERT INTO patients(" + - "id,gender,givenName,familyName,birthDate," + - "street,house,postalCode,city,healthInsurance" + - ") VALUES (" + - sqlValue(patient.id().value()) + "," + - sqlValue(patient.gender()) + "," + - sqlValue(patient.givenName()) + "," + - sqlValue(patient.familyName()) + "," + - sqlValue(patient.birthDate()) + "," + - sqlValue(patient.address().street()) + "," + - sqlValue(patient.address().house()) + "," + - sqlValue(patient.address().postalCode()) + "," + - sqlValue(patient.address().city()) + "," + - sqlValue(patient.healthInsurance()) + - ");"; - } - - private static String updateSQL(Patient patient){ - return - "UPDATE patients SET " + - "gender = " + sqlValue(patient.gender()) + "," + - "givenName = " + sqlValue(patient.givenName()) + "," + - "familyName = " + sqlValue(patient.familyName()) + "," + - "birthDate = " + sqlValue(patient.birthDate()) + "," + - "street = " + sqlValue(patient.address().street()) + "," + - "house = " + sqlValue(patient.address().house()) + "," + - "postalCode = " + sqlValue(patient.address().postalCode()) + "," + - "city = " + sqlValue(patient.address().city()) + "," + - "healthInsurance = " + sqlValue(patient.healthInsurance()) + " " + - "WHERE id = " + sqlValue(patient.id().value()) + ";"; } - @Override - public Id<Patient> patientId(){ - - var id = new Id<Patient>(randomUUID().toString()); - - return findPatient(id).isEmpty() ? id : patientId(); - } - - @Override - public void createPatient(Patient patient) throws SQLException { - - try ( - var stmt = conn.createStatement() - ){ - var sql = - findPatient(patient.id()).isPresent() ? - updateSQL(patient) : - insertSQL(patient); - - stmt.executeUpdate(sql); - - } catch (SQLException e){ - throw new RuntimeException(e); - } - - } - - @Override - public Optional<Patient> findPatient(Id<Patient> id){ - try ( - var result = - conn.createStatement() - .executeQuery("SELECT * FROM patients WHERE id = " + sqlValue(id.value()) + ";") - ){ - return - result.next() ? - Optional.of(readPatientFromRow(result)) : - Optional.empty(); - - } catch (SQLException e){ - throw new RuntimeException(e); - } - } - - @Override - public Optional<Patient> deletePatient(Id<Patient> id) throws SQLException { - - var patient = findPatient(id); - - patient.ifPresent( - p -> { - try { - conn.createStatement() - .executeUpdate("DELETE FROM patients WHERE id = " + quoted(id.value()) + ";"); - } catch (SQLException e){ - throw new RuntimeException(e); - } - } - ); - - return patient; - } - */ - - // Hier dann die restlichen create/edit Methoden rein - - /** - * The structure of the operations table. - */ - private static final String CREATE_OPERATION_TABLE = """ - CREATE TABLE IF NOT EXISTS operations( - id VARCHAR(50) PRIMARY KEY, - date DATE NOT NULL, - startTime TIME NOT NULL, - endTime TIME NOT NULL, - operationteamid VARCHAR(50), - lastUpdate TIMESTAMP NOT NULL, - FOREIGN KEY (operationteamid) REFERENCES operationteam(id) - ); - """; - /** * Creates the insert statement of the given operation for SQL. * @param operation the operation to turn into an insert SQL statement @@ -261,16 +171,144 @@ class JDBCRepository implements Repository * @return the SQL statement */ private static String updateSQL(Operation operation){ - return + return "UPDATE operations SET " + "date = " + sqlValue(operation.date()) + "," + "startTime = " + sqlValue(operation.startTime()) + "," + "endTime = " + sqlValue(operation.endTime()) + "," + - "operationteamid = " + sqlValue(operation.operationTeamId()) + "," + + "operationTeamId = " + sqlValue(operation.operationTeamId()) + "," + "lastUpdate = " + sqlValue(operation.lastUpdate()) + " " + "WHERE id = " + sqlValue(operation.id().value()) + ";"; } + /** + * Creates the insert statement of the given OPStaff for SQL. + * @param opStaff the OPStaff to turn into an insert SQL statement + * @return the SQL statement + */ + private static String insertSQL(OPStaff opStaff){ + return + "INSERT INTO opStaffs(" + + "id,role,specialty,lastUpdate" + + ") VALUES (" + + sqlValue(opStaff.id().value()) + "," + + sqlValue(opStaff.role()) + "," + + sqlValue(opStaff.specialty()) + "," + + sqlValue(opStaff.lastUpdate()) + + ");"; + } + + /** + * Creates the update statement of the given OPStaff for SQL. + * @param opStaff the opStaff to turn into an update SQL statement + * @return the SQL statement + */ + private static String updateSQL(OPStaff opStaff){ + return + "UPDATE opStaffs SET " + + "role = " + sqlValue(opStaff.role()) + "," + + "specialty = " + sqlValue(opStaff.specialty()) + "," + + "lastUpdate = " + sqlValue(opStaff.lastUpdate()) + " " + + "WHERE id = " + sqlValue(opStaff.id().value()) + ";"; + } + + /** + * Creates the insert statement of the given operationTeam for SQL. + * @param operationTeam the operationTeam to turn into an insert SQL statement + * @return the SQL statement + */ + private static String insertSQL(OperationTeam operationTeam){ + return + "INSERT INTO operationTeams(" + + "id,teamName,lastUpdate" + + ") VALUES (" + + sqlValue(operationTeam.id().value()) + "," + + sqlValue(operationTeam.teamName()) + "," + + sqlValue(operationTeam.lastUpdate()) + + ");"; + } + + /** + * Creates the update statement of the given operationTeam for SQL. + * @param operationTeam the operationTeam to turn into an update SQL statement + * @return the SQL statement + */ + private static String updateSQL(OperationTeam operationTeam){ + return + "UPDATE operationTeams SET " + + "teamName = " + sqlValue(operationTeam.teamName()) + "," + + "lastUpdate = " + sqlValue(operationTeam.lastUpdate()) + " " + + "WHERE id = " + sqlValue(operationTeam.id().value()) + ";"; + } + + /** + * Creates the insert statement of the given teamMember for SQL. + * @param teamMember the operationTeam to turn into an insert SQL statement + * @return the SQL statement + */ + private static String insertSQL(TeamMember teamMember){ + return + "INSERT INTO teamMember(" + + "id,operationTeamId,opStaffId,lastUpdate" + + ") VALUES (" + + sqlValue(teamMember.teamMemberId().value()) + "," + + sqlValue(teamMember.operationTeamId()) + "," + + sqlValue(teamMember.opStaffId()) + "," + + sqlValue(teamMember.lastUpdate()) + + ");"; + } + + /** + * Creates an Operation object with the given result of a read SQL-Row. + * @param rs the result of a read SQL-Row + * @return the created operation object + * @throws SQLException Error while reading the SQL-Row + */ + private static Operation readOperationFromRow(ResultSet rs) throws SQLException { + + return new Operation( + new Id<>(rs.getString("id")), + rs.getDate("date").toLocalDate(), + rs.getTime("startTime").toLocalTime(), + rs.getTime("endTime").toLocalTime(), + new Id<>(rs.getString("patientId")), + new Id<>(rs.getString("operationTeamId")), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + + /** + * Creates an Operation object with the given result of a read SQL-Row. + * @param rs the result of a read SQL-Row + * @return the created operation object + * @throws SQLException Error while reading the SQL-Row + */ + + private static OPStaff readOPStaffFromRow(ResultSet rs) throws SQLException { + + return new OPStaff( + new Id<>(rs.getString("id")), + Role.valueOf(rs.getString("role")), + Specialty.valueOf(rs.getString("specialty")), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + + + private static OperationTeam readOperationTeamFromRow(ResultSet rs) throws SQLException{ + + return new OperationTeam( + new Id<>(rs.getString("id")), + rs.getString("teamname"), + rs.getTimestamp("lastupdate").toInstant() + ); + } + + //endregion + + + //region REPOSITORY OPERATIONS + /** * Returns a new generated ID in the operations-SQLTable * If the generated ID is already used, generate another one. @@ -285,27 +323,25 @@ class JDBCRepository implements Repository } /** - * Connects to the database and checks if the ID of the given Operation is already used. + * Connects to the database and checks if the ID of the given operation is already used. * If yes, then update existing entry, else insert new entry. * @param operation the Operation to save into the database */ @Override public void save(Operation operation) { + try ( + var stmt = conn.createStatement() + ){ + var sql = + findOperation(operation.id()).isPresent() ? + updateSQL(operation) : + insertSQL(operation); - try ( - var stmt = conn.createStatement() - ){ - var sql = - findOperation(operation.id()).isPresent() ? - updateSQL(operation) : - insertSQL(operation); - - stmt.executeUpdate(sql); - - } catch (SQLException e){ - throw new RuntimeException(e); - } + stmt.executeUpdate(sql); + } catch (SQLException e){ + throw new RuntimeException(e); + } } /** @@ -316,19 +352,19 @@ class JDBCRepository implements Repository */ @Override public Optional<Operation> findOperation(Id<Operation> id){ - try ( - var result = - conn.createStatement() - .executeQuery("SELECT * FROM operations WHERE id = " + sqlValue(id.value()) + ";") - ){ - return - result.next() ? - Optional.of(readOperationFromRow(result)) : - Optional.empty(); + try ( + var result = + conn.createStatement() + .executeQuery("SELECT * FROM operations WHERE id = " + sqlValue(id.value()) + ";") + ){ + return + result.next() ? + Optional.of(readOperationFromRow(result)) : + Optional.empty(); - } catch (SQLException e){ - throw new RuntimeException(e); - } + } catch (SQLException e){ + throw new RuntimeException(e); + } } /** @@ -341,80 +377,56 @@ class JDBCRepository implements Repository @Override public List<Operation> findOperations(Operation.Filter filter) { - var sql = ""; - - if(filter.equals(Operation.Filter.NONE)) { - sql = "SELECT * FROM operations;"; + var sql = ""; - } else { - sql = "SELECT * FROM operations WHERE date = " + + if(filter.equals(Operation.Filter.NONE)) { + sql = "SELECT * FROM operations;"; + } else { + sql = "SELECT * FROM operations WHERE date = " + sqlValue(filter.date()). - replace("Optional", ""). - replace("[", ""). - replace("]", "") + ";"; - } - - try ( - var resultSet = - conn.createStatement().executeQuery(sql) - ){ - - var operations = new ArrayList<Operation>(); - - while(resultSet.next()){ - operations.add(readOperationFromRow(resultSet)); + replace("Optional", ""). + replace("[", ""). + replace("]", "") + ";"; + } + try ( + var resultSet = + conn.createStatement().executeQuery(sql) + ){ + var operations = new ArrayList<Operation>(); + + while(resultSet.next()){ + operations.add(readOperationFromRow(resultSet)); + } + return operations; + + } catch (SQLException e){ + throw new RuntimeException(e); } - - return operations; - - } catch (SQLException e){ - throw new RuntimeException(e); - } } /** - * Creates an Operation object with the given result of a read SQL-Row. - * @param rs the result of a read SQL-Row - * @return the created operation object - * @throws SQLException Error while reading the SQL-Row + * Connects to the database and deletes the operation with the given id. + * @param id the id of the Operation to delete */ - private static Operation readOperationFromRow(ResultSet rs) throws SQLException { - - return new Operation( - new Id<>(rs.getString("id")), - rs.getDate("date").toLocalDate(), - rs.getTime("startTime").toLocalTime(), - rs.getTime("endTime").toLocalTime(), - new Id<>(rs.getString("operationteamid")), - rs.getTimestamp("lastUpdate").toInstant() - ); - } - @Override - public void deleteOperation(Id<Operation> id) throws SQLException { + public Operation deleteOperation(Id<Operation> id) { var operation = findOperation(id); if(operation.isPresent()) { - conn.createStatement() - .executeUpdate("DELETE FROM operations WHERE id = " + quoted(id.value()) + ";"); + var sql = "DELETE FROM operations WHERE id = " + quoted(id.value()) + ";"; + + try { + conn.createStatement().executeUpdate(sql); + return operation.get(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } + return null; } - /** - * The structure of the operations table. - */ - private static final String CREATE_OPSTAFF_TABLE = """ - CREATE TABLE IF NOT EXISTS opstaff( - id VARCHAR(50) PRIMARY KEY, - role VARCHAR(50) NOT NULL, - specialty VARCHAR(50) NOT NULL, - lastUpdate TIMESTAMP NOT NULL - ); - """; - - /** - * Returns a new generated ID in the opstaff-SQLTable + * Returns a new generated ID in the opStaff-SQLTable * If the generated ID is already used, generate another one. * @return OPStaff ID */ @@ -423,7 +435,7 @@ class JDBCRepository implements Repository var id = new Id<OPStaff>(randomUUID().toString()); - return id; //TODO //findOperation(id).isEmpty() ? id : operationId(); + return findOPStaff(id).isEmpty() ? id : opStaffId(); } /** @@ -432,17 +444,14 @@ class JDBCRepository implements Repository * @param opStaff the OPStaff to save into the database */ @Override - public void saveOPStaff(OPStaff opStaff) { - + public void save(OPStaff opStaff) { try ( var stmt = conn.createStatement() ){ - //TODO - var sql = + var sql = findOPStaff(opStaff.id()).isPresent() ? - updateOPStaffSQL(opStaff) : - insertOPStaffSQL(opStaff); - //var sql = insertOPStaffSQL(opStaff); + updateSQL(opStaff) : + insertSQL(opStaff); stmt.executeUpdate(sql); @@ -451,18 +460,6 @@ class JDBCRepository implements Repository } } - private static String insertOPStaffSQL(OPStaff opStaff){ - return - "INSERT INTO opstaff(" + - "id,role,specialty,lastUpdate" + - ") VALUES (" + - sqlValue(opStaff.id().value()) + "," + - sqlValue(opStaff.role()) + "," + - sqlValue(opStaff.specialty()) + "," + - sqlValue(opStaff.lastUpdate()) + - ");"; - } - /** * Reads an OPStaff from the OPStaff-SQLTable with the given ID. * If no OPStaff with this ID is found, it will return an empty Optional. @@ -474,7 +471,7 @@ class JDBCRepository implements Repository try ( var result = conn.createStatement() - .executeQuery("SELECT * FROM opstaff WHERE id = " + sqlValue(id.value()) + ";") + .executeQuery("SELECT * FROM opStaffs WHERE id = " + sqlValue(id.value()) + ";") ){ return result.next() ? @@ -487,25 +484,22 @@ class JDBCRepository implements Repository } /** - * Reads an OPStaff from the OPStaff-SQLTable with the given ID. - * If no OPStaff with this ID is found, it will return an empty Optional. - * @return the found Operation + * Reads all opStaffs in the opStaffs table + * @return all opStaffs */ @Override public List<OPStaff> findOPStaffs(){ try ( var result = conn.createStatement() - .executeQuery("SELECT * FROM opstaff;") + .executeQuery("SELECT * FROM opStaffs;") ){ - var opStaffs = new ArrayList<OPStaff>(); - - while(result.next()){ - opStaffs.add(readOPStaffFromRow(result)); - } - - return opStaffs; + var opStaffs = new ArrayList<OPStaff>(); + while(result.next()){ + opStaffs.add(readOPStaffFromRow(result)); + } + return opStaffs; } catch (SQLException e){ throw new RuntimeException(e); @@ -513,49 +507,27 @@ class JDBCRepository implements Repository } /** - * Creates an Operation object with the given result of a read SQL-Row. - * @param rs the result of a read SQL-Row - * @return the created operation object - * @throws SQLException Error while reading the SQL-Row + * Connects to the database and deletes the OPStaff with the given id. + * @param id the id of the OPStaff to delete */ - - private static OPStaff readOPStaffFromRow(ResultSet rs) throws SQLException { - - return new OPStaff( - new Id<>(rs.getString("id")), - Role.valueOf(rs.getString("role")), - Specialty.valueOf(rs.getString("specialty")), - rs.getTimestamp("lastUpdate").toInstant() - ); - } - - /** - * Creates the update statement of the given OPStaff for SQL. - * @param opStaff the opStaff to turn into an update SQL statement - * @return the SQL statement - */ - private static String updateOPStaffSQL(OPStaff opStaff){ - return - "UPDATE opstaff SET " + - "role = " + sqlValue(opStaff.role()) + "," + - "specialty = " + sqlValue(opStaff.specialty()) + "," + - "lastUpdate = " + sqlValue(opStaff.lastUpdate()) + " " + - "WHERE id = " + sqlValue(opStaff.id().value()) + ";"; - } - @Override - public void deleteOPStaff(Id<OPStaff> id) throws SQLException{ + public OPStaff deleteOPStaff(Id<OPStaff> id) { var opStaff = findOPStaff(id); if(opStaff.isPresent()) { - conn.createStatement().executeUpdate( - "DELETE FROM opstaff WHERE id = " + quoted(id.value()) + ";"); + var sql = "DELETE FROM opStaffs WHERE id = " + quoted(id.value()) + ";"; + try { + conn.createStatement().executeUpdate(sql); + return opStaff.get(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } + return null; } - /** * Returns a new generated ID in the operationTeam-SQLTable * If the generated ID is already used, generate another one. @@ -566,8 +538,7 @@ class JDBCRepository implements Repository var id = new Id<OperationTeam>(randomUUID().toString()); - return id; //TODO - // findOperation(id).isEmpty() ? id : operationId(); + return findOperationTeam(id).isEmpty() ? id : operationTeamId(); } /** @@ -576,17 +547,14 @@ class JDBCRepository implements Repository * @param operationTeam the OPStaff to save into the database */ @Override - public void saveOperationTeam(OperationTeam operationTeam) { - + public void save(OperationTeam operationTeam) { try ( var stmt = conn.createStatement() ){ - //TODO - /*var sql = - findOPStaff(opStaff.id()).isPresent() ? - updateOPStaffSQL(opStaff) : - insertOPStaffSQL(opStaff);*/ - var sql = insertOperationTeamSQL(operationTeam); + var sql = + findOperationTeam(operationTeam.id()).isPresent() ? + updateSQL(operationTeam) : + insertSQL(operationTeam); stmt.executeUpdate(sql); @@ -595,88 +563,70 @@ class JDBCRepository implements Repository } } - private static String insertOperationTeamSQL(OperationTeam operationTeam){ - return - "INSERT INTO operationteam(" + - "id,teamname,lastUpdate" + - ") VALUES (" + - sqlValue(operationTeam.operationTeamId().value()) + "," + - sqlValue(operationTeam.teamName()) + "," + - sqlValue(operationTeam.lastUpdate()) + - ");"; - } - - + /** + * Reads an operationTeam from the operationTeams-SQLTable with the given ID. + * If no OPStaff with this ID is found, it will return an empty Optional. + * @param id the ID to search for + * @return the found OperationTeam + */ @Override - public OperationTeam deleteOperationTeam(Id<OperationTeam> id) throws SQLException{ - - //var opStaff = findOPStaff(id); - - //if(opStaff.isPresent()) { - conn.createStatement().executeUpdate( - "DELETE FROM operationteam WHERE id = " + quoted(id.value()) + ";"); - - //} - return null; - } - - private static final String CREATE_OPERATIONTEAM_TABLE = """ - CREATE TABLE IF NOT EXISTS operationteam( - id VARCHAR(50) PRIMARY KEY, - teamname VARCHAR(50) NOT NULL, - lastUpdate TIMESTAMP NOT NULL - ); - """; - - public Optional<OperationTeam> getOperationTeamImp(Id<OperationTeam> id) throws SQLException{ - - try( + public Optional<OperationTeam> findOperationTeam(Id<OperationTeam> id){ + try ( var result = conn.createStatement() - .executeQuery("SELECT * FROM operationteam WHERE id = " + sqlValue(id.value()) + ";"); - ){ + .executeQuery("SELECT * FROM operationTeams WHERE id = " + sqlValue(id.value()) + ";") + ){ return result.next() ? - Optional.of(readOperationTeamFromRow(result)): - Optional.empty();} - catch(SQLException e){ + Optional.of(readOperationTeamFromRow(result)) : + Optional.empty(); + + } catch (SQLException e){ throw new RuntimeException(e); } } - - public List<OperationTeam> getOperationTeamsImp() throws SQLException{ - + @Override + public List<OperationTeam> findOperationTeams() { try( var result = conn.createStatement() - .executeQuery("SELECT * FROM operationteam;"); + .executeQuery("SELECT * FROM operationTeams;"); ) { - - var operationTeam = new ArrayList<OperationTeam>(); + var operationTeams = new ArrayList<OperationTeam>(); while (result.next()) { - operationTeam.add(readOperationTeamFromRow(result)); + operationTeams.add(readOperationTeamFromRow(result)); } - return operationTeam; + return operationTeams; + }catch(SQLException e){ throw new RuntimeException(e); } } + /** + * Connects to the database and deletes the OperationTeam with the given id. + * @param id the id of the OperationTeam to delete + */ + @Override + public OperationTeam deleteOperationTeam(Id<OperationTeam> id) { - private static OperationTeam readOperationTeamFromRow(ResultSet rs) throws SQLException{ - - return new OperationTeam( - new Id<>(rs.getString("id")), - rs.getString("teamname"), - rs.getTimestamp("lastupdate").toInstant() - ); - } - + var operationTeam = findOperationTeam(id); + if(operationTeam.isPresent()) { + var sql = "DELETE FROM operationTeams WHERE id = " + quoted(id.value()) + ";"; + try { + conn.createStatement().executeUpdate(sql); + return operationTeam.get(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return null; + } /** @@ -687,13 +637,10 @@ class JDBCRepository implements Repository @Override public Id<TeamMember> teamMemberId() { - var id = new Id<TeamMember>(randomUUID().toString()); - - return id; //TODO - // findOperation(id).isEmpty() ? id : operationId(); + return new Id<TeamMember>(randomUUID().toString()); //TODO findOperation(id).isEmpty() ? id : operationId(); } - public void saveTeamMember(TeamMember teamMember) throws Exception{ + public void saveTeamMember(TeamMember TeamMember) throws Exception{ try( var stmt = conn.createStatement() @@ -703,7 +650,7 @@ class JDBCRepository implements Repository findOPStaff(opStaff.id()).isPresent() ? updateOPStaffSQL(opStaff) : insertOPStaffSQL(opStaff);*/ - var sql = saveTeamMemberSQL(teamMember); + var sql = insertSQL(TeamMember); stmt.executeUpdate(sql); @@ -712,29 +659,6 @@ class JDBCRepository implements Repository } } - private static String saveTeamMemberSQL(TeamMember teamMember){ - return - "INSERT INTO teammember(" + - "id,operationteamid,opstaffid,lastUpdate" + - ") VALUES (" + - sqlValue(teamMember.teamMemberId().value()) + "," + - sqlValue(teamMember.operationTeamId()) + "," + - sqlValue(teamMember.opStaffId()) + "," + - sqlValue(teamMember.lastUpdate()) + - ");"; - } - - private static final String CREATE_TEAMMEMBER_TABLE = """ - CREATE TABLE IF NOT EXISTS teammember( - id VARCHAR(50) PRIMARY KEY, - operationteamid VARCHAR(50), - opstaffid VARCHAR(50), - lastUpdate TIMESTAMP NOT NULL, - FOREIGN KEY (operationteamid) REFERENCES operationteam(id), - FOREIGN KEY (opstaffid) REFERENCES opstaff(id) - ); - """; - @Override public List<OPStaff> getOperationTeamOPStaffImpl(Id<OperationTeam> operationTeamId){ @@ -742,10 +666,10 @@ class JDBCRepository implements Repository var result = conn.createStatement() .executeQuery("SELECT DISTINCT opstaff.id, opstaff.role, opstaff.specialty, opstaff.lastupdate " + - "FROM opstaff " + - "JOIN teammember " + - "ON (opstaff.id = teammember.opstaffid) " + - "WHERE teammember.operationteamid= " + sqlValue(operationTeamId.value()) + ";"); + "FROM opstaff " + + "JOIN teammember " + + "ON (opstaff.id = teammember.opstaffid) " + + "WHERE teammember.operationteamid = " + sqlValue(operationTeamId.value()) + ";"); ) { //SELECT DISTINCT opstaff.id, opstaff.role, opstaff.specialty, opstaff.lastupdate, teammember.operationteamid FROM opstaff JOIN teammember ON (opstaff.id = teammember.opstaffid); //SELECT DISTINCT opstaff.id, opstaff.role, opstaff.specialty, opstaff.lastupdate, teammember.operationteamid FROM opstaff JOIN teammember ON (opstaff.id = teammember.opstaffid) WHERE teammember.operationteamid= '31313131'; @@ -760,7 +684,7 @@ class JDBCRepository implements Repository } } - + //endregion } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index 7ef506a501b8e08606b66aa9f06cb943f234a391..0cda509b3fe2b4938d5d7294b657c87dde32ecc4 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -17,6 +17,8 @@ public final class Tests private static Repository repo = null; + private static Patient testPatient = null; + private static Operation testOperation = null; private static OperationService opService; @@ -42,12 +44,23 @@ public final class Tests repo = JDBCRepository.instance(); + testPatient = new Patient( + new Id<>("9999") + ); + + testOperationTeam = new OperationTeam( + new Id<>("31"), + ("AmCoolsten"), + Instant.now() + ); + testOperation = new Operation( new Id<>("1111"), LocalDate.of(2024, 05, 24), LocalTime.of(13, 35, 00), LocalTime.of(14, 00, 00), - new Id<>("31313131"), + testPatient.id(), + testOperationTeam.id(), Instant.now()); opService = new OperationServiceImpl(repo); @@ -60,19 +73,12 @@ public final class Tests opStaffService = new OPStaffServiceImpl(repo); - - testOperationTeam = new OperationTeam( - new Id<>("31"), - ("AmCoolsten"), - Instant.now() - ); - operationTeamService = new OperationTeamImpl(repo) { }; testTeamMember = new TeamMember( new Id<>("TeamMember2"), - testOperationTeam.operationTeamId(), + testOperationTeam.id(), testOPStaff.id(), Instant.now() ); @@ -81,10 +87,11 @@ public final class Tests } - //@Test + @Test public void testRepoOperationSave(){ try { + repo.save(testOperationTeam); repo.save(testOperation); } catch (Exception e){ e.printStackTrace(); @@ -95,10 +102,11 @@ public final class Tests ); } - //@Test + @Test public void testRepoOperationUpdate() { try { + repo.save(testOperationTeam); repo.save(testOperation); } catch (Exception e){ e.printStackTrace(); @@ -109,6 +117,7 @@ public final class Tests LocalDate.of(1999, 01, 01), testOperation.startTime(), testOperation.endTime(), + testOperation.patientId(), testOperation.operationTeamId(), Instant.now() ); @@ -127,13 +136,15 @@ public final class Tests //@Test public void testRepoOperationDelete() { try { + repo.save(testOperationTeam); repo.save(testOperation); } catch (Exception e){ e.printStackTrace(); } try { - repo.deleteOperation(testOperation.id()); + //repo.deleteOperationTeam(testOperationTeam.id()); + repo.deleteOperation(testOperation.id()); } catch (SQLException e) { throw new RuntimeException(e); } @@ -146,6 +157,7 @@ public final class Tests LocalDate.of(2025, 05, 11), LocalTime.of(10, 30, 00), LocalTime.of(11, 00, 00), + new Id<Patient>("8888"), new Id<OperationTeam>("31313131") ); @@ -166,7 +178,8 @@ public final class Tests Optional.of(LocalDate.of(1999, 01, 01)), Optional.empty(), Optional.empty(), - new Id<OperationTeam>("31") + Optional.empty(), + Optional.empty() ); opService.getOperation(testOperation.id()); @@ -228,7 +241,7 @@ public final class Tests //@Test public void testCreateOPStaff_1() { try { - repo.saveOPStaff(testOPStaff); + repo.save(testOPStaff); } catch (Exception e){ e.printStackTrace(); } @@ -303,7 +316,7 @@ public final class Tests public void testCreateOperationTeam() throws Exception{ try { - repo.saveOperationTeam(testOperationTeam); + repo.save(testOperationTeam); } catch (Exception e){ e.printStackTrace(); }