diff --git a/opp/core/src/main/java/OperationTeam.java b/opp/core/src/main/java/OperationTeam.java index a42f5352f46cb9162750793edfa0c2b72870e098..4f7cf921e71ff5f57b039ed195338f6eff8a27b2 100644 --- a/opp/core/src/main/java/OperationTeam.java +++ b/opp/core/src/main/java/OperationTeam.java @@ -1,19 +1,37 @@ import java.time.Instant; +import java.util.List; public record OperationTeam( Id<OperationTeam> id, + List<OPStaff> opStaffs, String teamName, Instant lastUpdate ) { - public static sealed interface Command permits OperationTeam.CreateTeam, OperationTeam.DeleteTeam {} + public static sealed interface Command permits + CreateTeam, + DeleteTeam, + AssignStaff, + RemoveStaff + {} public static record CreateTeam( - String teamName - ) implements OperationTeam.Command {} + String teamName, + List<OPStaff> opStaffs + ) implements Command {} public static record DeleteTeam( Id<OperationTeam> operationTeamId - ) implements OperationTeam.Command {} + ) implements Command {} + + public static record AssignStaff( + Id<OperationTeam> operationTeamId, + OPStaff opStaff + ) implements Command {} + + public static record RemoveStaff( + Id<OperationTeam> operationTeamId, + OPStaff opStaff + ) implements Command {} } diff --git a/opp/core/src/main/java/OperationTeamImpl.java b/opp/core/src/main/java/OperationTeamImpl.java index 061212e28cabd4eac58c4c2a6df202aa16f34717..4905ad51f6bfe1c77515aa0209d828a50cffe1de 100644 --- a/opp/core/src/main/java/OperationTeamImpl.java +++ b/opp/core/src/main/java/OperationTeamImpl.java @@ -17,16 +17,12 @@ public class OperationTeamImpl implements OperationTeamService{ return switch(cmd){ case OperationTeam.CreateTeam cr -> create(cr); case OperationTeam.DeleteTeam del -> delete(del); + case OperationTeam.AssignStaff as -> assignStaff(as); + case OperationTeam.RemoveStaff rs -> removeStaff(rs); }; } - @Override - public Optional<OperationTeam> getOperationTeam(Id<OperationTeam> operationTeamId) throws SQLException { - return repo.findOperationTeam(operationTeamId); - } - private OperationTeam delete(OperationTeam.DeleteTeam del) throws SQLException { - return repo.deleteOperationTeam(del.operationTeamId()); } @@ -34,12 +30,26 @@ public class OperationTeamImpl implements OperationTeamService{ OperationTeam operationTeam = new OperationTeam( repo.operationTeamId(), + cr.opStaffs(), cr.teamName(), Instant.now() ); return operationTeam; } + private OperationTeam assignStaff(OperationTeam.AssignStaff as) { + + if (repo.assignOPStaffToOperationTeam(as.operationTeamId(), as.opStaff().id())) { + return repo.findOperationTeam(as.operationTeamId()).get(); + } else { + return null; + } + } + + private OperationTeam removeStaff(OperationTeam.RemoveStaff rs) { + return null; + } + @Override public List<OperationTeam> getOperationTeams() throws SQLException { @@ -47,4 +57,9 @@ public class OperationTeamImpl implements OperationTeamService{ } + @Override + public Optional<OperationTeam> getOperationTeam(Id<OperationTeam> operationTeamId) throws SQLException { + return repo.findOperationTeam(operationTeamId); + } + } diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 6b2355c497cc9a6637c61506dd79155c16228b6b..97e8e39cb77d40e7af3c5a09a47affcce2d462da 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -1,5 +1,6 @@ import java.sql.SQLException; import java.util.List; +import java.util.Map; import java.util.Optional; public interface Repository { @@ -89,6 +90,12 @@ public interface Repository { OperationTeam deleteOperationTeam(Id<OperationTeam> id) throws SQLException; + Id<String> opStaffsInOperationTeamsId(); + + Optional<Map<OperationTeam, OPStaff>> findOPStaffsInOperationTeam(Id<String> id); + Boolean assignOPStaffToOperationTeam(Id<OperationTeam> operationTeamId, 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 a54de5c0b2097d1240da29a3ce09203a23fe507d..acacacb7fd50c0f37da66516a35141562a2a3f73 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -1,10 +1,9 @@ import java.sql.*; +import java.sql.Date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; +import java.util.*; import static java.util.UUID.randomUUID; @@ -89,12 +88,12 @@ class JDBCRepository implements Repository */ 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), - PRIMARY KEY (operationTeamId, opStaffId) + FOREIGN KEY (opStaffId) REFERENCES opStaffs(id) ); """; @@ -154,12 +153,13 @@ class JDBCRepository implements Repository private static String insertSQL(Operation operation){ return "INSERT INTO operations(" + - "id,date,startTime,endTime,operationteamid,lastUpdate" + + "id,date,startTime,endTime,patientId,operationTeamId,lastUpdate" + ") VALUES (" + sqlValue(operation.id().value()) + "," + sqlValue(operation.date()) + "," + sqlValue(operation.startTime()) + "," + sqlValue(operation.endTime()) + "," + + sqlValue(operation.patientId()) + "," + sqlValue(operation.operationTeamId()) + "," + sqlValue(operation.lastUpdate()) + ");"; @@ -176,6 +176,7 @@ class JDBCRepository implements Repository "date = " + sqlValue(operation.date()) + "," + "startTime = " + sqlValue(operation.startTime()) + "," + "endTime = " + sqlValue(operation.endTime()) + "," + + "patientId = " + sqlValue(operation.patientId()) + "," + "operationTeamId = " + sqlValue(operation.operationTeamId()) + "," + "lastUpdate = " + sqlValue(operation.lastUpdate()) + " " + "WHERE id = " + sqlValue(operation.id().value()) + ";"; @@ -241,6 +242,18 @@ class JDBCRepository implements Repository "WHERE id = " + sqlValue(operationTeam.id().value()) + ";"; } + private String insertSQL(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) { + return + "INSERT INTO opStaffsInOperationTeams(" + + "id,operationTeamId,opStaffId,lastUpdate" + + ") VALUES (" + + sqlValue(opStaffsInOperationTeamsId()) + "," + + sqlValue(operationTeamId.value()) + "," + + sqlValue(opStaffId.value()) + "," + + sqlValue(Instant.now()) + + ");"; + } + /** * Creates the insert statement of the given teamMember for SQL. * @param teamMember the operationTeam to turn into an insert SQL statement @@ -299,6 +312,7 @@ class JDBCRepository implements Repository return new OperationTeam( new Id<>(rs.getString("id")), + new ArrayList<>(), rs.getString("teamname"), rs.getTimestamp("lastupdate").toInstant() ); @@ -572,15 +586,32 @@ class JDBCRepository implements Repository @Override public Optional<OperationTeam> findOperationTeam(Id<OperationTeam> id){ try ( - var result = + var resultOPStaffs = + 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 = " + quoted(id.value()) + ";"); + + var resultOperationTeam = conn.createStatement() .executeQuery("SELECT * FROM operationTeams WHERE id = " + sqlValue(id.value()) + ";") ){ - return - result.next() ? - Optional.of(readOperationTeamFromRow(result)) : - Optional.empty(); + 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); + + } else { + return Optional.empty(); + } } catch (SQLException e){ throw new RuntimeException(e); } @@ -628,6 +659,68 @@ 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); + + return Optional.of(Map.of(operationTeam, opStaff)); + } else { + return Optional.empty(); + } + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + @Override + public Boolean assignOPStaffToOperationTeam(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId) { + try ( + var stmt = conn.createStatement() + ){ + var sql = + findOperationTeam(operationTeamId).isPresent() && findOPStaff(opStaffId).isPresent() ? + insertSQL(operationTeamId, opStaffId) : + null; + + if (sql != null) { + stmt.executeUpdate(sql); + return true; + } else { + return false; + } + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } /** * Returns a new generated ID in the teammember-SQLTable diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index 591744285499c5d9d89cd38e8885c7723187ec28..8d9464a4aef0a6066e42e7352a2fa57851cc6f3c 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -1,10 +1,12 @@ import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Test; import java.sql.SQLException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -21,7 +23,7 @@ public final class Tests private static Operation testOperation = null; - private static OperationService opService; + private static OperationService operationService; private static OPStaff testOPStaff = null; @@ -45,36 +47,41 @@ public final class Tests repo = JDBCRepository.instance(); testPatient = new Patient( - new Id<>("9999") + new Id<>("patient1111") ); + testOPStaff = new OPStaff( + new Id<>("opStaff1111"), + Role.ASSISTANT, + Specialty.urology, + Instant.now() + ); + + List<OPStaff> opStaffList = new ArrayList<>(); + opStaffList.add(testOPStaff); + testOperationTeam = new OperationTeam( - new Id<>("31"), - ("AmCoolsten"), + new Id<>("operationTeam1111"), + opStaffList, + ("Cool"), 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<>("operation1111"), + LocalDate.of(2024, 5, 24), + LocalTime.of(13, 35, 0), + LocalTime.of(14, 0, 0), testPatient.id(), testOperationTeam.id(), - Instant.now()); - - opService = new OperationServiceImpl(repo); + Instant.now() + ); - testOPStaff = new OPStaff( - new Id<>("4444"), - Role.ASSISTANT, - Specialty.urology, - Instant.now()); + operationService = new OperationServiceImpl(repo); opStaffService = new OPStaffServiceImpl(repo); - operationTeamService = new OperationTeamImpl(repo) { - }; + operationTeamService = new OperationTeamImpl(repo); testTeamMember = new TeamMember( testOperationTeam.id(), @@ -86,7 +93,7 @@ public final class Tests } - //@Test + @Test public void testRepoOperationSave(){ try { @@ -101,8 +108,8 @@ public final class Tests ); } - //@Test - public void testRepoOperationUpdate() { + @Test + public void testRepoOperationUpdate() throws Exception { try { repo.save(testOperationTeam); @@ -113,7 +120,7 @@ public final class Tests Operation updatedTestOperation = new Operation( testOperation.id(), - LocalDate.of(1999, 01, 01), + LocalDate.of(1999, 1, 1), testOperation.startTime(), testOperation.endTime(), testOperation.patientId(), @@ -127,12 +134,16 @@ public final class Tests e.printStackTrace(); } - assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().date(), testOperation.date()); - - assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().lastUpdate(), testOperation.lastUpdate()); + Optional<Operation> readOperation = repo.findOperation(updatedTestOperation.id()); + if (readOperation.isPresent()) { + assertNotEquals(readOperation.get().date(), testOperation.date()); + assertNotEquals(readOperation.get().lastUpdate(), testOperation.lastUpdate()); + } else { + throw new Exception(); + } } - //@Test + @Test public void testRepoOperationDelete() { try { repo.save(testOperationTeam); @@ -147,89 +158,97 @@ public final class Tests } catch (SQLException e) { throw new RuntimeException(e); } + + assertFalse(repo.findOperation(testOperation.id()).isPresent()); } - //@Test + @Test public void testCreateOperation() throws Exception { Operation.Create createCommand = new Operation.Create( - LocalDate.of(2025, 05, 11), - LocalTime.of(10, 30, 00), - LocalTime.of(11, 00, 00), - new Id<Patient>("8888"), - new Id<OperationTeam>("31313131") + LocalDate.of(2025, 5, 11), + LocalTime.of(10, 30, 0), + LocalTime.of(11, 0, 0), + testPatient.id(), + testOperationTeam.id() ); - Operation createOperation = opService.process(createCommand); + Operation createdOperation = operationService.process(createCommand); assertTrue( - repo.findOperation(createOperation.id()).isPresent() + repo.findOperation(createdOperation.id()).isPresent() ); } - //@Test + @Test public void testUpdateOperation() throws Exception { repo.save(testOperation); Operation.Update updateCommand = new Operation.Update( testOperation.id(), - Optional.of(LocalDate.of(1999, 01, 01)), Optional.empty(), + Optional.of(LocalTime.of(23,59,0)), Optional.empty(), Optional.empty(), Optional.empty() ); - opService.getOperation(testOperation.id()); + Operation updateOperation = operationService.process(updateCommand); - Operation updateOperation = opService.process(updateCommand); + Optional<Operation> readOperation = repo.findOperation(updateOperation.id()); - assertNotEquals( - repo.findOperation(updateOperation.id()).get().date(), testOperation.date() - ); + if (readOperation.isPresent()) { + assertNotEquals(readOperation.get().startTime(), testOperation.startTime()); - assertEquals( - repo.findOperation(updateOperation.id()).get().startTime(), testOperation.startTime() - ); + assertEquals(readOperation.get().date(), testOperation.date()); - assertEquals( - repo.findOperation(updateOperation.id()).get().endTime(), testOperation.endTime() - ); + assertEquals(readOperation.get().endTime(), testOperation.endTime()); - assertNotEquals( - repo.findOperation(updateOperation.id()).get().lastUpdate(), testOperation.lastUpdate() - ); + assertNotEquals(readOperation.get().lastUpdate(), testOperation.lastUpdate()); + } else { + throw new Exception(); + } } -//@Test + @Test public void testDeleteOperation() throws Exception { - //repo.save(testOperation); - - var id = new Id<Operation>("1111"); + repo.save(testOperation); Operation.Delete deleteCommand = new Operation.Delete( - //testOperation.id() - id + testOperation.id() ); - Operation deleteOperation = opService.process(deleteCommand); + Operation deleteOperation = operationService.process(deleteCommand); - assertFalse( - repo.findOperation(deleteOperation.id()).isPresent() - ); + assertFalse(repo.findOperation(deleteOperation.id()).isPresent()); } - //ade321ba-53f7-4f72-9748-bf2bbddfd98c + @Ignore + @Test + public void testGetOperation() throws Exception { + repo.save(testOperation); + + Optional<Operation> readOperation = operationService.getOperation(testOperation.id()); + + if (readOperation.isPresent()) { + assertEquals(testOperation, readOperation.get()); + } else { + throw new Exception(); + } + } + + @Test + public void testGetOperations() throws Exception { + + repo.save(testOperation); + + LocalDate testDate = LocalDate.of(2024, 5, 24); - //@Test - public void testGetOperations() { - LocalDate testDate = LocalDate.of(2025, 05, 11); Operation.Filter filter = new Operation.Filter(Optional.of(testDate)); -// Operation.Filter filter = Operation.Filter.NONE; - List<Operation> operations = opService.getOperations(filter); + List<Operation> operations = operationService.getOperations(filter); for (Operation operation : operations) { System.out.println(operation); @@ -237,6 +256,19 @@ public final class Tests } } + @Test + public void testAssignStaffToOperationTeam() throws Exception { + + repo.save(testOperationTeam); + repo.save(testOPStaff); + + Boolean result = repo.assignOPStaffToOperationTeam(testOperationTeam.id(), testOPStaff.id()); + + assertTrue(result); + } + + + //@Test public void testCreateOPStaff_1() { try { @@ -333,7 +365,7 @@ public final class Tests OperationTeam deleteOperationTeam = operationTeamService.process(deleteCommand); } - @Test + //@Test public void testInsertTeamMember1() throws Exception{ TeamMember.AssignStaff assignCommand = new TeamMember.AssignStaff(