From a176cc15c354e1435d3fca743491f3e9d4c98ead Mon Sep 17 00:00:00 2001
From: agoer <alexander.goerlitz@student.reutlingen-university.de>
Date: Thu, 27 Jun 2024 21:21:37 +0200
Subject: [PATCH] preparatioNote command for delete working

---
 opp/core/src/main/java/PreparationNote.java     |  6 +++++-
 opp/core/src/main/java/PreparationNoteImpl.java | 14 ++++++++++++--
 opp/core/src/main/java/Repository.java          |  2 ++
 .../src/main/java/JDBCRepository.java           | 17 +++++++++++++++++
 opp/jdbc-repo-impl/src/test/java/Tests.java     | 16 ++++++++++++++++
 5 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/opp/core/src/main/java/PreparationNote.java b/opp/core/src/main/java/PreparationNote.java
index 435270b..25bf198 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 f25bafd..2b40100 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 ff9813a..10b040b 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 e65311a..0888d71 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 7177ea7..90b8b9f 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(){
-- 
GitLab