From d8abd9bc4f318825b9ab357cbaf49af6290b5a7b Mon Sep 17 00:00:00 2001 From: Andre Hartig <andre.hartig@student.reutlingen-university.de> Date: Sun, 9 Jun 2024 13:30:06 +0200 Subject: [PATCH] Added update and delete commands for operations. --- opp/core/src/main/java/Operation.java | 13 +- .../src/main/java/OperationServiceImpl.java | 28 ++++ opp/core/src/main/java/Repository.java | 7 + .../src/main/java/JDBCRepository.java | 12 +- opp/jdbc-repo-impl/src/test/java/Tests.java | 124 ++++++++++++------ 5 files changed, 144 insertions(+), 40 deletions(-) rename opp/{jdbc-repo-impl => core}/src/main/java/OperationServiceImpl.java (56%) diff --git a/opp/core/src/main/java/Operation.java b/opp/core/src/main/java/Operation.java index b0d376b..bc7c622 100644 --- a/opp/core/src/main/java/Operation.java +++ b/opp/core/src/main/java/Operation.java @@ -34,7 +34,7 @@ public record Operation( /** * 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 @@ -48,4 +48,15 @@ public record Operation( LocalTime endTime ) 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 {} + } diff --git a/opp/jdbc-repo-impl/src/main/java/OperationServiceImpl.java b/opp/core/src/main/java/OperationServiceImpl.java similarity index 56% rename from opp/jdbc-repo-impl/src/main/java/OperationServiceImpl.java rename to opp/core/src/main/java/OperationServiceImpl.java index 4e0f04d..264f960 100644 --- a/opp/jdbc-repo-impl/src/main/java/OperationServiceImpl.java +++ b/opp/core/src/main/java/OperationServiceImpl.java @@ -18,6 +18,8 @@ public class OperationServiceImpl implements OperationService { public Operation process(Operation.Command cmd) throws Exception { return switch (cmd){ 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 { 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; + } } diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index b47dd81..70224cd 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -1,3 +1,4 @@ +import java.sql.SQLException; import java.util.List; import java.util.Optional; @@ -39,4 +40,10 @@ public interface Repository { * @return the matching entries */ 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; } diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index 553d367..6b1a5d1 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -259,7 +259,7 @@ class JDBCRepository implements Repository "UPDATE operations SET " + "date = " + sqlValue(operation.date()) + "," + "startTime = " + sqlValue(operation.startTime()) + "," + - "endTime = " + sqlValue(operation.endTime()) + " " + + "endTime = " + sqlValue(operation.endTime()) + "," + "lastUpdate = " + sqlValue(operation.lastUpdate()) + " " + "WHERE id = " + sqlValue(operation.id().value()) + ";"; } @@ -381,4 +381,14 @@ class JDBCRepository implements Repository 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()) + ";"); + } + } } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index 4643f70..4e75e17 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -1,6 +1,7 @@ import org.junit.BeforeClass; import org.junit.Test; +import java.sql.SQLException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; @@ -16,12 +17,10 @@ public final class Tests private static Repository repo = null; - private static OperationService opService; - - //private static Patient testPatient = null; - private static Operation testOperation = null; + private static OperationService opService; + @BeforeClass public static void init() throws Exception { @@ -31,17 +30,6 @@ public final class Tests 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( new Id<>("1111"), LocalDate.of(2024, 05, 24), @@ -52,68 +40,128 @@ public final class Tests opService = new OperationServiceImpl(repo); } - /* @Test - public void testPatientSave(){ + public void testRepoOperationSave(){ try { - repo.createPatient(testPatient); + repo.save(testOperation); } catch (Exception e){ e.printStackTrace(); } assertTrue( - repo.findPatient(testPatient.id()).isPresent() + repo.findOperation(testOperation.id()).isPresent() ); } - @Test - public void testPatientDelete(){ + public void testRepoOperationUpdate() { try { - repo.deletePatient(testPatient.id()); + repo.save(testOperation); } catch (Exception e){ e.printStackTrace(); } - assertTrue( - repo.findPatient(testPatient.id()).isEmpty() + Operation updatedTestOperation = new Operation( + testOperation.id(), + LocalDate.of(1999, 01, 01), + testOperation.startTime(), + testOperation.endTime(), + Instant.now() ); - } - */ - - @Test - public void testOperationSave(){ try { - repo.save(testOperation); + repo.save(updatedTestOperation); } catch (Exception e){ e.printStackTrace(); } - assertTrue( - repo.findOperation(testOperation.id()).isPresent() - ); + assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().date(), testOperation.date()); + + 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 public void testCreateOperation() throws Exception { - Operation.Command createCommand = new Operation.Create( + Operation.Create createCommand = new Operation.Create( LocalDate.of(2025, 05, 11), LocalTime.of(10, 30, 00), - LocalTime.of(11, 00, 00)); + LocalTime.of(11, 00, 00) + ); Operation createOperation = opService.process(createCommand); - System.out.println(createOperation); - assertTrue( 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 public void testGetOperations() { LocalDate testDate = LocalDate.of(2024, 05, 24); @@ -124,7 +172,7 @@ public final class Tests for (Operation operation : operations) { System.out.println(operation); - assertEquals(operation.date(), testDate); + assertEquals(operation.date(), testDate); } } -- GitLab