diff --git a/opp/core/src/main/java/OPStaffService.java b/opp/core/src/main/java/OPStaffService.java index 762e4407eaaeb23449e85a5995ea5ab3ec4db524..aaed926d2d676bc02bf935a1f258aee489ed59b0 100644 --- a/opp/core/src/main/java/OPStaffService.java +++ b/opp/core/src/main/java/OPStaffService.java @@ -20,6 +20,6 @@ public interface OPStaffService { */ Optional<OPStaff> getOPStaff(Id<OPStaff> id); - List<OPStaff> getAllOPStaffs(); + //List<OPStaff> getAllOPStaffs(); } \ No newline at end of file diff --git a/opp/core/src/main/java/OPStaffServiceImpl.java b/opp/core/src/main/java/OPStaffServiceImpl.java index 134be4f836db96d97492d4235fd64bb93a8545b9..bfe6b15c508fa790d3041ea7f9ff5a2aee0b77cb 100644 --- a/opp/core/src/main/java/OPStaffServiceImpl.java +++ b/opp/core/src/main/java/OPStaffServiceImpl.java @@ -55,9 +55,15 @@ public class OPStaffServiceImpl implements OPStaffService{ public OPStaff delete(OPStaff.Delete del) throws Exception{ - if(repo.findOPStaffOperationTeams(del.id())==false){ //wenn false, dann ist er keinem team zugewiesen - return repo.deleteOPStaff(del.id()); + Optional<OPStaff> foundOPStaff = repo.findOPStaff(del.id()); + + if (foundOPStaff.isPresent()) { + if(!repo.findOPStaffOperationTeams(del.id())){ + return repo.deleteOPStaff(del.id()); + } + return foundOPStaff.get(); } + return null; } @@ -66,10 +72,12 @@ public class OPStaffServiceImpl implements OPStaffService{ return repo.findOPStaff(id); } + /* @Override public List<OPStaff> getAllOPStaffs(){ return repo.findAllOPStaffs(); } + */ } diff --git a/opp/core/src/main/java/Operation.java b/opp/core/src/main/java/Operation.java index 1c086a3b27b7c1c0ed7b496b714add5a99dc446a..acfca8a6a24748f62a50e8580deccaf808a8143e 100644 --- a/opp/core/src/main/java/Operation.java +++ b/opp/core/src/main/java/Operation.java @@ -11,7 +11,6 @@ import java.util.Optional; * @param endTime the planned ending time of the operation * @param patientId the id of the patient * @param operationTeamId the id of the operationTeam - * @param preparationNoteId the id of the preparationNote * @param roomId the id of the room * @param lastUpdate the last time this object was changed */ @@ -22,7 +21,6 @@ public record Operation( LocalTime endTime, Id<Patient> patientId, Id<OperationTeam> operationTeamId, - Id<PreparationNote> preparationNoteId, Id<Room> roomId, Instant lastUpdate ) { @@ -52,7 +50,6 @@ public record Operation( * @param endTime the ending time of the new Operation * @param patientId the id of the patient * @param operationTeamId the id of the operationTeam - * @param preparationNoteId the id of the preparationNote * @param roomId the id of the room */ public record Create( @@ -61,7 +58,6 @@ public record Operation( LocalTime endTime, Id<Patient> patientId, Id<OperationTeam> operationTeamId, - Id<PreparationNote> preparationNoteId, Id<Room> roomId ) implements Command {} @@ -73,7 +69,6 @@ public record Operation( * @param endTime optional new end time * @param patientId optional new patient id * @param operationTeamId optional new operation team - * @param preparationNoteId optional new preparation note * @param roomId optional new room */ public record Update( @@ -83,7 +78,6 @@ public record Operation( Optional<LocalTime> endTime, Optional<Id<Patient>> patientId, Optional<Id<OperationTeam>> operationTeamId, - Optional<Id<PreparationNote>> preparationNoteId, Optional<Id<Room>> roomId ) implements Command {} diff --git a/opp/core/src/main/java/OperationServiceImpl.java b/opp/core/src/main/java/OperationServiceImpl.java index c36508e9296184566d3a608bd1745a3ce85ea96b..f76cf9021a8357f64198cd15c33b70498275d0f3 100644 --- a/opp/core/src/main/java/OperationServiceImpl.java +++ b/opp/core/src/main/java/OperationServiceImpl.java @@ -51,7 +51,6 @@ public class OperationServiceImpl implements OperationService { cr.endTime(), cr.patientId(), cr.operationTeamId(), - cr.preparationNoteId(), cr.roomId(), Instant.now()); @@ -82,7 +81,6 @@ public class OperationServiceImpl implements OperationService { up.endTime().orElse(currentOperation.endTime()), up.patientId().orElse(currentOperation.patientId()), up.operationTeamId().orElse(currentOperation.operationTeamId()), - up.preparationNoteId().orElse(currentOperation.preparationNoteId()), up.roomId().orElse(currentOperation.roomId()), Instant.now()); @@ -102,6 +100,12 @@ public class OperationServiceImpl implements OperationService { Optional<Operation> foundOperation = repo.findOperation(del.id()); if (foundOperation.isPresent()) { + List<PreparationNote> allPreparationNotes = repo.findAllPreparationNotes(); + for (PreparationNote preparationNote : allPreparationNotes) { + if (preparationNote.operationsId().value().equals(foundOperation.get().id().value())) { + repo.deletePreparationNote(preparationNote.id()); + } + } return repo.deleteOperation(del.id()); } return null; diff --git a/opp/core/src/main/java/OperationTeamImpl.java b/opp/core/src/main/java/OperationTeamImpl.java index e1e50ffa67a6901b599375599cfb0350abdf1899..dbec376179822310ba5baa7cd470848a025017e0 100644 --- a/opp/core/src/main/java/OperationTeamImpl.java +++ b/opp/core/src/main/java/OperationTeamImpl.java @@ -23,21 +23,19 @@ public class OperationTeamImpl implements OperationTeamService{ }; } - private OperationTeam delete(OperationTeam.Delete del) throws Exception { + private OperationTeam delete(OperationTeam.Delete del) { - Optional<OperationTeam> returnedOperationTeam = repo.findOperationTeam(del.operationTeamId()); + Optional<OperationTeam> foundOperationTeam = repo.findOperationTeam(del.operationTeamId()); - if(returnedOperationTeam.isPresent()) { - if (returnedOperationTeam.get().opStaffs().isEmpty()) { - return repo.deleteOperationTeam(del.operationTeamId()); - } else { - int sizeOPStafflist = returnedOperationTeam.get().opStaffs().size(); - for(int i = 0; i<sizeOPStafflist; i++){ - repo.removeOPStaffInOperationTeam(del.operationTeamId(),returnedOperationTeam.get().opStaffs().get(i).id()); + if(foundOperationTeam.isPresent()) { + + if (!foundOperationTeam.get().opStaffs().isEmpty()) { + for (OPStaff opStaff : foundOperationTeam.get().opStaffs()) { + repo.removeOPStaffInOperationTeam(del.operationTeamId(), opStaff.id()); } return repo.deleteOperationTeam(del.operationTeamId()); - } + return foundOperationTeam.get(); } return null; } @@ -92,11 +90,13 @@ public class OperationTeamImpl implements OperationTeamService{ return null; } + /* @Override public List<OperationTeam> getAllOperationTeams() throws SQLException { return repo.findOperationTeams(); } + */ @Override diff --git a/opp/core/src/main/java/OperationTeamService.java b/opp/core/src/main/java/OperationTeamService.java index 88d3ae2f2b43156dd371045e2319fbb7a9691ce3..171ab4da85122dcae6d3b598c365adfde5cba3ce 100644 --- a/opp/core/src/main/java/OperationTeamService.java +++ b/opp/core/src/main/java/OperationTeamService.java @@ -8,6 +8,6 @@ public interface OperationTeamService { public Optional<OperationTeam> getOperationTeam(Id<OperationTeam> operationTeamId) throws SQLException; - public List<OperationTeam> getAllOperationTeams() throws SQLException; + //public List<OperationTeam> getAllOperationTeams() throws SQLException; } diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 95eae49b8407de71d4151fa5b76a5ccc812fda82..3ede90d2a4d1e30f3b03801cfcd0c83900db17d2 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -3,34 +3,23 @@ import java.util.List; import java.util.Optional; public interface Repository { -/* - Id<Patient> patientId(); - - void createPatient(Patient patient) throws Exception; - - Optional<Patient> findPatient(Id<Patient> id); - - Optional<Patient> deletePatient(Id<Patient> id) throws Exception; -*/ - // Hier dann die restlichen create/edit Methoden rein /** - * Return an unused operation ID. - * @return Operation ID + * Return an unused operation id + * @return generated operation id */ Id<Operation> operationId(); /** - * Saves the Operation into the database. - * @param operation the Operation to save into the database - * @throws Exception while saving + * Saves a new operation into the database, updates an already existing entry otherwise + * @param operation the operation to save/update to the database */ - void save(Operation operation) throws Exception; + void save(Operation operation); /** - * Returns the Operation with the given ID. - * @param id the ID to search for - * @return the found Operation + * Returns the operation with the given id. + * @param id the id to search for + * @return the found operation, if it exists */ Optional<Operation> findOperation(Id<Operation> id); @@ -42,80 +31,163 @@ public interface Repository { List<Operation> findOperations(Operation.Filter filter); /** - * Deletes the Operation with the matching id. - * @param id the id of the Operation to delete + * Deletes the operation with the matching id. + * @param id the id of the operation to delete + * @return the deleted operation */ - Operation deleteOperation(Id<Operation> id) throws SQLException; + Operation deleteOperation(Id<Operation> id); + /** - * Returns a new generated ID in the opstaff-SQLTable - * If the generated ID is already used, generate another one. - * @return OPStaff ID + * Return an unused op staff id + * @return generated op staff id */ - Id<OPStaff> opStaffId(); + Id<OPStaff> opStaffId(); /** - * Saves the OPStaff into the database. - * @param opStaff the OPStaff to save into the database - * @throws Exception while saving + * Saves a new op staff into the database, updates an already existing entry otherwise + * @param opStaff the op staff to save/update to the database */ - void save(OPStaff opStaff) throws Exception; + void save(OPStaff opStaff); /** - * 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. - * @param id the ID to search for - * @return the found Operation + * Returns the op staff with the given id. + * @param id the id to search for + * @return the found op staff, if it exists */ - Optional<OPStaff> findOPStaff(Id<OPStaff> id); + /** + * Returns a list of all op staffs + * @return all op staff entries + */ List<OPStaff> findAllOPStaffs(); - - OPStaff deleteOPStaff(Id<OPStaff> id) throws SQLException; - + /** + * Deletes the op staff with the matching id. + * @param id the id of the op staff to delete + * @return the deleted op staff + */ + OPStaff deleteOPStaff(Id<OPStaff> id); + /** + * Return an unused operation team id + * @return generated operation team id + */ Id<OperationTeam> operationTeamId(); - void save(OperationTeam operationTeam) throws Exception; + /** + * Saves a new operation team into the database, updates an already existing entry otherwise + * @param operationTeam the operation team to save/update to the database + */ + void save(OperationTeam operationTeam); + /** + * Returns the operation team with the given id. + * @param id the id to search for + * @return the found operation team, if it exists + */ Optional<OperationTeam> findOperationTeam(Id<OperationTeam> id); - List<OperationTeam> findOperationTeams() throws SQLException; - - OperationTeam deleteOperationTeam(Id<OperationTeam> id) throws SQLException; - - OperationTeam getOperationTeam(Id<OperationTeam> id); + /** + * Returns a list of all operation teams + * @return all operation team entries + */ + //List<OperationTeam> findOperationTeams() throws SQLException; + /** + * Deletes the operation team with the matching id. + * @param id the id of the operation team to delete + * @return the deleted operation team + */ + OperationTeam deleteOperationTeam(Id<OperationTeam> id); - Boolean assignOPStaffToOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId); + /** + * Return an unused preparation note id + * @return generated preparation note id + */ + Id<PreparationNote> preparationNoteId(); - Boolean removeOPStaffInOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) throws Exception; + /** + * Saves a new preparation note into the database, updates an already existing entry otherwise + * @param preparationNote the preparation note to save/update to the database + */ + void save(PreparationNote preparationNote); + /** + * Returns the preparation note with the given id. + * @param id the id to search for + * @return the found preparation note, if it exists + */ + Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id); - Boolean findOperationTeamOPStaff(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId); + /** + * Returns a list of all preparation notes + * @return all preparation note entries + */ + List<PreparationNote> findAllPreparationNotes(); - Boolean findOPStaffOperationTeams(Id<OPStaff> opStaffId); + /** + * Deletes the preparation note with the matching id. + * @param id the id of the preparation note to delete + * @return the deleted preparation note + */ + PreparationNote deletePreparationNote(Id<PreparationNote> id); - Id<PreparationNote> preparationNoteId(); + /** + * Return an unused room id + * @return generated room id + */ + Id<Room> roomId(); - Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id); + /** + * Saves a new room into the database, updates an already existing entry otherwise + * @param room the operation to save/update to the database + */ + void save(Room room); - void save(PreparationNote preparationNote); + /** + * Returns the preparation note with the given id. + * @param id the id to search for + * @return the found preparation note, if it exists + */ + Optional<Room> findRoom(Id<Room> id); - PreparationNote deletePreparationNote(Id<PreparationNote> id); + /** + * Deletes the room with the matching id. + * @param id the id of the room to delete + * @return the deleted room + */ + Room deleteRoom(Id<Room> id); - Id<Room> roomId(); - Optional<Room> findRoom(Id<Room> id); + /** + * Assigns an op staff to an operation team + * @param operationTeamId the id of the operation team to assign the op staff to + * @param opStaffId the id of the op staff + * @return true if the assign was successful, otherwise false + */ + Boolean assignOPStaffToOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId); - void save(Room room); + /** + * Removes an op staff from an operation team + * @param operationTeamId the id of the operation team to remove the op staff from + * @param opStaffId the id of the op staff + * @return true if the remove was successful, otherwise false + */ + Boolean removeOPStaffInOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId); - Room deleteRoom(Id<Room> id); + /** + * Searches if the op staff is already assigned to the operation team + * @param operationTeamId the id of the operation team + * @param opStaffId the id of the op staff + * @return true if the op staff is already assigned, otherwise false + */ + Boolean findOperationTeamOPStaff(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId); + Boolean findOPStaffOperationTeams(Id<OPStaff> opStaffId); } diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index 9b19fac04e400df8c864d1912e3390c2b3a8ebc9..d2ac4b3ac1c447b44350b7879079a519cc6ceb85 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -15,11 +15,14 @@ class JDBCRepository implements Repository private final Connection conn; + /** + * Sets the connection with the database + * @param conn the connection to the database + */ private JDBCRepository(Connection conn){ this.conn = conn; } - /** * Sets up a connection with the repository database via the set url, user and password. * @return the connected repository @@ -28,9 +31,9 @@ class JDBCRepository implements Repository try { var conn = DriverManager.getConnection( - System.getProperty("pms.repo.jdbc.url"), - System.getProperty("pms.repo.jdbc.user"), - System.getProperty("pms.repo.jdbc.password") + System.getProperty("opp.repo.jdbc.url"), + System.getProperty("opp.repo.jdbc.user"), + System.getProperty("opp.repo.jdbc.password") ); var repo = new JDBCRepository(conn); @@ -55,8 +58,10 @@ class JDBCRepository implements Repository endTime TIME NOT NULL, patientId VARCHAR(50), operationTeamId VARCHAR(50), + roomId VARCHAR(50), lastUpdate TIMESTAMP NOT NULL, - FOREIGN KEY (operationTeamId) REFERENCES operationTeams(id) + FOREIGN KEY (operationTeamId) REFERENCES operationTeams(id), + FOREIGN KEY (roomId) REFERENCES rooms(id) ); """; @@ -98,23 +103,26 @@ class JDBCRepository implements Repository """; /** - * The structure of the opStaffs table. + * The structure of the preparationNotes table. */ - private static final String CREATE_PREPARATIONNOTE_TABLE = """ - CREATE TABLE IF NOT EXISTS preparationNote( + private static final String CREATE_PREPARATION_NOTE_TABLE = """ + CREATE TABLE IF NOT EXISTS preparationNotes( id VARCHAR(50) PRIMARY KEY, note VARCHAR(50) NOT NULL, - operationsId VARCHAR(50) NOT NULL, + operationId VARCHAR(50), lastUpdate TIMESTAMP NOT NULL, - FOREIGN KEY (operationsId) REFERENCES operations(id) + FOREIGN KEY (operationId) REFERENCES operations(id) ); """; + /** + * The structure of the rooms table. + */ private static final String CREATE_ROOM_TABLE = """ - CREATE TABLE IF NOT EXISTS room( - id VARCHAR(50) PRIMARY KEY, - roomName VARCHAR(50) NOT NULL, - lastUpdate TIMESTAMP NOT NULL + CREATE TABLE IF NOT EXISTS rooms( + id VARCHAR(50) PRIMARY KEY, + roomName VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL ); """; @@ -132,10 +140,10 @@ class JDBCRepository implements Repository stmt.execute(CREATE_OPERATION_TEAM_TABLE); stmt.execute(CREATE_OP_STAFF_TABLE); + stmt.execute(CREATE_ROOM_TABLE); stmt.execute(CREATE_OPERATION_TABLE); stmt.execute(CREATE_OP_STAFF_IN_OPERATION_TEAM_TABLE); - stmt.execute(CREATE_PREPARATIONNOTE_TABLE); - stmt.execute(CREATE_ROOM_TABLE); + stmt.execute(CREATE_PREPARATION_NOTE_TABLE); } catch (SQLException e){ throw new RuntimeException(e); @@ -171,13 +179,13 @@ class JDBCRepository implements Repository /** * Creates the insert statement of the given operation for SQL. - * @param operation the operation to turn into an insert SQL statement + * @param operation the operation to create the insert SQL statement with * @return the SQL statement */ private static String insertSQL(Operation operation){ return "INSERT INTO operations(" + - "id,date,startTime,endTime,patientId,operationTeamId,lastUpdate" + + "id,date,startTime,endTime,patientId,operationTeamId,roomId,lastUpdate" + ") VALUES (" + sqlValue(operation.id().value()) + "," + sqlValue(operation.date()) + "," + @@ -185,13 +193,14 @@ class JDBCRepository implements Repository sqlValue(operation.endTime()) + "," + sqlValue(operation.patientId()) + "," + sqlValue(operation.operationTeamId()) + "," + + sqlValue(operation.roomId()) + "," + sqlValue(operation.lastUpdate()) + ");"; } /** * Creates the update statement of the given operation for SQL. - * @param operation the operation to turn into an update SQL statement + * @param operation the operation to create the update SQL statement with * @return the SQL statement */ private static String updateSQL(Operation operation){ @@ -202,13 +211,14 @@ class JDBCRepository implements Repository "endTime = " + sqlValue(operation.endTime()) + "," + "patientId = " + sqlValue(operation.patientId()) + "," + "operationTeamId = " + sqlValue(operation.operationTeamId()) + "," + + "roomId = " + sqlValue(operation.roomId()) + "," + "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 + * Creates the insert statement of the given opStaff for SQL. + * @param opStaff the opStaff to create the insert SQL statement with * @return the SQL statement */ private static String insertSQL(OPStaff opStaff){ @@ -224,8 +234,8 @@ class JDBCRepository implements Repository } /** - * Creates the update statement of the given OPStaff for SQL. - * @param opStaff the opStaff to turn into an update SQL statement + * Creates the update statement of the given opStaff for SQL. + * @param opStaff the opStaff to create the update SQL statement with * @return the SQL statement */ private static String updateSQL(OPStaff opStaff){ @@ -239,7 +249,7 @@ class JDBCRepository implements Repository /** * Creates the insert statement of the given operationTeam for SQL. - * @param operationTeam the operationTeam to turn into an insert SQL statement + * @param operationTeam the operationTeam to create the insert SQL statement with * @return the SQL statement */ private static String insertSQL(OperationTeam operationTeam){ @@ -255,7 +265,7 @@ class JDBCRepository implements Repository /** * Creates the update statement of the given operationTeam for SQL. - * @param operationTeam the operationTeam to turn into an update SQL statement + * @param operationTeam the operationTeam to create the update SQL statement with * @return the SQL statement */ private static String updateSQL(OperationTeam operationTeam){ @@ -266,6 +276,12 @@ class JDBCRepository implements Repository "WHERE id = " + sqlValue(operationTeam.id().value()) + ";"; } + /** + * Creates the insert statement for the opStaffsInOperationTeams table in SQL. + * @param operationTeamId the id of the operation team + * @param opStaffId the id of the op staff + * @return the SQL statement + */ private String insertSQL(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) { return "INSERT INTO opStaffsInOperationTeams(" + @@ -278,10 +294,69 @@ class JDBCRepository implements Repository } /** - * Creates an Operation object with the given result of a read SQL-Row. + * Creates the insert statement of the given preparationNote for SQL. + * @param preparationNote the preparationNote to create the insert SQL statement with + * @return the SQL statement + */ + private static String insertSQL(PreparationNote preparationNote){ + return + "INSERT INTO preparationNotes(" + + "id,note,operationId,lastUpdate" + + ") VALUES (" + + sqlValue(preparationNote.id().value()) + "," + + sqlValue(preparationNote.note()) + "," + + sqlValue(preparationNote.operationsId()) + "," + + sqlValue(preparationNote.lastUpdate()) + + ");"; + } + + /** + * Creates the update statement of the given preparationNote for SQL. + * @param preparationNote the preparationNote to create the update SQL statement with + * @return the SQL statement + */ + private static String updateSQL(PreparationNote preparationNote){ + return + "UPDATE preparationNotes SET " + + "note = " + sqlValue(preparationNote.note()) + "," + + "lastUpdate = " + sqlValue(preparationNote.lastUpdate()) + " " + + "WHERE id = " + sqlValue(preparationNote.id().value()) + ";"; + } + + /** + * Creates the insert statement of the given room for SQL. + * @param room the room to create the insert SQL statement with + * @return the SQL statement + */ + private static String insertSQL(Room room){ + return + "INSERT INTO rooms(" + + "id,roomName,lastUpdate" + + ") VALUES (" + + sqlValue(room.id().value()) + "," + + sqlValue(room.roomName()) + "," + + sqlValue(room.lastUpdate()) + + ");"; + } + + /** + * Creates the update statement of the given room for SQL. + * @param room the room to create the update SQL statement with + * @return the SQL statement + */ + private static String updateSQL(Room room){ + return + "UPDATE rooms SET " + + "roomName = " + sqlValue(room.roomName()) + "," + + "lastUpdate = " + sqlValue(room.lastUpdate()) + " " + + "WHERE id = " + sqlValue(room.id().value()) + ";"; + } + + /** + * 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 + * @throws SQLException while reading the result set */ private static Operation readOperationFromRow(ResultSet rs) throws SQLException { @@ -292,17 +367,17 @@ class JDBCRepository implements Repository rs.getTime("endTime").toLocalTime(), new Id<>(rs.getString("patientId")), new Id<>(rs.getString("operationTeamId")), + new Id<>(rs.getString("roomId")), rs.getTimestamp("lastUpdate").toInstant() ); } /** - * Creates an Operation object with the given result of a read SQL-Row. + * Creates an op staff 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 + * @return the created op staff object + * @throws SQLException while reading the result set */ - private static OPStaff readOPStaffFromRow(ResultSet rs) throws SQLException { return new OPStaff( @@ -313,14 +388,50 @@ class JDBCRepository implements Repository ); } - + /** + * Creates an operation team object with the given result of a read SQL-Row. + * @param rs the result of a read SQL-Row + * @return the created operation team object + * @throws SQLException while reading the result set + */ private static OperationTeam readOperationTeamFromRow(ResultSet rs) throws SQLException{ return new OperationTeam( new Id<>(rs.getString("id")), new ArrayList<>(), - rs.getString("teamname"), - rs.getTimestamp("lastupdate").toInstant() + rs.getString("teamName"), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + + /** + * Creates a preparation note object with the given result of a read SQL-Row. + * @param rs the result of a read SQL-Row + * @return the created preparation note object + * @throws SQLException while reading the result set + */ + private static PreparationNote readPreparationNoteFromRow(ResultSet rs) throws SQLException { + + return new PreparationNote( + new Id<>(rs.getString("id")), + rs.getString("note"), + new Id<>(rs.getString("operationId")), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + + /** + * Creates a room object with the given result of a read SQL-Row. + * @param rs the result of a read SQL-Row + * @return the created room object + * @throws SQLException while reading the result set + */ + private static Room readRoomFromRow(ResultSet rs) throws SQLException { + + return new Room( + new Id<>(rs.getString("id")), + rs.getString("roomName"), + rs.getTimestamp("lastUpdate").toInstant() ); } @@ -329,11 +440,7 @@ class JDBCRepository implements Repository //region REPOSITORY OPERATIONS - /** - * Returns a new generated ID in the operations-SQLTable - * If the generated ID is already used, generate another one. - * @return Operation ID - */ + @Override public Id<Operation> operationId() { @@ -342,11 +449,6 @@ class JDBCRepository implements Repository return findOperation(id).isEmpty() ? id : operationId(); } - /** - * 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 ( @@ -364,12 +466,6 @@ class JDBCRepository implements Repository } } - /** - * Reads an Operation from the Operations-SQLTable with the given ID. - * If no Operation with this ID is found, it will return an empty Optional. - * @param id the ID to search for - * @return the found Operation - */ @Override public Optional<Operation> findOperation(Id<Operation> id){ try ( @@ -387,13 +483,6 @@ class JDBCRepository implements Repository } } - /** - * Connects to the database and searches all filter criteria. - * Adds each found Operation to the list. - * @param filter the filter - * @return the matching entries - * TODO Ask how to remove "Optional[]" from filter.date() - */ @Override public List<Operation> findOperations(Operation.Filter filter) { @@ -424,10 +513,6 @@ class JDBCRepository implements Repository } } - /** - * Connects to the database and deletes the operation with the given id. - * @param id the id of the Operation to delete - */ @Override public Operation deleteOperation(Id<Operation> id) { var operation = findOperation(id); @@ -445,11 +530,7 @@ class JDBCRepository implements Repository return null; } - /** - * Returns a new generated ID in the opStaff-SQLTable - * If the generated ID is already used, generate another one. - * @return OPStaff ID - */ + @Override public Id<OPStaff> opStaffId() { @@ -458,11 +539,6 @@ class JDBCRepository implements Repository return findOPStaff(id).isEmpty() ? id : opStaffId(); } - /** - * Connects to the database and checks if the ID of the given OPStaff is already used. - * If yes, then update existing entry, else insert new entry. - * @param opStaff the OPStaff to save into the database - */ @Override public void save(OPStaff opStaff) { try ( @@ -480,12 +556,6 @@ 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. - * @param id the ID to search for - * @return the found Operation - */ @Override public Optional<OPStaff> findOPStaff(Id<OPStaff> id){ try ( @@ -503,10 +573,6 @@ class JDBCRepository implements Repository } } - /** - * Reads all opStaffs in the opStaffs table - * @return all opStaffs - */ @Override public List<OPStaff> findAllOPStaffs(){ try ( @@ -526,20 +592,22 @@ class JDBCRepository implements Repository } } - /** - * Connects to the database and deletes the OPStaff with the given id. - * @param id the id of the OPStaff to delete - */ @Override public OPStaff deleteOPStaff(Id<OPStaff> id) { var opStaff = findOPStaff(id); if(opStaff.isPresent()) { - var sql = "DELETE FROM opStaffs WHERE id = " + quoted(id.value()) + ";"; + + var opStaffsInOperationTeamsSQL = + "DELETE FROM opStaffsInOperationTeams WHERE opStaffId = " + + quoted(opStaff.get().id().value()); + + var opStaffSQL = "DELETE FROM opStaffs WHERE id = " + quoted(id.value()) + ";"; try { - conn.createStatement().executeUpdate(sql); + conn.createStatement().executeUpdate(opStaffsInOperationTeamsSQL); + conn.createStatement().executeUpdate(opStaffSQL); return opStaff.get(); } catch (SQLException e) { throw new RuntimeException(e); @@ -548,11 +616,7 @@ class JDBCRepository implements Repository return null; } - /** - * Returns a new generated ID in the operationTeam-SQLTable - * If the generated ID is already used, generate another one. - * @return operationTeamID - */ + @Override public Id<OperationTeam> operationTeamId() { @@ -561,11 +625,6 @@ class JDBCRepository implements Repository return findOperationTeam(id).isEmpty() ? id : operationTeamId(); } - /** - * Connects to the database and checks if the ID of the given OperationTeam is already used. - * If yes, then update existing entry, else insert new entry. - * @param operationTeam the OPStaff to save into the database - */ @Override public void save(OperationTeam operationTeam) { try ( @@ -583,12 +642,6 @@ class JDBCRepository implements Repository } } - /** - * 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 Optional<OperationTeam> findOperationTeam(Id<OperationTeam> id){ try ( @@ -605,12 +658,9 @@ class JDBCRepository implements Repository .executeQuery("SELECT * FROM operationTeams WHERE id = " + sqlValue(id.value()) + ";") ){ - System.out.println("Result: " + resultOperationTeam); - if(resultOperationTeam.next()) { OperationTeam operationTeam = readOperationTeamFromRow(resultOperationTeam); while(resultOPStaffs.next()) { - System.out.println("Result: " + resultOPStaffs); operationTeam.opStaffs().add(readOPStaffFromRow(resultOPStaffs)); } return Optional.of(operationTeam); @@ -623,25 +673,7 @@ class JDBCRepository implements Repository } } - public OperationTeam getOperationTeam(Id<OperationTeam> id){ - try( - var result = - conn.createStatement() - .executeQuery("SELECT * FROM operationTeams WHERE id = " + sqlValue(id.value()) + ";") - ) { - - OperationTeam operationTeam = null; - - while (result.next()) { - operationTeam = readOperationTeamFromRow(result); - } - return operationTeam; - - }catch(SQLException e){ - throw new RuntimeException(e); - } - } - + /* @Override public List<OperationTeam> findOperationTeams() { try( @@ -661,21 +693,28 @@ class JDBCRepository implements Repository } } - - /** - * 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) { var operationTeam = findOperationTeam(id); if(operationTeam.isPresent()) { - var sql = "DELETE FROM operationTeams WHERE id = " + quoted(id.value()) + ";"; + + for (OPStaff opStaff : operationTeam.get().opStaffs()) { + removeOPStaffInOperationTeam(operationTeam.get().id(), opStaff.id()); + } + + var opStaffsInOperationTeamsSQL = + "DELETE FROM opStaffsInOperationTeams WHERE operationTeamId = " + + quoted(operationTeam.get().id().value()); + + var operationTeamsSQL = "DELETE FROM operationTeams WHERE id = " + quoted(id.value()) + ";"; try { - conn.createStatement().executeUpdate(sql); + conn.createStatement().executeUpdate(opStaffsInOperationTeamsSQL); + conn.createStatement().executeUpdate(operationTeamsSQL); return operationTeam.get(); } catch (SQLException e) { throw new RuntimeException(e); @@ -684,249 +723,139 @@ class JDBCRepository implements Repository return null; } - /* - @Override - public Id<String> opStaffsInOperationTeamsId() { - var id = new Id<String>(randomUUID().toString()); - return findOPStaffsInOperationTeam(id).isEmpty() ? id : opStaffsInOperationTeamsId(); - } -*/ - /* @Override - public Optional<Map<OperationTeam, OPStaff>> findOPStaffsInOperationTeam(Id<String> id) { - try ( - var resultOPStaff = - conn.createStatement() - .executeQuery("SELECT DISTINCT opStaffs.id, opStaffs.role, opStaffs.specialty, opStaffs.lastUpdate " + - "FROM opStaffs " + - "JOIN opStaffsInOperationTeams " + - "ON (opStaffs.id = opStaffsInOperationTeams.opStaffId) " + - "WHERE opStaffsInOperationTeams.id = " + sqlValue(id.value()) + ";"); - - var resultOperationTeam = - conn.createStatement() - .executeQuery("SELECT DISTINCT operationTeams.id, operationTeams.teamName, operationTeams.lastUpdate " + - "FROM operationTeams " + - "JOIN opStaffsInOperationTeams " + - "ON (operationTeams.id = opStaffsInOperationTeams.operationTeamId) " + - "WHERE opStaffsInOperationTeams.id = " + sqlValue(id.value()) + ";") - ){ - - if (resultOperationTeam.next() && resultOPStaff.next()) { - OperationTeam operationTeam = readOperationTeamFromRow(resultOperationTeam); - OPStaff opStaff = readOPStaffFromRow(resultOPStaff); + public Id<PreparationNote> preparationNoteId() { - return Optional.of(Map.of(operationTeam, opStaff)); - } else { - return Optional.empty(); - } + var id = new Id<PreparationNote>(randomUUID().toString()); - } catch (SQLException e){ - throw new RuntimeException(e); - } - }*/ + return findPreparationNote(id).isEmpty() ? id : preparationNoteId(); + } - //TODO Check what exactly happens when assigning the same staff to the same team twice. @Override - public Boolean assignOPStaffToOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) { + public void save(PreparationNote preparationNote) { try ( var stmt = conn.createStatement() ){ var sql = - findOperationTeam(operationTeamId).isPresent() && - findOPStaff(opStaffId).isPresent() && - !findOperationTeamOPStaff(operationTeamId, opStaffId) ? - insertSQL(operationTeamId, opStaffId) : - null; - - if (sql != null) { - stmt.executeUpdate(sql); - return true; - } else { - return false; - } - - } catch (SQLException e){ - throw new RuntimeException(e); - } - } - - @Override - public Boolean removeOPStaffInOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) throws Exception{ - - try( - var stmt = conn.createStatement() - ){ - var sql = "DELETE FROM opStaffsInOperationTeams WHERE operationTeamId = " + quoted(operationTeamId.value()) + - " AND opStaffId = " + quoted(opStaffId.value()) + ";"; + findPreparationNote (preparationNote.id()).isPresent() ? + updateSQL(preparationNote) : + insertSQL(preparationNote ); stmt.executeUpdate(sql); - return true; - - }catch(SQLException e){ + } catch (SQLException e){ throw new RuntimeException(e); } } @Override - public Boolean findOperationTeamOPStaff(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId){ - - try( + public Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id){ + try ( var result = conn.createStatement() - .executeQuery("SELECT * " + - "FROM opStaffsInOperationTeams " + - "WHERE opStaffsInOperationTeams.operationTeamId = " + quoted(operationTeamId.value()) + " " + - "AND opStaffsInOperationTeams.opStaffId = " + quoted(opStaffId.value()) + ";") - ) { - return result.next(); - }catch(SQLException e){ - throw new RuntimeException(e); - } - } - - @Override - public Boolean findOPStaffOperationTeams(Id<OPStaff> opStaffId){ + .executeQuery("SELECT * FROM preparationNotes WHERE id = " + sqlValue(id.value()) + ";") + ){ + return + result.next() ? + Optional.of(readPreparationNoteFromRow(result)) : + Optional.empty(); - try( - var result = - conn.createStatement() - .executeQuery("SELECT * " + - "FROM opStaffsInOperationTeams " + - "WHERE opStaffsInOperationTeams.opStaffId = " + quoted(opStaffId.value()) + ";") - ) { - return result.next(); - }catch(SQLException e){ + } catch (SQLException e){ throw new RuntimeException(e); } } - /* @Override - public List<OPStaff> getOperationTeamOPStaffImpl(Id<OperationTeam> operationTeamId){ - + public List<PreparationNote> findAllPreparationNotes() { try( var result = conn.createStatement() - .executeQuery("SELECT DISTINCT opStaffs.id, opStaffs.role, opStaffs.specialty, opStaffs.lastupdate " + - "FROM opStaffs " + - "JOIN opStaffsInOperationTeams " + - "ON (opStaffs.id = opStaffsInOperationTeams.opstaffid) " + - "WHERE opStaffsInOperationTeams.operationteamid = '"+operationTeamId.value()+"';"); - //SELECT DISTINCT opStaffs.id, opStaffs.role, opStaffs.specialty, opStaffs.lastupdate FROM opStaffs JOIN opStaffsInOperationTeams ON (opStaffs.id = '4444') WHERE opStaffsInOperationTeams.operationteamid = '31';S + .executeQuery("SELECT * FROM preparationNotes;") ) { - //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'; - var opStaffs = new ArrayList<OPStaff>(); + var preparationNotes = new ArrayList<PreparationNote>(); while (result.next()) { - opStaffs.add(readOPStaffFromRow(result)); + preparationNotes.add(readPreparationNoteFromRow(result)); } - return opStaffs; + return preparationNotes; + }catch(SQLException e){ throw new RuntimeException(e); } } - */ - - //endregion - - - @Override - public Id<PreparationNote> preparationNoteId() { - - var id = new Id<PreparationNote>(randomUUID().toString()); - - return findPreparationNote(id).isEmpty() ? id : preparationNoteId(); - } + public PreparationNote deletePreparationNote(Id<PreparationNote> id) { + var preparationNote = findPreparationNote(id); - public Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id){ - try ( - var result = - conn.createStatement() - .executeQuery("SELECT * FROM preparationNote WHERE id = " + sqlValue(id.value()) + ";") - ){ - return - result.next() ? - Optional.of(readPreparationNoteFromRow(result)) : - Optional.empty(); + if(preparationNote.isPresent()) { + var sql = "DELETE FROM preparationNotes WHERE id = " + quoted(id.value()) + ";"; - } catch (SQLException e){ + try { + conn.createStatement().executeUpdate(sql); + return preparationNote.get(); + } catch (SQLException e) { throw new RuntimeException(e); } + } + return null; } - private static PreparationNote readPreparationNoteFromRow(ResultSet rs) throws SQLException { + @Override + public Id<Room> roomId() { - return new PreparationNote( - new Id<>(rs.getString("id")), - rs.getString("note"), - new Id<>(rs.getString("operationsId")), - rs.getTimestamp("lastUpdate").toInstant() - ); + var id = new Id<Room>(randomUUID().toString()); + + return findRoom(id).isEmpty() ? id : roomId(); } @Override - public void save(PreparationNote preparationNote) { + public void save(Room room) { try ( var stmt = conn.createStatement() ){ var sql = - findPreparationNote (preparationNote.id()).isPresent() ? - updateSQL(preparationNote) : - insertSQL(preparationNote ); + findRoom(room.id()).isPresent() ? + updateSQL(room) : + insertSQL(room); stmt.executeUpdate(sql); + } catch (SQLException e){ throw new RuntimeException(e); } } - /* - CREATE TABLE IF NOT EXISTS preparationNote( - id VARCHAR(50) PRIMARY KEY, - note VARCHAR(50) NOT NULL, - operationsId VARCHAR(50) NOT NULL, - lastUpdate TIMESTAMP NOT NULL, - FOREIGN KEY (operationsId) REFERENCES operations(id) - ); - */ - private static String updateSQL(PreparationNote preparationNote){ - return - "UPDATE preparationNote SET " + - "note = " + sqlValue(preparationNote.note()) + "," + - "lastUpdate = " + sqlValue(preparationNote.lastUpdate()) + " " + - "WHERE id = " + sqlValue(preparationNote.id().value()) + ";"; - } + @Override + public Optional<Room> findRoom(Id<Room> id){ + try ( + var result = + conn.createStatement() + .executeQuery("SELECT * FROM rooms WHERE id = " +sqlValue(id.value()) + ";") + ){ + return + result.next() ? + Optional.of(readRoomFromRow(result)) : + Optional.empty(); - private static String insertSQL(PreparationNote preparationNote){ - return - "INSERT INTO preparationNote(" + - "id,note,operationsId,lastUpdate" + - ") VALUES (" + - sqlValue(preparationNote.id().value()) + "," + - sqlValue(preparationNote.note()) + "," + - sqlValue(preparationNote.operationsId()) + "," + - sqlValue(preparationNote.lastUpdate()) + - ");"; + } catch (SQLException e){ + throw new RuntimeException(e); + } } @Override - public PreparationNote deletePreparationNote(Id<PreparationNote> id) { - var preparationNote = findPreparationNote(id); + public Room deleteRoom(Id<Room> id) { + var room = findRoom(id); - if(preparationNote.isPresent()) { - var sql = "DELETE FROM preparationNote WHERE id = " + quoted(id.value()) + ";"; + if(room.isPresent()) { + var sql = "DELETE FROM rooms WHERE id =" + quoted(id.value()) + ";"; try { conn.createStatement().executeUpdate(sql); - return preparationNote.get(); + return room.get(); } catch (SQLException e) { throw new RuntimeException(e); } @@ -934,93 +863,84 @@ class JDBCRepository implements Repository return null; } - @Override - public Id<Room> roomId() { - - var id = new Id<Room>(randomUUID().toString()); - return findRoom(id).isEmpty() ? id : roomId(); - } - - public Optional<Room> findRoom(Id<Room> id){ + @Override + public Boolean assignOPStaffToOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) { try ( - var result = - conn.createStatement() - .executeQuery("SELECT * FROM room WHERE id = " +sqlValue(id.value()) + ";") + var stmt = conn.createStatement() ){ - return - result.next() ? - Optional.of(readRoomFromRow(result)) : - Optional.empty(); + var sql = + findOperationTeam(operationTeamId).isPresent() && + findOPStaff(opStaffId).isPresent() && + !findOperationTeamOPStaff(operationTeamId, opStaffId) ? + insertSQL(operationTeamId, opStaffId) : + null; + + if (sql != null) { + stmt.executeUpdate(sql); + return true; + } else { + return false; + } } catch (SQLException e){ throw new RuntimeException(e); } } - private static Room readRoomFromRow(ResultSet rs) throws SQLException { - - return new Room( - new Id<>(rs.getString("id")), - rs.getString("roomName"), - rs.getTimestamp("lastUpdate").toInstant() - ); - } - @Override - public void save(Room room) { - try ( + public Boolean removeOPStaffInOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) { + + try( var stmt = conn.createStatement() ){ - var sql = - findRoom(room.id()).isPresent() ? - updateSQL(room) : - insertSQL(room); + var sql = "DELETE FROM opStaffsInOperationTeams WHERE operationTeamId = " + quoted(operationTeamId.value()) + + " AND opStaffId = " + quoted(opStaffId.value()) + ";"; stmt.executeUpdate(sql); + return true; - } catch (SQLException e){ + }catch(SQLException e){ throw new RuntimeException(e); } } - private static String updateSQL(Room room){ - return - "UPDATE room SET " + - "roomName = " + sqlValue(room.roomName()) + "," + - "lastUpdate = " + sqlValue(room.lastUpdate()) + " " + - "WHERE id = " + sqlValue(room.id().value()) + ";"; - } + @Override + public Boolean findOperationTeamOPStaff(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId){ - private static String insertSQL(Room room){ - return - "INSERT INTO room(" + - "id,roomName,lastUpdate" + - ") VALUES (" + - sqlValue(room.id().value()) + "," + - sqlValue(room.roomName()) + "," + - sqlValue(room.lastUpdate()) + - ");"; + try( + var result = + conn.createStatement() + .executeQuery("SELECT * " + + "FROM opStaffsInOperationTeams " + + "WHERE opStaffsInOperationTeams.operationTeamId = " + quoted(operationTeamId.value()) + " " + + "AND opStaffsInOperationTeams.opStaffId = " + quoted(opStaffId.value()) + ";") + ) { + return result.next(); + }catch(SQLException e){ + throw new RuntimeException(e); + } } @Override - public Room deleteRoom(Id<Room> id) { - var room = findRoom(id); - - if(room.isPresent()) { - var sql = "DELETE FROM room WHERE id =" + quoted(id.value()) + ";"; + public Boolean findOPStaffOperationTeams(Id<OPStaff> opStaffId){ - try { - conn.createStatement().executeUpdate(sql); - return room.get(); - } catch (SQLException e) { - throw new RuntimeException(e); - } + try( + var result = + conn.createStatement() + .executeQuery("SELECT * " + + "FROM opStaffsInOperationTeams " + + "WHERE opStaffsInOperationTeams.opStaffId = " + quoted(opStaffId.value()) + ";") + ) { + return result.next(); + }catch(SQLException e){ + throw new RuntimeException(e); } - return null; } + //endregion + } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index 8c29b66e51b2e024d1f4c33b74c5093af1c201e5..ca007aa3da2d82eadc6b74582ca7839258b139ce 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -28,6 +28,7 @@ public final class Tests private static OPStaff testOPStaff1 = null; private static OPStaff testOPStaff2 = null; + private static OPStaff testOPStaff3 = null; private static OPStaffService opStaffService; @@ -36,7 +37,9 @@ public final class Tests private static OperationTeamService operationTeamService; - private static PreparationNote testPreparationNote = null; + private static PreparationNote testPreparationNote1 = null; + private static PreparationNote testPreparationNote2 = null; + private static PreparationNoteService preparationNoteService; @@ -47,9 +50,9 @@ public final class Tests @BeforeClass public static void init() throws Exception { - System.setProperty("pms.repo.jdbc.url", "jdbc:postgresql:postgres"); - System.setProperty("pms.repo.jdbc.user", "postgres"); - System.setProperty("pms.repo.jdbc.password", "1234"); + System.setProperty("opp.repo.jdbc.url", "jdbc:postgresql:postgres"); + System.setProperty("opp.repo.jdbc.user", "postgres"); + System.setProperty("opp.repo.jdbc.password", "1234"); repo = JDBCRepository.instance(); @@ -70,24 +73,39 @@ public final class Tests Specialty.CARDIOLOGY, Instant.now()); - List<OPStaff> opStaffList = new ArrayList<>(); - opStaffList.add(testOPStaff1); - opStaffList.add(testOPStaff2); + testOPStaff3 = new OPStaff( + new Id<>("opStaff3333"), + Role.SURGEON, + Specialty.GASTROENTEROLOGY, + Instant.now()); + + List<OPStaff> opStaffList1 = new ArrayList<>(); + opStaffList1.add(testOPStaff1); + opStaffList1.add(testOPStaff2); testOperationTeam1 = new OperationTeam( new Id<>("operationTeam1111"), - opStaffList, + opStaffList1, ("Team1"), Instant.now() ); + List<OPStaff> opStaffList2 = new ArrayList<>(); + opStaffList1.add(testOPStaff3); + testOperationTeam2 = new OperationTeam( new Id<>("operationTeam2222"), - opStaffList, + opStaffList2, ("Team2"), Instant.now() ); + testRoom = new Room( + new Id<>("room1111"), + ("Room1"), + Instant.now() + ); + testOperation = new Operation( new Id<>("operation1111"), LocalDate.of(2024, 5, 24), @@ -95,19 +113,21 @@ public final class Tests LocalTime.of(14, 0, 0), testPatient.id(), testOperationTeam1.id(), + testRoom.id(), Instant.now() ); - testPreparationNote = new PreparationNote( + testPreparationNote1 = new PreparationNote( new Id<>("note1111"), - ("Test notiz!"), + ("note1"), testOperation.id(), Instant.now() ); - testRoom = new Room( - new Id<>("room1111"), - ("Test Raum"), + testPreparationNote2 = new PreparationNote( + new Id<>("note2222"), + ("note2"), + testOperation.id(), Instant.now() ); @@ -123,11 +143,13 @@ public final class Tests } -@Ignore + @Ignore @Test public void testRepoOperationSave(){ try { + repo.save(testPreparationNote1); + repo.save(testRoom); repo.save(testOperationTeam1); repo.save(testOperation); } catch (Exception e){ @@ -139,172 +161,13 @@ public final class Tests ); } -@Ignore - @Test - public void testCreatePreparationNote() throws Exception { - - PreparationNote.Create createCommand = new PreparationNote.Create( - ("Notiz erstellt mit Command"), - testOperation.id() - ); - - PreparationNote createdPreparationNote = preparationNoteService.process(createCommand); - - assertTrue( - repo.findPreparationNote(createdPreparationNote.id()).isPresent() - ); - } - - - @Ignore - @Test - public void testUpdatePreparationNote() throws Exception { - - PreparationNote.Update updateCommand = new PreparationNote.Update( - testPreparationNote.id(), - ("COmmand wurde hier geupdaet!?!?!?!?") - ); - - PreparationNote updatePreparationNote = preparationNoteService.process(updateCommand); - - Optional<PreparationNote> readPreparationNote = repo.findPreparationNote(updatePreparationNote.id()); - - if (readPreparationNote.isPresent()) { - assertNotEquals(readPreparationNote.get().note(), testPreparationNote.note()); - - assertEquals(readPreparationNote.get().operationsId(), testPreparationNote.operationsId()); - - assertEquals(readPreparationNote.get().id(), testPreparationNote.id()); - - assertNotEquals(readPreparationNote.get().lastUpdate(), testPreparationNote.lastUpdate()); - } else { - throw new Exception(); - } - } - - - @Ignore - @Test - public void testDeletePreparationNote() throws Exception { - - repo.save(testPreparationNote); - - PreparationNote.Delete deleteCommand = new PreparationNote.Delete( - testPreparationNote.id() - ); - - PreparationNote deletePreparationNote = preparationNoteService.process(deleteCommand); - - assertFalse(repo.findPreparationNote(deletePreparationNote.id()).isPresent()); - } - - @Ignore - @Test - public void testRepoPreparationNoteSave(){ - - try { - repo.save(testPreparationNote); - } catch (Exception e){ - e.printStackTrace(); - } - - assertTrue( - repo.findPreparationNote(testPreparationNote.id()).isPresent() - ); - } - - - - @Ignore - @Test - public void testRepoPreparationNoteUpdate() throws Exception { - - try { - repo.save(testPreparationNote); - } catch (Exception e){ - e.printStackTrace(); - } - - PreparationNote updatedTestPreparatioNote = new PreparationNote( - testPreparationNote.id(), - ("ES WURDE GEUPDATED!!!"), - testPreparationNote.operationsId(), - Instant.now() - ); - - try { - repo.save(updatedTestPreparatioNote); - } catch (Exception e){ - e.printStackTrace(); - } - - Optional<PreparationNote> readPreparationNote = repo.findPreparationNote(updatedTestPreparatioNote.id()); - if (readPreparationNote.isPresent()) { - assertNotEquals(readPreparationNote.get().note(), testPreparationNote.note()); - assertNotEquals(readPreparationNote.get().lastUpdate(), testPreparationNote.lastUpdate()); - } else { - throw new Exception(); - } - } - @Ignore - @Test - public void testCreateRoom() throws Exception{ - - Room.Create createCommand = new Room.Create( - "Neuen Raum erstellt mit Command" - ); - - Room createdRoom = roomService.process(createCommand); - - assertNotNull("Room should be created", createdRoom); - assertNotNull("Room ID should not be null", createdRoom.id()); - assertTrue( - repo.findRoom(createdRoom.id()).isPresent() - ); - } - @Ignore - @Test - public void testRoomUpdate() throws Exception { - // repo.save(testRoom); - Room.Update updateCommand = new Room.Update( - testRoom.id(), - ("COmmand wurde hier geupdaet!?!?!?!?") - ); - - Room updateRoom = roomService.process(updateCommand); - - Optional<Room> readRoom = repo.findRoom(updateRoom.id()); - - if (readRoom.isPresent()) { - assertNotEquals(readRoom.get().roomName(), testRoom.roomName()); - - assertEquals(readRoom.get().id(), testRoom.id()); - - assertNotEquals(readRoom.get().lastUpdate(), testRoom.lastUpdate()); - } else { - throw new Exception(); - } - } - - @Test - public void testDeleteRoom() throws Exception { - - // repo.save(testRoom); - - Room.Delete deleteCommand = new Room.Delete( - testRoom.id() - ); - - Room deleteRoom = roomService.process(deleteCommand); - - assertFalse(repo.findRoom(deleteRoom.id()).isPresent()); - } - @Ignore @Test public void testRepoOperationUpdate() throws Exception { try { + repo.save(testPreparationNote1); + repo.save(testRoom); repo.save(testOperationTeam1); repo.save(testOperation); } catch (Exception e){ @@ -318,6 +181,7 @@ public final class Tests testOperation.endTime(), testOperation.patientId(), testOperation.operationTeamId(), + testOperation.roomId(), Instant.now() ); @@ -339,26 +203,25 @@ public final class Tests @Ignore @Test public void testRepoOperationDelete() { - try { - repo.save(testOperationTeam1); - repo.save(testOperation); - } catch (Exception e){ - e.printStackTrace(); - } + try { + repo.save(testPreparationNote1); + repo.save(testRoom); + repo.save(testOperationTeam1); + repo.save(testOperation); + } catch (Exception e){ + e.printStackTrace(); + } - try { - //repo.deleteOperationTeam(testOperationTeam.id()); - repo.deleteOperation(testOperation.id()); - } catch (SQLException e) { - throw new RuntimeException(e); - } + //repo.deleteOperationTeam(testOperationTeam.id()); + repo.deleteOperation(testOperation.id()); - assertFalse(repo.findOperation(testOperation.id()).isPresent()); + assertFalse(repo.findOperation(testOperation.id()).isPresent()); } -@Ignore + @Test public void testCreateOperation() throws Exception { + repo.save(testRoom); repo.save(testOperationTeam1); Operation.Create createCommand = new Operation.Create( @@ -366,7 +229,8 @@ public final class Tests LocalTime.of(10, 30, 0), LocalTime.of(11, 0, 0), testPatient.id(), - testOperationTeam1.id() + testOperationTeam1.id(), + testRoom.id() ); Operation createdOperation = operationService.process(createCommand); @@ -375,10 +239,11 @@ public final class Tests repo.findOperation(createdOperation.id()).isPresent() ); } -@Ignore + @Test public void testUpdateOperation() throws Exception { + repo.save(testRoom); repo.save(testOperationTeam1); repo.save(testOperation); @@ -388,6 +253,7 @@ public final class Tests Optional.of(LocalTime.of(23,59,0)), Optional.empty(), Optional.empty(), + Optional.empty(), Optional.empty() ); @@ -408,12 +274,14 @@ public final class Tests } } -@Ignore @Test public void testDeleteOperation() throws Exception { + repo.save(testRoom); repo.save(testOperationTeam1); repo.save(testOperation); + repo.save(testPreparationNote1); + repo.save(testPreparationNote2); Operation.Delete deleteCommand = new Operation.Delete( testOperation.id() @@ -421,12 +289,13 @@ public final class Tests Operation deleteOperation = operationService.process(deleteCommand); - assertFalse(repo.findOperation(deleteOperation.id()).isPresent()); + assertFalse(repo.findOperation(deleteOperation.id()).isPresent()); } - @Ignore @Test public void testGetOperation() throws Exception { + + repo.save(testRoom); repo.save(testOperationTeam1); repo.save(testOperation); @@ -439,15 +308,17 @@ public final class Tests assertEquals(testOperation.endTime(), readOperation.get().endTime()); assertEquals(testOperation.patientId(), readOperation.get().patientId()); assertEquals(testOperation.operationTeamId(), readOperation.get().operationTeamId()); + assertEquals(testOperation.roomId(), readOperation.get().roomId()); + } else { throw new Exception(); } } - @Ignore @Test public void testGetOperations() throws Exception { + repo.save(testRoom); repo.save(testOperationTeam1); repo.save(testOperation); @@ -458,11 +329,11 @@ public final class Tests List<Operation> operations = operationService.getOperations(filter); for (Operation operation : operations) { - System.out.println(operation); assertEquals(operation.date(), testDate); } } -@Ignore + + @Ignore @Test public void testRepoOPStaffSave(){ @@ -476,7 +347,8 @@ public final class Tests repo.findOPStaff(testOPStaff1.id()).isPresent() ); } -@Ignore + + @Ignore @Test public void testRepoOPStaffUpdate() throws Exception { @@ -517,16 +389,11 @@ public final class Tests e.printStackTrace(); } - try { - repo.deleteOPStaff(testOPStaff1.id()); - } catch (SQLException e) { - throw new RuntimeException(e); - } + repo.deleteOPStaff(testOPStaff1.id()); assertFalse(repo.findOPStaff(testOPStaff1.id()).isPresent()); } - @Ignore @Test public void testCreateOPStaff() throws Exception { @@ -542,7 +409,6 @@ public final class Tests ); } - @Ignore @Test public void testUpdateOPStaff() throws Exception { @@ -569,7 +435,6 @@ public final class Tests } } -@Ignore @Test public void testDeleteOPStaff() throws Exception { @@ -584,7 +449,6 @@ public final class Tests assertFalse(repo.findOPStaff(deletedOPStaff.id()).isPresent()); } - @Ignore @Test public void testGetOPStaff() throws Exception { repo.save(testOPStaff1); @@ -600,23 +464,126 @@ public final class Tests } } + + @Ignore + @Test + public void testRepoOperationTeamUpdate() throws Exception { + + try { + repo.save(testOPStaff1); + repo.save(testOPStaff2); + repo.save(testOperationTeam1); + repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff1.id()); + repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff2.id()); + } catch (Exception e){ + e.printStackTrace(); + } + + OperationTeam updatedTestOperationTeam = new OperationTeam( + testOperationTeam1.id(), + testOperationTeam1.opStaffs(), + "team22", + Instant.now() + ); + + try { + repo.save(updatedTestOperationTeam); + } catch (Exception e){ + e.printStackTrace(); + } + + Optional<OperationTeam> readOperationTeam = repo.findOperationTeam(updatedTestOperationTeam.id()); + if (readOperationTeam.isPresent()) { + assertNotEquals(readOperationTeam.get().teamName(), testOperationTeam1.teamName()); + assertNotEquals(readOperationTeam.get().lastUpdate(), testOperationTeam1.lastUpdate()); + } else { + throw new Exception(); + } + } + @Ignore @Test - public void testGetOPStaffs() throws Exception { + public void testRepoOperationTeamDelete() { + try { + repo.save(testOPStaff1); + repo.save(testOPStaff2); + repo.save(testOperationTeam1); + repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff1.id()); + repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff2.id()); + } catch (Exception e){ + e.printStackTrace(); + } + + repo.deleteOperationTeam(testOperationTeam1.id()); + + assertFalse(repo.findOperationTeam(testOperationTeam1.id()).isPresent()); + } + + @Test + public void testCreateOperationTeam() throws Exception { + repo.save(testRoom); + repo.save(testOperation); repo.save(testOPStaff1); + repo.save(testOPStaff2); + OperationTeam.Create createCommand = new OperationTeam.Create( + "Team33", + testOperationTeam1.opStaffs() + ); + + OperationTeam createdOperationTeam = operationTeamService.process(createCommand); + + assertTrue( + repo.findOperationTeam(createdOperationTeam.id()).isPresent() + ); + for(OPStaff opStaff : createdOperationTeam.opStaffs()) { + assertTrue(repo.findOperationTeamOPStaff(createdOperationTeam.id(), opStaff.id())); + } + } + + @Test + public void testDeleteOperationTeam() throws Exception { + + repo.save(testOPStaff3); + repo.save(testOperationTeam2); + + repo.assignOPStaffToOperationTeam(testOperationTeam2.id(), testOPStaff3.id()); + + OperationTeam.Delete deleteCommand = new OperationTeam.Delete( + testOperationTeam2.id() + ); + + OperationTeam deletedOperationTeam = operationTeamService.process(deleteCommand); + + assertFalse(repo.findOperationTeam(deletedOperationTeam.id()).isPresent()); + } + + @Ignore + @Test + public void testGetOperationTeam() throws Exception { + repo.save(testOPStaff1); repo.save(testOPStaff2); + repo.save(testOperationTeam1); - List<OPStaff> opStaffs = opStaffService.getAllOPStaffs(); + repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff1.id()); + repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff2.id()); - List<OPStaff> filteredList = opStaffs.stream() - .filter(opStaff -> Objects.equals(opStaff.id().value(), testOPStaff1.id().value()) || Objects.equals(opStaff.id().value(), testOPStaff2.id().value())) - .toList(); + Optional<OperationTeam> readOperationTeam = operationTeamService.getOperationTeam(testOperationTeam1.id()); - assertEquals(2, filteredList.size()); + if (readOperationTeam.isPresent()) { + assertEquals(testOperationTeam1.id(), readOperationTeam.get().id()); + assertEquals(testOperationTeam1.teamName(), readOperationTeam.get().teamName()); + + for(int i = 0; i < readOperationTeam.get().opStaffs().size(); i++) { + assertEquals(testOperationTeam1.opStaffs().get(i).id(), readOperationTeam.get().opStaffs().get(i).id()); + } + } else { + throw new Exception(); + } } + @Ignore @Test public void testRepoOperationTeamAssignStaff(){ @@ -640,112 +607,152 @@ public final class Tests } } + + @Ignore @Test - public void testCreateOperationTeam() throws Exception { + public void testCreatePreparationNote() throws Exception { - OperationTeam.Create createCommand = new OperationTeam.Create( - "Team33", - testOperationTeam1.opStaffs() + PreparationNote.Create createCommand = new PreparationNote.Create( + ("Notiz erstellt mit Command"), + testOperation.id() ); - OperationTeam createdOperationTeam = operationTeamService.process(createCommand); + PreparationNote createdPreparationNote = preparationNoteService.process(createCommand); assertTrue( - repo.findOperationTeam(createdOperationTeam.id()).isPresent() + repo.findPreparationNote(createdPreparationNote.id()).isPresent() ); - for(OPStaff opStaff : createdOperationTeam.opStaffs()) { - assertTrue(repo.findOperationTeamOPStaff(createdOperationTeam.id(), opStaff.id())); - } } + @Ignore @Test - public void testRepoOperationTeamUpdate() throws Exception { + public void testUpdatePreparationNote() throws Exception { - try { - repo.save(testOPStaff1); - repo.save(testOPStaff2); - repo.save(testOperationTeam1); - repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff1.id()); - repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff2.id()); - } catch (Exception e){ - e.printStackTrace(); + PreparationNote.Update updateCommand = new PreparationNote.Update( + testPreparationNote1.id(), + ("COmmand wurde hier geupdaet!?!?!?!?") + ); + + PreparationNote updatePreparationNote = preparationNoteService.process(updateCommand); + + Optional<PreparationNote> readPreparationNote = repo.findPreparationNote(updatePreparationNote.id()); + + if (readPreparationNote.isPresent()) { + assertNotEquals(readPreparationNote.get().note(), testPreparationNote1.note()); + + assertEquals(readPreparationNote.get().operationsId(), testPreparationNote1.operationsId()); + + assertEquals(readPreparationNote.get().id(), testPreparationNote1.id()); + + assertNotEquals(readPreparationNote.get().lastUpdate(), testPreparationNote1.lastUpdate()); + } else { + throw new Exception(); } + } - OperationTeam updatedTestOperationTeam = new OperationTeam( - testOperationTeam1.id(), - testOperationTeam1.opStaffs(), - "team22", - Instant.now() + + @Ignore + @Test + public void testDeletePreparationNote() throws Exception { + + repo.save(testPreparationNote1); + + PreparationNote.Delete deleteCommand = new PreparationNote.Delete( + testPreparationNote1.id() ); + PreparationNote deletePreparationNote = preparationNoteService.process(deleteCommand); + + assertFalse(repo.findPreparationNote(deletePreparationNote.id()).isPresent()); + } + + @Ignore + @Test + public void testRepoPreparationNoteSave(){ + try { - repo.save(updatedTestOperationTeam); + repo.save(testPreparationNote1); } catch (Exception e){ e.printStackTrace(); } - Optional<OperationTeam> readOperationTeam = repo.findOperationTeam(updatedTestOperationTeam.id()); - if (readOperationTeam.isPresent()) { - assertNotEquals(readOperationTeam.get().teamName(), testOperationTeam1.teamName()); - assertNotEquals(readOperationTeam.get().lastUpdate(), testOperationTeam1.lastUpdate()); - } else { - throw new Exception(); - } + assertTrue( + repo.findPreparationNote(testPreparationNote1.id()).isPresent() + ); } -@Ignore + + + + @Ignore @Test - public void testRepoOperationTeamDelete() { + public void testRepoPreparationNoteUpdate() throws Exception { + try { - repo.save(testOPStaff1); - repo.save(testOPStaff2); - repo.save(testOperationTeam1); - repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff1.id()); - repo.assignOPStaffToOperationTeam(testOperationTeam1.id(), testOPStaff2.id()); + repo.save(testPreparationNote1); } catch (Exception e){ e.printStackTrace(); } + PreparationNote updatedTestPreparatioNote = new PreparationNote( + testPreparationNote1.id(), + ("ES WURDE GEUPDATED!!!"), + testPreparationNote1.operationsId(), + Instant.now() + ); + try { - repo.deleteOperationTeam(testOperationTeam1.id()); - } catch (SQLException e) { - throw new RuntimeException(e); + repo.save(updatedTestPreparatioNote); + } catch (Exception e){ + e.printStackTrace(); } - assertFalse(repo.findOperationTeam(testOperationTeam1.id()).isPresent()); + Optional<PreparationNote> readPreparationNote = repo.findPreparationNote(updatedTestPreparatioNote.id()); + if (readPreparationNote.isPresent()) { + assertNotEquals(readPreparationNote.get().note(), testPreparationNote1.note()); + assertNotEquals(readPreparationNote.get().lastUpdate(), testPreparationNote1.lastUpdate()); + } else { + throw new Exception(); + } } - @Ignore @Test - public void testDeleteOperationTeam() throws Exception { - - repo.save(testOperationTeam1); + public void testCreateRoom() throws Exception{ - OperationTeam.Delete deleteCommand = new OperationTeam.Delete( - testOperationTeam1.id() + Room.Create createCommand = new Room.Create( + "Neuen Raum erstellt mit Command" ); - OperationTeam deletedOperationTeam = operationTeamService.process(deleteCommand); + Room createdRoom = roomService.process(createCommand); - assertFalse(repo.findOperationTeam(deletedOperationTeam.id()).isPresent()); + assertNotNull("Room should be created", createdRoom); + assertNotNull("Room ID should not be null", createdRoom.id()); + assertTrue( + repo.findRoom(createdRoom.id()).isPresent() + ); } @Ignore @Test - public void testGetOperationTeam() throws Exception { - repo.save(testOperationTeam1); + public void testRoomUpdate() throws Exception { + // repo.save(testRoom); + Room.Update updateCommand = new Room.Update( + testRoom.id(), + ("COmmand wurde hier geupdaet!?!?!?!?") + ); - Optional<OperationTeam> readOperationTeam = operationTeamService.getOperationTeam(testOperationTeam1.id()); + Room updateRoom = roomService.process(updateCommand); - if (readOperationTeam.isPresent()) { - assertEquals(testOperationTeam1.id(), readOperationTeam.get().id()); - assertEquals(testOperationTeam1.teamName(), readOperationTeam.get().teamName()); + Optional<Room> readRoom = repo.findRoom(updateRoom.id()); - for(int i = 0; i < readOperationTeam.get().opStaffs().size(); i++) { - assertEquals(testOperationTeam1.opStaffs().get(i).id(), readOperationTeam.get().opStaffs().get(i).id()); - } + if (readRoom.isPresent()) { + assertNotEquals(readRoom.get().roomName(), testRoom.roomName()); + + assertEquals(readRoom.get().id(), testRoom.id()); + + assertNotEquals(readRoom.get().lastUpdate(), testRoom.lastUpdate()); } else { throw new Exception(); } @@ -753,22 +760,20 @@ public final class Tests @Ignore @Test - public void testGetOperationTeams() throws Exception { + public void testDeleteRoom() throws Exception { - repo.save(testOperationTeam1); + // repo.save(testRoom); - repo.save(testOperationTeam2); - - List<OperationTeam> operationTeams = operationTeamService.getAllOperationTeams(); + Room.Delete deleteCommand = new Room.Delete( + testRoom.id() + ); - List<OperationTeam> filteredList = operationTeams.stream() - .filter(operationTeam -> Objects.equals(operationTeam.id().value(), testOperationTeam1.id().value()) || - Objects.equals(operationTeam.id().value(), testOperationTeam2.id().value())) - .toList(); + Room deleteRoom = roomService.process(deleteCommand); - assertEquals(2, filteredList.size()); + assertFalse(repo.findRoom(deleteRoom.id()).isPresent()); } + @Ignore @Test public void testRepoAssignStaffToOperationTeam() throws Exception { @@ -781,7 +786,7 @@ public final class Tests assertTrue(result); } -@Ignore + @Ignore @Test public void testAssignStaffToOperationTeam() throws Exception { @@ -803,7 +808,7 @@ public final class Tests -@Ignore + @Ignore @Test public void testRemoveOPStaffsInOperationTeams() throws Exception{