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

implemented update for OPStaff.

parent 22ff133f
No related branches found
No related tags found
No related merge requests found
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;
public record OPStaff(
Id<OPStaff> id,
......@@ -12,7 +13,7 @@ public record OPStaff(
/**
* OPStaff commands
*/
public static sealed interface Command permits OPStaff.CreateOPStaff { } //, OPStaff.UpdateOPStaff, OPStaff.DeleteOPStaff
public static sealed interface Command permits OPStaff.CreateOPStaff, OPStaff.UpdateOPStaff { } //OPStaff.DeleteOPStaff
/**
* Create command to create a new Operation
......@@ -24,5 +25,11 @@ public record OPStaff(
Specialty specialty
) implements Command {}
public static record UpdateOPStaff(
Id<OPStaff> id,
Optional<Role> role,
Optional<Specialty> specialty
) implements Command {}
}
......@@ -14,7 +14,7 @@ public class OPStaffServiceImpl implements OPStaffService{
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.UpdateOPStaff up -> updateOPStaffimpl(up);
//case OPStaff.Delete del -> delete(del);
};
}
......@@ -32,6 +32,21 @@ public class OPStaffServiceImpl implements OPStaffService{
return opStaff;
}
public OPStaff updateOPStaffimpl(OPStaff.UpdateOPStaff up) throws Exception{
OPStaff currentOPStaff = repo.findOPStaff(up.id()).get();
OPStaff opStaff =
new OPStaff(
currentOPStaff.id(),
up.role().orElse(currentOPStaff.role()),
up.specialty().orElse((currentOPStaff.specialty())),
Instant.now()
);
repo.saveOPStaff(opStaff);
return opStaff;
}
public Optional<OPStaff> getOPStaff(Id<OPStaff> id) {
return repo.findOPStaff(id);
......
......@@ -431,11 +431,11 @@ class JDBCRepository implements Repository
var stmt = conn.createStatement()
){
//TODO
/*var sql =
findOperation(operation.id()).isPresent() ?
updateSQL(operation) :
insertSQL(operation);*/
var sql = insertOPStaffSQL(opStaff);
var sql =
findOPStaff(opStaff.id()).isPresent() ?
updateOPStaffSQL(opStaff) :
insertOPStaffSQL(opStaff);
//var sql = insertOPStaffSQL(opStaff);
stmt.executeUpdate(sql);
......@@ -522,6 +522,20 @@ 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
* @return the SQL statement
*/
private static String updateOPStaffSQL(OPStaff opStaff){
return
"UPDATE opstaff SET " +
"role = " + sqlValue(opStaff.role()) + "," +
"specialty = " + sqlValue(opStaff.specialty()) + "," +
"lastUpdate = " + sqlValue(opStaff.lastUpdate()) + " " +
"WHERE id = " + sqlValue(opStaff.id().value()) + ";";
}
}
......
......@@ -226,7 +226,7 @@ public final class Tests
//assertEquals(operation.date(), testDate);
}
@Test
//@Test
public void testGetOPStaffs() {
try{
......@@ -241,6 +241,19 @@ public final class Tests
}
//assertEquals(operation.date(), testDate);
}
@Test
public void testUpdateOPStaff() throws Exception{
OPStaff.UpdateOPStaff updateCommand = new OPStaff.UpdateOPStaff(
testOPStaff.id(),
Optional.empty(),
Optional.of(Specialty.Orthopedics)
);
OPStaff updateOPStaff = opStaffService.process(updateCommand);
}
}
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