Skip to content
Snippets Groups Projects
Commit b585b422 authored by Andre Hartig's avatar Andre Hartig
Browse files

implemented connection between opStaff and operationTeam without using teamMember class

parent 2060f94d
No related branches found
No related tags found
No related merge requests found
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 {}
}
......@@ -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);
}
}
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);
......
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
......
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(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment