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

Added update and delete commands for operations.

parent 552da99c
No related branches found
No related tags found
No related merge requests found
...@@ -34,7 +34,7 @@ public record Operation( ...@@ -34,7 +34,7 @@ public record Operation(
/** /**
* Operation commands * Operation commands
*/ */
public static sealed interface Command permits Create { } public static sealed interface Command permits Create, Update, Delete { }
/** /**
* Create command to create a new Operation * Create command to create a new Operation
...@@ -48,4 +48,15 @@ public record Operation( ...@@ -48,4 +48,15 @@ public record Operation(
LocalTime endTime LocalTime endTime
) implements Command {} ) implements Command {}
public static record Update(
Id<Operation> id,
Optional<LocalDate> date,
Optional<LocalTime> startTime,
Optional<LocalTime> endTime
) implements Command {}
public static record Delete(
Id<Operation> id
) implements Command {}
} }
...@@ -18,6 +18,8 @@ public class OperationServiceImpl implements OperationService { ...@@ -18,6 +18,8 @@ public class OperationServiceImpl implements OperationService {
public Operation process(Operation.Command cmd) throws Exception { public Operation process(Operation.Command cmd) throws Exception {
return switch (cmd){ return switch (cmd){
case Operation.Create cr -> create(cr); case Operation.Create cr -> create(cr);
case Operation.Update up -> update(up);
case Operation.Delete del -> delete(del);
}; };
} }
...@@ -44,4 +46,30 @@ public class OperationServiceImpl implements OperationService { ...@@ -44,4 +46,30 @@ public class OperationServiceImpl implements OperationService {
return operation; return operation;
} }
public Operation update(Operation.Update up) throws Exception {
Operation currentOperation = repo.findOperation(up.id()).get();
Operation operation =
new Operation(
currentOperation.id(),
up.date().orElse(currentOperation.date()),
up.startTime().orElse(currentOperation.startTime()),
up.endTime().orElse(currentOperation.endTime()),
Instant.now());
repo.save(operation);
return operation;
}
public Operation delete(Operation.Delete del) throws Exception {
Operation operation = repo.findOperation(del.id()).get();
repo.deleteOperation(del.id());
return operation;
}
} }
import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -39,4 +40,10 @@ public interface Repository { ...@@ -39,4 +40,10 @@ public interface Repository {
* @return the matching entries * @return the matching entries
*/ */
List<Operation> findOperations(Operation.Filter filter); List<Operation> findOperations(Operation.Filter filter);
/**
* Deletes the Operation with the matching id.
* @param id the id of the Operation to delete
*/
void deleteOperation(Id<Operation> id) throws SQLException;
} }
...@@ -259,7 +259,7 @@ class JDBCRepository implements Repository ...@@ -259,7 +259,7 @@ class JDBCRepository implements Repository
"UPDATE operations SET " + "UPDATE operations SET " +
"date = " + sqlValue(operation.date()) + "," + "date = " + sqlValue(operation.date()) + "," +
"startTime = " + sqlValue(operation.startTime()) + "," + "startTime = " + sqlValue(operation.startTime()) + "," +
"endTime = " + sqlValue(operation.endTime()) + " " + "endTime = " + sqlValue(operation.endTime()) + "," +
"lastUpdate = " + sqlValue(operation.lastUpdate()) + " " + "lastUpdate = " + sqlValue(operation.lastUpdate()) + " " +
"WHERE id = " + sqlValue(operation.id().value()) + ";"; "WHERE id = " + sqlValue(operation.id().value()) + ";";
} }
...@@ -381,4 +381,14 @@ class JDBCRepository implements Repository ...@@ -381,4 +381,14 @@ class JDBCRepository implements Repository
rs.getTimestamp("lastUpdate").toInstant() rs.getTimestamp("lastUpdate").toInstant()
); );
} }
@Override
public void deleteOperation(Id<Operation> id) throws SQLException {
var operation = findOperation(id);
if(operation.isPresent()) {
conn.createStatement()
.executeUpdate("DELETE FROM operations WHERE id = " + quoted(id.value()) + ";");
}
}
} }
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.sql.SQLException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
...@@ -16,12 +17,10 @@ public final class Tests ...@@ -16,12 +17,10 @@ public final class Tests
private static Repository repo = null; private static Repository repo = null;
private static OperationService opService;
//private static Patient testPatient = null;
private static Operation testOperation = null; private static Operation testOperation = null;
private static OperationService opService;
@BeforeClass @BeforeClass
public static void init() throws Exception { public static void init() throws Exception {
...@@ -31,17 +30,6 @@ public final class Tests ...@@ -31,17 +30,6 @@ public final class Tests
repo = JDBCRepository.instance(); repo = JDBCRepository.instance();
/*
testPatient = new Patient(
new Id<>("1111"),
Gender.MALE,
"Hans",
"Guenther",
LocalDate.of(1999, 01, 01),
new Address("Musterstraße", "12", "12345", "Beispielhausen"),
"AOK");
*/
testOperation = new Operation( testOperation = new Operation(
new Id<>("1111"), new Id<>("1111"),
LocalDate.of(2024, 05, 24), LocalDate.of(2024, 05, 24),
...@@ -52,68 +40,128 @@ public final class Tests ...@@ -52,68 +40,128 @@ public final class Tests
opService = new OperationServiceImpl(repo); opService = new OperationServiceImpl(repo);
} }
/*
@Test @Test
public void testPatientSave(){ public void testRepoOperationSave(){
try { try {
repo.createPatient(testPatient); repo.save(testOperation);
} catch (Exception e){ } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
assertTrue( assertTrue(
repo.findPatient(testPatient.id()).isPresent() repo.findOperation(testOperation.id()).isPresent()
); );
} }
@Test @Test
public void testPatientDelete(){ public void testRepoOperationUpdate() {
try { try {
repo.deletePatient(testPatient.id()); repo.save(testOperation);
} catch (Exception e){ } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
assertTrue( Operation updatedTestOperation = new Operation(
repo.findPatient(testPatient.id()).isEmpty() testOperation.id(),
LocalDate.of(1999, 01, 01),
testOperation.startTime(),
testOperation.endTime(),
Instant.now()
); );
}
*/
@Test
public void testOperationSave(){
try { try {
repo.save(testOperation); repo.save(updatedTestOperation);
} catch (Exception e){ } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
assertTrue( assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().date(), testOperation.date());
repo.findOperation(testOperation.id()).isPresent()
); assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().lastUpdate(), testOperation.lastUpdate());
}
@Test
public void testRepoOperationDelete() {
try {
repo.save(testOperation);
} catch (Exception e){
e.printStackTrace();
}
try {
repo.deleteOperation(testOperation.id());
} catch (SQLException e) {
throw new RuntimeException(e);
}
} }
@Test @Test
public void testCreateOperation() throws Exception { public void testCreateOperation() throws Exception {
Operation.Command createCommand = new Operation.Create( Operation.Create createCommand = new Operation.Create(
LocalDate.of(2025, 05, 11), LocalDate.of(2025, 05, 11),
LocalTime.of(10, 30, 00), LocalTime.of(10, 30, 00),
LocalTime.of(11, 00, 00)); LocalTime.of(11, 00, 00)
);
Operation createOperation = opService.process(createCommand); Operation createOperation = opService.process(createCommand);
System.out.println(createOperation);
assertTrue( assertTrue(
repo.findOperation(createOperation.id()).isPresent() repo.findOperation(createOperation.id()).isPresent()
); );
} }
@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.empty()
);
opService.getOperation(testOperation.id());
Operation updateOperation = opService.process(updateCommand);
assertNotEquals(
repo.findOperation(updateOperation.id()).get().date(), testOperation.date()
);
assertEquals(
repo.findOperation(updateOperation.id()).get().startTime(), testOperation.startTime()
);
assertEquals(
repo.findOperation(updateOperation.id()).get().endTime(), testOperation.endTime()
);
assertNotEquals(
repo.findOperation(updateOperation.id()).get().lastUpdate(), testOperation.lastUpdate()
);
}
@Test
public void testDeleteOperation() throws Exception {
repo.save(testOperation);
Operation.Delete deleteCommand = new Operation.Delete(
testOperation.id()
);
Operation deleteOperation = opService.process(deleteCommand);
assertFalse(
repo.findOperation(deleteOperation.id()).isPresent()
);
}
@Test @Test
public void testGetOperations() { public void testGetOperations() {
LocalDate testDate = LocalDate.of(2024, 05, 24); LocalDate testDate = LocalDate.of(2024, 05, 24);
...@@ -124,7 +172,7 @@ public final class Tests ...@@ -124,7 +172,7 @@ public final class Tests
for (Operation operation : operations) { for (Operation operation : operations) {
System.out.println(operation); System.out.println(operation);
assertEquals(operation.date(), testDate); assertEquals(operation.date(), testDate);
} }
} }
......
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