diff --git a/opp/core/src/main/java/PreparationNote.java b/opp/core/src/main/java/PreparationNote.java index 435270b5be9f8ebc7d1b20d4e363631c1a7bbd72..25bf198499066cc83d5dc5936989bfe6efa5bee8 100644 --- a/opp/core/src/main/java/PreparationNote.java +++ b/opp/core/src/main/java/PreparationNote.java @@ -11,7 +11,7 @@ public record PreparationNote ( Instant lastUpdate ) { - public static sealed interface Command permits Create, Update { } //Update, Delete + public static sealed interface Command permits Create, Update, Delete { } public static record Create( String note, @@ -23,5 +23,9 @@ public record PreparationNote ( String note ) implements Command {} + public static record Delete( + Id<PreparationNote> id + ) implements Command {} + } diff --git a/opp/core/src/main/java/PreparationNoteImpl.java b/opp/core/src/main/java/PreparationNoteImpl.java index f25bafd15ca8eebfa6c0af7b4b1cdc3cf75624fe..2b40100f37ea18303b235901b04cb7939d05c485 100644 --- a/opp/core/src/main/java/PreparationNoteImpl.java +++ b/opp/core/src/main/java/PreparationNoteImpl.java @@ -14,7 +14,7 @@ public class PreparationNoteImpl implements PreparationNoteService { return switch (cmd){ case PreparationNote.Create cr -> create(cr); case PreparationNote.Update up -> update(up); - //case PreparationNote.Delete del -> delete(del); + case PreparationNote.Delete del -> delete(del); }; } @@ -52,10 +52,20 @@ public class PreparationNoteImpl implements PreparationNoteService { return preparationNote; } - public Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id){ + private PreparationNote delete(PreparationNote.Delete del) throws Exception { + + Optional<PreparationNote> returnedPreparationNote = repo.findPreparationNote(del.id()); + + if(returnedPreparationNote.isPresent()) { + return repo.deletePreparationNote(del.id()); + } return null; } + public Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id){ + return repo.findPreparationNote(id); + } + } diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index ff9813ac94a450e222f9a309dee860acb26114b7..10b040b6904d875971310aeec3a913e6377455b7 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -106,4 +106,6 @@ public interface Repository { Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id); void save(PreparationNote preparationNote); + + PreparationNote deletePreparationNote(Id<PreparationNote> id); } diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index e65311ad13938c3df16e2a591ee494f837b62fb5..0888d713edbdaa4b98919d31f184d4b495b701fb 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -907,6 +907,23 @@ class JDBCRepository implements Repository ");"; } + @Override + public PreparationNote deletePreparationNote(Id<PreparationNote> id) { + var preparationNote = findPreparationNote(id); + + if(preparationNote.isPresent()) { + var sql = "DELETE FROM preparationNote WHERE id = " + quoted(id.value()) + ";"; + + try { + conn.createStatement().executeUpdate(sql); + return preparationNote.get(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return null; + } + } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index 7177ea7ee20a8bb5e0c774b8d069314dc56fa9b2..90b8b9fdeda7eb34530ab0b35cf96d96433c50af 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -170,6 +170,22 @@ public final class Tests } } + + @Ignore + @Test + public void testDeletePreparationNote() throws Exception { + + repo.save(testPreparationNote); + + PreparationNote.Delete deleteCommand = new PreparationNote.Delete( + testPreparationNote.id() + ); + + PreparationNote deletePreparationNote = preparationNoteService.process(deleteCommand); + + assertFalse(repo.findPreparationNote(deletePreparationNote.id()).isPresent()); + } + @Ignore @Test public void testRepoPreparationNoteSave(){