From c3303d1525ecf441454abdfe1df8879ad3941185 Mon Sep 17 00:00:00 2001
From: agoer <alexander.goerlitz@student.reutlingen-university.de>
Date: Fri, 14 Jun 2024 19:29:18 +0200
Subject: [PATCH] Implemented the methods to create a new OPStaff with the enum
 Specialty

---
 opp/core/src/main/java/OPStaff.java           | 23 ++++++-
 opp/core/src/main/java/OPStaffService.java    | 23 +++++++
 .../src/main/java/OPStaffServiceImpl.java     | 38 +++++++++++
 opp/core/src/main/java/Repository.java        | 14 ++++
 opp/core/src/main/java/Specialty.java         |  6 ++
 .../src/main/java/JDBCRepository.java         | 68 +++++++++++++++++++
 opp/jdbc-repo-impl/src/test/java/Tests.java   | 55 ++++++++++++---
 7 files changed, 216 insertions(+), 11 deletions(-)
 create mode 100644 opp/core/src/main/java/OPStaffService.java
 create mode 100644 opp/core/src/main/java/OPStaffServiceImpl.java
 create mode 100644 opp/core/src/main/java/Specialty.java

diff --git a/opp/core/src/main/java/OPStaff.java b/opp/core/src/main/java/OPStaff.java
index d92a5af..352781c 100644
--- a/opp/core/src/main/java/OPStaff.java
+++ b/opp/core/src/main/java/OPStaff.java
@@ -1,9 +1,28 @@
 import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalTime;
 
 public record OPStaff(
-        Id<OPStaff> opStaffId,
+        Id<OPStaff> id,
         Role role,
-        String specialty,
+        Specialty specialty,
         Instant lastUpdate
 ) {
+
+    /**
+     * OPStaff commands
+     */
+    public static sealed interface Command permits OPStaff.CreateOPStaff { } //, OPStaff.UpdateOPStaff, OPStaff.DeleteOPStaff
+
+    /**
+     * Create command to create a new Operation
+     * @param role if the Personal is a surgeon or an assistant
+     * @param specialty the hospital specialties
+     */
+    public static record CreateOPStaff(
+            Role role,
+            Specialty specialty
+    ) implements Command {}
+
+
 }
diff --git a/opp/core/src/main/java/OPStaffService.java b/opp/core/src/main/java/OPStaffService.java
new file mode 100644
index 0000000..d08b1a7
--- /dev/null
+++ b/opp/core/src/main/java/OPStaffService.java
@@ -0,0 +1,23 @@
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Available Services for OPStaff
+ */
+public interface OPStaffService {
+
+    /**
+     * Processes the possible commands for an OPStaff
+     * @param cmd the command to process
+     * @return the processed OPStaff
+     */
+    public OPStaff process(OPStaff.Command cmd) throws Exception;
+
+    /**
+     * Gets the Operation by ID
+     * @param id the ID of the OPStaff
+     * @return the OPStaff
+     */
+    public Optional<OPStaff> getOPStaff(Id<Operation> id);
+
+}
\ No newline at end of file
diff --git a/opp/core/src/main/java/OPStaffServiceImpl.java b/opp/core/src/main/java/OPStaffServiceImpl.java
new file mode 100644
index 0000000..fadda58
--- /dev/null
+++ b/opp/core/src/main/java/OPStaffServiceImpl.java
@@ -0,0 +1,38 @@
+import java.time.Instant;
+import java.util.Optional;
+
+public class OPStaffServiceImpl implements OPStaffService{
+
+    private final Repository repo;
+
+    public OPStaffServiceImpl(Repository repo) {
+        this.repo = repo;
+    }
+
+    @Override
+    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.Delete del -> delete(del);
+        };
+    }
+
+    public OPStaff createOPStaffimpl(OPStaff.CreateOPStaff cr) throws Exception {
+        OPStaff opStaff =
+                new OPStaff(
+                        repo.opStaffId(),
+                        cr.role(),
+                        cr.specialty(),
+                        Instant.now());
+
+        repo.saveOPStaff(opStaff);
+
+        return opStaff;
+    }
+
+    @Override
+    public Optional<OPStaff> getOPStaff(Id<Operation> id) {
+        return Optional.empty();
+    }
+}
diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java
index 70224cd..859941d 100644
--- a/opp/core/src/main/java/Repository.java
+++ b/opp/core/src/main/java/Repository.java
@@ -46,4 +46,18 @@ public interface Repository {
      * @param id the id of the Operation to delete
      */
     void deleteOperation(Id<Operation> id) throws SQLException;
+
+    /**
+     * Returns a new generated ID in the opstaff-SQLTable
+     * If the generated ID is already used, generate another one.
+     * @return OPStaff ID
+     */
+     Id<OPStaff> opStaffId();
+
+    /**
+     * Saves the OPStaff into the database.
+     * @param
+     * @throws Exception while saving
+     */
+    void saveOPStaff(OPStaff opStaff) throws Exception;
 }
diff --git a/opp/core/src/main/java/Specialty.java b/opp/core/src/main/java/Specialty.java
new file mode 100644
index 0000000..4a48eed
--- /dev/null
+++ b/opp/core/src/main/java/Specialty.java
@@ -0,0 +1,6 @@
+public enum Specialty {
+    Orthopedics,
+    cardiology,
+    urology,
+    Gastroenterology
+}
diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java
index 6b1a5d1..277d1fa 100644
--- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java
+++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java
@@ -50,6 +50,7 @@ class JDBCRepository implements Repository
     try (var stmt = conn.createStatement()){
 
       stmt.execute(CREATE_OPERATION_TABLE);
+      stmt.execute(CREATE_OPSTAFF_TABLE);
 
     } catch (SQLException e){
       throw new RuntimeException(e);
@@ -391,4 +392,71 @@ class JDBCRepository implements Repository
               .executeUpdate("DELETE FROM operations WHERE id = " + quoted(id.value()) + ";");
     }
   }
