diff --git a/opp/core/src/main/java/PreparationNote.java b/opp/core/src/main/java/PreparationNote.java index 073b3efc5e856a3b985083469b314115bf75e666..958257d223e7c3a917452f4a091ee21b775f6c88 100644 --- a/opp/core/src/main/java/PreparationNote.java +++ b/opp/core/src/main/java/PreparationNote.java @@ -1,80 +1,21 @@ import java.time.Instant; -import java.time.LocalDateTime; -import java.util.Objects; +import java.time.LocalDate; +import java.time.LocalTime; -import java.util.Objects; +public record PreparationNote ( -public class PreparationNote { - private String noteId; // Eindeutige Identifikation der Notiz - private String operationId; // ID der zugehörigen Operation - private String content; // Inhalt der Notiz + Id<PreparationNote> id, + String note, + Id<Operation> operationsId, + Instant lastUpdate +) { - // Konstruktor zur Initialisierung der Notiz-Attribute - public PreparationNote(String noteId, String operationId, String content) { - this.noteId = noteId; - this.operationId = operationId; - this.content = content; - } -/* - // Getter und Setter Methoden für die Attribute - public String getId() { - return noteId; - } + public static sealed interface Command permits Create { } //Update, Delete - public void setNoteId(String noteId) { - this.noteId = noteId; - } + public static record Create( + String note, + Id<Operation> operationsId + ) implements Command {} - public String getOperationId() { - return operationId; - } - public void setOperationId(String operationId) { - this.operationId = operationId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - // toString Methode zur Darstellung der Notiz als String - @Override - public String toString() { - return "PreparationNote{" + - "noteId='" + noteId + '\'' + - ", operationId='" + operationId + '\'' + - ", content='" + content + '\'' + - '}'; - } - - // equals Methode zum Vergleichen von zwei PreparationNote Objekten - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - PreparationNote that = (PreparationNote) o; - return Objects.equals(noteId, that.NoteId) && - Objects.equals(operationId, that.operationId) && - Objects.equals(content, that.content); - } - // hashCode Methode zur Berechnung des Hashcodes eines PreparationNote Objekts - @Override - public int hashCode() { - return Objects.hash(noteId, operationId, content); - } - - - */ } - -//public record PreparationNote( -// Id<PreparationNote> preparationNoteId, -// String text, -// Operation operationId, -// Instant lastUpdate -//) { - diff --git a/opp/core/src/main/java/PreparationNoteCommand.java b/opp/core/src/main/java/PreparationNoteCommand.java deleted file mode 100644 index 4581407480159030077bd461dc98811926b06ce9..0000000000000000000000000000000000000000 --- a/opp/core/src/main/java/PreparationNoteCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -public class PreparationNoteCommand { - private String operationId; - private String content; - - // Konstruktor zur Initialisierung der Command-Attribute - public PreparationNoteCommand(String operationId, String content) { - this.operationId = operationId; - this.content = content; - } - - // Getter und Setter Methoden für die Attribute - public String getOperationId() { - return operationId; - } - - public void setOperationId(String operationId) { - this.operationId = operationId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } -} diff --git a/opp/core/src/main/java/PreparationNoteImpl.java b/opp/core/src/main/java/PreparationNoteImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..112215a04850fb53882abf800cc117fd70257a90 --- /dev/null +++ b/opp/core/src/main/java/PreparationNoteImpl.java @@ -0,0 +1,40 @@ +import java.time.Instant; +import java.util.Optional; + +public class PreparationNoteImpl implements PreparationNoteService { + + private final Repository repo; + + public PreparationNoteImpl(Repository repo) { + this.repo = repo; + } + + @Override + public PreparationNote process(PreparationNote.Command cmd) throws Exception { + return switch (cmd){ + case PreparationNote.Create cr -> create(cr); + //case Operation.Update up -> update(up); + //case Operation.Delete del -> delete(del); + }; + } + + public PreparationNote create(PreparationNote.Create cr) throws Exception { + PreparationNote preparationNote = + new PreparationNote( + repo.preparationNoteId(), + cr.note(), + cr.operationsId(), + Instant.now()); + + repo.save(preparationNote); + + return preparationNote; + } + + public Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id){ + return null; + } + + + +} diff --git a/opp/core/src/main/java/PreparationNoteService.java b/opp/core/src/main/java/PreparationNoteService.java index 485c834dfa8432e1a2432823773422ba9539920d..d2f230ea643fa4410d9d0a071bcba564c3929ba0 100644 --- a/opp/core/src/main/java/PreparationNoteService.java +++ b/opp/core/src/main/java/PreparationNoteService.java @@ -1,9 +1,8 @@ -import java.util.List; +import java.util.Optional; public interface PreparationNoteService { - PreparationNote createNote(PreparationNoteCommand command); // Methode zum Erstellen einer neuen Notiz - PreparationNote updateNote(String noteId, PreparationNoteCommand command); // Methode zum Aktualisieren einer Notiz - void deleteNote(String noteId); // Methode zum Löschen einer Notiz - PreparationNote getNoteById(String noteId); // Methode zum Abrufen einer Notiz nach ID - List<PreparationNote> getNotesByOperationId(String operationId); // Methode zum Abrufen von Notizen nach Operation-ID + + PreparationNote process(PreparationNote.Command cmd) throws Exception; + + Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id); } diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 0002eb54b7710bbf43d82b8c640f036c73cdbe4b..ff9813ac94a450e222f9a309dee860acb26114b7 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -99,4 +99,11 @@ public interface Repository { Boolean findOperationTeamOPStaff(Id<OperationTeam> operationTeamId, Id<OPStaff> opStaffId); Boolean findOPStaffOperationTeams(Id<OPStaff> opStaffId); + + + Id<PreparationNote> preparationNoteId(); + + Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id); + + void save(PreparationNote preparationNote); } diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index 9063cbbe9a18fefc8ea062f720f3458338654cb5..e65311ad13938c3df16e2a591ee494f837b62fb5 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -97,6 +97,19 @@ class JDBCRepository implements Repository ); """; + /** + * The structure of the opStaffs table. + */ + private static final String CREATE_PREPARATIONNOTE_TABLE = """ + CREATE TABLE IF NOT EXISTS preparationNote( + id VARCHAR(50) PRIMARY KEY, + note VARCHAR(50) NOT NULL, + operationsId VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL, + FOREIGN KEY (operationsId) REFERENCES operations(id) + ); + """; + //endregion @@ -112,6 +125,7 @@ class JDBCRepository implements Repository stmt.execute(CREATE_OP_STAFF_TABLE); stmt.execute(CREATE_OPERATION_TABLE); stmt.execute(CREATE_OP_STAFF_IN_OPERATION_TEAM_TABLE); + stmt.execute(CREATE_PREPARATIONNOTE_TABLE); } catch (SQLException e){ throw new RuntimeException(e); @@ -810,6 +824,89 @@ class JDBCRepository implements Repository //endregion + + + @Override + public Id<PreparationNote> preparationNoteId() { + + var id = new Id<PreparationNote>(randomUUID().toString()); + + return findPreparationNote(id).isEmpty() ? id : preparationNoteId(); + } + + public Optional<PreparationNote> findPreparationNote(Id<PreparationNote> id){ + try ( + var result = + conn.createStatement() + .executeQuery("SELECT * FROM preparationNote WHERE id = " + sqlValue(id.value()) + ";") + ){ + return + result.next() ? + Optional.of(readPreparationNoteFromRow(result)) : + Optional.empty(); + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + + private static PreparationNote readPreparationNoteFromRow(ResultSet rs) throws SQLException { + + return new PreparationNote( + new Id<>(rs.getString("id")), + rs.getString("note"), + new Id<>(rs.getString("operationsId")), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + + @Override + public void save(PreparationNote preparationNote) { + try ( + var stmt = conn.createStatement() + ){ + var sql = + findPreparationNote (preparationNote.id()).isPresent() ? + updateSQL(preparationNote) : + insertSQL(preparationNote ); + + stmt.executeUpdate(sql); + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + /* + CREATE TABLE IF NOT EXISTS preparationNote( + id VARCHAR(50) PRIMARY KEY, + note VARCHAR(50) NOT NULL, + operationsId VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL, + FOREIGN KEY (operationsId) REFERENCES operations(id) + ); + */ + private static String updateSQL(PreparationNote preparationNote){ + return + "UPDATE preparationNote SET " + + "note = " + sqlValue(preparationNote.note()) + "," + + "lastUpdate = " + sqlValue(preparationNote.lastUpdate()) + " " + + "WHERE id = " + sqlValue(preparationNote.id().value()) + ";"; + } + + private static String insertSQL(PreparationNote preparationNote){ + return + "INSERT INTO preparationNote(" + + "id,note,operationsId,lastUpdate" + + ") VALUES (" + + sqlValue(preparationNote.id().value()) + "," + + sqlValue(preparationNote.note()) + "," + + sqlValue(preparationNote.operationsId()) + "," + + sqlValue(preparationNote.lastUpdate()) + + ");"; + } + } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index eb583c7ef3ee27484a60a07f15d9727d4f88c8ce..35e0fcff0346ab1c6d26ac2b385e461c9d7324fb 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -36,6 +36,10 @@ public final class Tests private static OperationTeamService operationTeamService; + private static PreparationNote testPreparationNote = null; + + private static PreparationNoteService preparationNoteService; + @BeforeClass public static void init() throws Exception { @@ -90,15 +94,24 @@ public final class Tests Instant.now() ); + testPreparationNote = new PreparationNote( + new Id<>("note1111"), + ("Test notiz!"), + testOperation.id(), + Instant.now() + ); + operationService = new OperationServiceImpl(repo); opStaffService = new OPStaffServiceImpl(repo); operationTeamService = new OperationTeamImpl(repo); + preparationNoteService = new PreparationNoteImpl(repo); + } - @Ignore +@Ignore @Test public void testRepoOperationSave(){ @@ -114,6 +127,54 @@ public final class Tests ); } + @Ignore + @Test + public void testRepoPreparationNoteSave(){ + + try { + repo.save(testPreparationNote); + } catch (Exception e){ + e.printStackTrace(); + } + + assertTrue( + repo.findPreparationNote(testPreparationNote.id()).isPresent() + ); + } + + + + @Test + public void testRepoPreparationNoteUpdate() throws Exception { + + try { + repo.save(testPreparationNote); + } catch (Exception e){ + e.printStackTrace(); + } + + PreparationNote updatedTestPreparatioNote = new PreparationNote( + testPreparationNote.id(), + ("ES WURDE GEUPDATED!!!"), + testPreparationNote.operationsId(), + Instant.now() + ); + + try { + repo.save(updatedTestPreparatioNote); + } catch (Exception e){ + e.printStackTrace(); + } + + Optional<PreparationNote> readPreparationNote = repo.findPreparationNote(updatedTestPreparatioNote.id()); + if (readPreparationNote.isPresent()) { + assertNotEquals(readPreparationNote.get().note(), testPreparationNote.note()); + assertNotEquals(readPreparationNote.get().lastUpdate(), testPreparationNote.lastUpdate()); + } else { + throw new Exception(); + } + } + @Ignore @Test public void testRepoOperationUpdate() throws Exception {