Skip to content
Snippets Groups Projects
Commit c3303d15 authored by Alexander Görlitz's avatar Alexander Görlitz
Browse files

Implemented the methods to create a new OPStaff with the enum Specialty

parent d8abd9bc
No related branches found
No related tags found
No related merge requests found
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
public record OPStaff( public record OPStaff(
Id<OPStaff> opStaffId, Id<OPStaff> id,
Role role, Role role,
String specialty, Specialty specialty,
Instant lastUpdate Instant lastUpdate
) { ) {
/**
* OPStaff commands
*/
public static sealed interface Command permits OPStaff.CreateOPStaff { } //, OPStaff.UpdateOPStaff, OPStaff.DeleteOPStaff
/**
* Create command to create a new Operation
* @param role if the Personal is a surgeon or an assistant
* @param specialty the hospital specialties
*/
public static record CreateOPStaff(
Role role,
Specialty specialty
) implements Command {}
} }
import java.util.List;
import java.util.Optional;
/**
* Available Services for OPStaff
*/
public interface OPStaffService {
/**
* Processes the possible commands for an OPStaff
* @param cmd the command to process
* @return the processed OPStaff
*/
public OPStaff process(OPStaff.Command cmd) throws Exception;
/**
* Gets the Operation by ID
* @param id the ID of the OPStaff
* @return the OPStaff
*/
public Optional<OPStaff> getOPStaff(Id<Operation> id);
}
\ No newline at end of file
import java.time.Instant;
import java.util.Optional;
public class OPStaffServiceImpl implements OPStaffService{
private final Repository repo;
public OPStaffServiceImpl(Repository repo) {
this.repo = repo;
}
@Override
public OPStaff process(OPStaff.Command cmd) throws Exception {
return switch (cmd){
case OPStaff.CreateOPStaff cr -> createOPStaffimpl(cr);
//case OPStaff.Update up -> update(up);
//case OPStaff.Delete del -> delete(del);
};
}
public OPStaff createOPStaffimpl(OPStaff.CreateOPStaff cr) throws Exception {
OPStaff opStaff =
new OPStaff(
repo.opStaffId(),
cr.role(),
cr.specialty(),
Instant.now());
repo.saveOPStaff(opStaff);
return opStaff;
}
@Override
public Optional<OPStaff> getOPStaff(Id<Operation> id) {
return Optional.empty();
}
}
...@@ -46,4 +46,18 @@ public interface Repository { ...@@ -46,4 +46,18 @@ public interface Repository {
* @param id the id of the Operation to delete * @param id the id of the Operation to delete
*/ */
void deleteOperation(Id<Operation> id) throws SQLException; void deleteOperation(Id<Operation> id) throws SQLException;
/**
* Returns a new generated ID in the opstaff-SQLTable
* If the generated ID is already used, generate another one.
* @return OPStaff ID
*/
Id<OPStaff> opStaffId();
/**
* Saves the OPStaff into the database.
* @param
* @throws Exception while saving
*/
void saveOPStaff(OPStaff opStaff) throws Exception;
} }
public enum Specialty {
Orthopedics,
cardiology,
urology,
Gastroenterology
}
...@@ -50,6 +50,7 @@ class JDBCRepository implements Repository ...@@ -50,6 +50,7 @@ class JDBCRepository implements Repository
try (var stmt = conn.createStatement()){ try (var stmt = conn.createStatement()){
stmt.execute(CREATE_OPERATION_TABLE); stmt.execute(CREATE_OPERATION_TABLE);
stmt.execute(CREATE_OPSTAFF_TABLE);
} catch (SQLException e){ } catch (SQLException e){
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -391,4 +392,71 @@ class JDBCRepository implements Repository ...@@ -391,4 +392,71 @@ class JDBCRepository implements Repository
.executeUpdate("DELETE FROM operations WHERE id = " + quoted(id.value()) + ";"); .executeUpdate("DELETE FROM operations WHERE id = " + quoted(id.value()) + ";");
} }
} }
/**
* The structure of the operations table.
*/
private static final String CREATE_OPSTAFF_TABLE = """
CREATE TABLE IF NOT EXISTS opstaff(
id VARCHAR(50) PRIMARY KEY,
role VARCHAR(50) NOT NULL,
specialty VARCHAR(50) NOT NULL,
lastUpdate TIMESTAMP NOT NULL
);
""";
/**
* Returns a new generated ID in the opstaff-SQLTable
* If the generated ID is already used, generate another one.
* @return OPStaff ID
*/
@Override
public Id<OPStaff> opStaffId() {
var id = new Id<OPStaff>(randomUUID().toString());
return id; //TODO //findOperation(id).isEmpty() ? id : operationId();
}
/**
* 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 saveOPStaff(OPStaff opStaff) {
try (
var stmt = conn.createStatement()
){
//TODO
/*var sql =
findOperation(operation.id()).isPresent() ?
updateSQL(operation) :
insertSQL(operation);*/
var sql = insertOPStaffSQL(opStaff);
stmt.executeUpdate(sql);
} catch (SQLException e){
throw new RuntimeException(e);
}
}
private static String insertOPStaffSQL(OPStaff opStaff){
return
"INSERT INTO opstaff(" +
"id,role,specialty,lastUpdate" +
") VALUES (" +
sqlValue(opStaff.id().value()) + "," +
sqlValue(opStaff.role()) + "," +
sqlValue(opStaff.specialty()) + "," +
sqlValue(opStaff.lastUpdate()) +
");";
}
} }
...@@ -21,6 +21,10 @@ public final class Tests ...@@ -21,6 +21,10 @@ public final class Tests
private static OperationService opService; private static OperationService opService;
private static OPStaff testOPStaff = null;
private static OPStaffService opStaffService;
@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init() throws Exception {
...@@ -38,9 +42,18 @@ public final class Tests ...@@ -38,9 +42,18 @@ public final class Tests
Instant.now()); Instant.now());
opService = new OperationServiceImpl(repo); opService = new OperationServiceImpl(repo);
testOPStaff = new OPStaff(
new Id<>("3333"),
Role.SURGEON,
Specialty.urology,
Instant.now());
opStaffService = new OPStaffServiceImpl(repo);
} }
@Test //@Test
public void testRepoOperationSave(){ public void testRepoOperationSave(){
try { try {
...@@ -54,7 +67,7 @@ public final class Tests ...@@ -54,7 +67,7 @@ public final class Tests
); );
} }
@Test //@Test
public void testRepoOperationUpdate() { public void testRepoOperationUpdate() {
try { try {
...@@ -82,7 +95,7 @@ public final class Tests ...@@ -82,7 +95,7 @@ public final class Tests
assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().lastUpdate(), testOperation.lastUpdate()); assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().lastUpdate(), testOperation.lastUpdate());
} }
@Test //@Test
public void testRepoOperationDelete() { public void testRepoOperationDelete() {
try { try {
repo.save(testOperation); repo.save(testOperation);
...@@ -97,7 +110,7 @@ public final class Tests ...@@ -97,7 +110,7 @@ public final class Tests
} }
} }
@Test //@Test
public void testCreateOperation() throws Exception { public void testCreateOperation() throws Exception {
Operation.Create createCommand = new Operation.Create( Operation.Create createCommand = new Operation.Create(
...@@ -113,7 +126,7 @@ public final class Tests ...@@ -113,7 +126,7 @@ public final class Tests
); );
} }
@Test //@Test
public void testUpdateOperation() throws Exception { public void testUpdateOperation() throws Exception {
repo.save(testOperation); repo.save(testOperation);
...@@ -146,13 +159,16 @@ public final class Tests ...@@ -146,13 +159,16 @@ public final class Tests
); );
} }
@Test
public void testDeleteOperation() throws Exception { public void testDeleteOperation() throws Exception {
repo.save(testOperation); //repo.save(testOperation);
var id = new Id<Operation>("ade321ba-53f7-4f72-9748-bf2bbddfd98c");
Operation.Delete deleteCommand = new Operation.Delete( Operation.Delete deleteCommand = new Operation.Delete(
testOperation.id() //testOperation.id()
id
); );
Operation deleteOperation = opService.process(deleteCommand); Operation deleteOperation = opService.process(deleteCommand);
...@@ -162,7 +178,9 @@ public final class Tests ...@@ -162,7 +178,9 @@ public final class Tests
); );
} }
@Test //ade321ba-53f7-4f72-9748-bf2bbddfd98c
//@Test
public void testGetOperations() { public void testGetOperations() {
LocalDate testDate = LocalDate.of(2024, 05, 24); LocalDate testDate = LocalDate.of(2024, 05, 24);
Operation.Filter filter = new Operation.Filter(Optional.of(testDate)); Operation.Filter filter = new Operation.Filter(Optional.of(testDate));
...@@ -174,6 +192,25 @@ public final class Tests ...@@ -174,6 +192,25 @@ public final class Tests
System.out.println(operation); System.out.println(operation);
assertEquals(operation.date(), testDate); assertEquals(operation.date(), testDate);
} }
}
@Test
public void testCreateOPStaff_1() {
try {
repo.saveOPStaff(testOPStaff);
} catch (Exception e){
e.printStackTrace();
}
}
@Test
public void testCreateOPStaff_2() throws Exception {
OPStaff.CreateOPStaff createCommand = new OPStaff.CreateOPStaff(
Role.SURGEON,
Specialty.cardiology
);
OPStaff createOPStaff = opStaffService.process(createCommand);
} }
} }
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