+
+
+  /**
+   * The structure of the operations table.
+   */
+  private static final String CREATE_OPSTAFF_TABLE = """
+    CREATE TABLE IF NOT EXISTS opstaff(
+      id VARCHAR(50) PRIMARY KEY,
+      role VARCHAR(50) NOT NULL,
+      specialty VARCHAR(50) NOT NULL,
+      lastUpdate TIMESTAMP NOT NULL
+    );
+  """;
+
+  /**
+   * Returns a new generated ID in the opstaff-SQLTable
+   * If the generated ID is already used, generate another one.
+   * @return OPStaff ID
+   */
+  @Override
+  public Id<OPStaff> opStaffId() {
+
+    var id = new Id<OPStaff>(randomUUID().toString());
+
+    return id; //TODO //findOperation(id).isEmpty() ? id : operationId();
+  }
+
+  /**
+   * Connects to the database and checks if the ID of the given OPStaff is already used.
+   * If yes, then update existing entry, else insert new entry.
+   * @param opStaff the OPStaff to save into the database
+   */
+  @Override
+  public void saveOPStaff(OPStaff opStaff) {
+
+    try (
+            var stmt = conn.createStatement()
+    ){
+      //TODO
+       /*var sql =
+              findOperation(operation.id()).isPresent() ?
+                      updateSQL(operation) :
+                      insertSQL(operation);*/
+      var sql = insertOPStaffSQL(opStaff);
+
+      stmt.executeUpdate(sql);
+
+    } catch (SQLException e){
+      throw new RuntimeException(e);
+    }
+  }
+
+  private static String insertOPStaffSQL(OPStaff opStaff){
+    return
+            "INSERT INTO opstaff(" +
+                    "id,role,specialty,lastUpdate" +
+                    ") VALUES (" +
+                    sqlValue(opStaff.id().value()) + "," +
+                    sqlValue(opStaff.role()) + "," +
+                    sqlValue(opStaff.specialty()) + "," +
+                    sqlValue(opStaff.lastUpdate()) +
+                    ");";
+  }
+
 }
+
+
+
diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java
index 4e75e17..9586021 100644
--- a/opp/jdbc-repo-impl/src/test/java/Tests.java
+++ b/opp/jdbc-repo-impl/src/test/java/Tests.java
@@ -21,6 +21,10 @@ public final class Tests
 
   private static OperationService opService;
 
+  private static OPStaff testOPStaff = null;
+
+  private static OPStaffService opStaffService;
+
   @BeforeClass
   public static void init() throws Exception {
 
@@ -38,9 +42,18 @@ public final class Tests
             Instant.now());
 
     opService = new OperationServiceImpl(repo);
+
+    testOPStaff = new OPStaff(
+            new Id<>("3333"),
+            Role.SURGEON,
+            Specialty.urology,
+            Instant.now());
+
+    opStaffService = new OPStaffServiceImpl(repo);
+
   }
 
-  @Test
+  //@Test
   public void testRepoOperationSave(){
 
     try {
@@ -54,7 +67,7 @@ public final class Tests
     );
   }
 
-  @Test
+  //@Test
   public void testRepoOperationUpdate() {
 
     try {
@@ -82,7 +95,7 @@ public final class Tests
       assertNotEquals(repo.findOperation(updatedTestOperation.id()).get().lastUpdate(), testOperation.lastUpdate());
   }
 
-  @Test
+  //@Test
   public void testRepoOperationDelete() {
       try {
         repo.save(testOperation);
@@ -97,7 +110,7 @@ public final class Tests
       }
   }
 
-  @Test
+  //@Test
   public void testCreateOperation() throws Exception {
 
     Operation.Create createCommand = new Operation.Create(
@@ -113,7 +126,7 @@ public final class Tests
     );
   }
 
-  @Test
+  //@Test
   public void testUpdateOperation() throws Exception {
 
     repo.save(testOperation);
@@ -146,13 +159,16 @@ public final class Tests
     );
   }
 
-  @Test
+
   public void testDeleteOperation() throws Exception {
 
-    repo.save(testOperation);
+    //repo.save(testOperation);
+
+    var id = new Id<Operation>("ade321ba-53f7-4f72-9748-bf2bbddfd98c");
 
     Operation.Delete deleteCommand = new Operation.Delete(
-            testOperation.id()
+            //testOperation.id()
+            id
     );
 
     Operation deleteOperation = opService.process(deleteCommand);
@@ -162,7 +178,9 @@ public final class Tests
     );
   }
 
-  @Test
+  //ade321ba-53f7-4f72-9748-bf2bbddfd98c
+
+  //@Test
   public void testGetOperations() {
     LocalDate testDate = LocalDate.of(2024, 05, 24);
     Operation.Filter filter = new Operation.Filter(Optional.of(testDate));
@@ -174,6 +192,25 @@ public final class Tests
       System.out.println(operation);
       assertEquals(operation.date(), testDate);
     }
+  }
+
+  @Test
+  public void testCreateOPStaff_1() {
+    try {
+      repo.saveOPStaff(testOPStaff);
+    } catch (Exception e){
+      e.printStackTrace();
+    }
+  }
 
+  @Test
+  public void testCreateOPStaff_2() throws Exception {
+    OPStaff.CreateOPStaff createCommand = new OPStaff.CreateOPStaff(
+            Role.SURGEON,
+            Specialty.cardiology
+    );
+
+    OPStaff createOPStaff = opStaffService.process(createCommand);
   }
+
 }
-- 
GitLab