From 96978a9ecf16efcaca5dae86dbeb2d4b4a67fcf8 Mon Sep 17 00:00:00 2001 From: agoer <alexander.goerlitz@student.reutlingen-university.de> Date: Sat, 15 Jun 2024 11:03:15 +0200 Subject: [PATCH] implemented getOPStaff with id. code is now runable. --- opp/core/src/main/java/OPStaffService.java | 2 +- .../src/main/java/OPStaffServiceImpl.java | 7 +++- opp/core/src/main/java/PreparationNote.java | 5 ++- opp/core/src/main/java/Repository.java | 11 +++++ opp/core/src/main/java/Room.java | 14 +++---- .../src/main/java/JDBCRepository.java | 40 +++++++++++++++++++ opp/jdbc-repo-impl/src/test/java/Tests.java | 19 ++++++++- 7 files changed, 85 insertions(+), 13 deletions(-) diff --git a/opp/core/src/main/java/OPStaffService.java b/opp/core/src/main/java/OPStaffService.java index d08b1a7..679b57e 100644 --- a/opp/core/src/main/java/OPStaffService.java +++ b/opp/core/src/main/java/OPStaffService.java @@ -18,6 +18,6 @@ public interface OPStaffService { * @param id the ID of the OPStaff * @return the OPStaff */ - public Optional<OPStaff> getOPStaff(Id<Operation> id); + public Optional<OPStaff> getOPStaff(Id<OPStaff> 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 index fadda58..4b33714 100644 --- a/opp/core/src/main/java/OPStaffServiceImpl.java +++ b/opp/core/src/main/java/OPStaffServiceImpl.java @@ -32,7 +32,10 @@ public class OPStaffServiceImpl implements OPStaffService{ } @Override - public Optional<OPStaff> getOPStaff(Id<Operation> id) { - return Optional.empty(); + public Optional<OPStaff> getOPStaff(Id<OPStaff> id) { + + return repo.findOPStaff(id); } + + } diff --git a/opp/core/src/main/java/PreparationNote.java b/opp/core/src/main/java/PreparationNote.java index 58ac8db..073b3ef 100644 --- a/opp/core/src/main/java/PreparationNote.java +++ b/opp/core/src/main/java/PreparationNote.java @@ -15,7 +15,7 @@ public class PreparationNote { this.operationId = operationId; this.content = content; } - +/* // Getter und Setter Methoden für die Attribute public String getId() { return noteId; @@ -66,6 +66,9 @@ public class PreparationNote { public int hashCode() { return Objects.hash(noteId, operationId, content); } + + + */ } //public record PreparationNote( diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 859941d..1d45a7e 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -1,3 +1,4 @@ +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Optional; @@ -60,4 +61,14 @@ public interface Repository { * @throws Exception while saving */ void saveOPStaff(OPStaff opStaff) throws Exception; + + /** + * Reads an OPStaff from the OPStaff-SQLTable with the given ID. + * If no OPStaff with this ID is found, it will return an empty Optional. + * @param id the ID to search for + * @return the found Operation + */ + + Optional<OPStaff> findOPStaff(Id<OPStaff> id); + } diff --git a/opp/core/src/main/java/Room.java b/opp/core/src/main/java/Room.java index a0de6ea..d492847 100644 --- a/opp/core/src/main/java/Room.java +++ b/opp/core/src/main/java/Room.java @@ -9,18 +9,18 @@ public class Room { // Konstruktor zur Initialisierung der Raum-Attribute public Room(String RoomId, String roomName, int capacity) { this.roomId = roomId; - this.roomName = RoomName; + //this.roomName = RoomName; this.capacity = capacity; } // Getter und Setter Methoden für die Attribute - public String getRoomId() { - return RoomId; - } + //public String getRoomId() { + // return RoomId; + //} - public void setRoomId(String roomId) { - this.id = RoomId; - } + //public void setRoomId(String roomId) { + // this.id = RoomId; + //} public String getRoomName() { return roomName; diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index 277d1fa..4983a4d 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -456,6 +456,46 @@ class JDBCRepository implements Repository ");"; } + /** + * Reads an OPStaff from the OPStaff-SQLTable with the given ID. + * If no OPStaff with this ID is found, it will return an empty Optional. + * @param id the ID to search for + * @return the found Operation + */ + @Override + public Optional<OPStaff> findOPStaff(Id<OPStaff> id){ + try ( + var result = + conn.createStatement() + .executeQuery("SELECT * FROM opstaff WHERE id = " + sqlValue(id.value()) + ";") + ){ + return + result.next() ? + Optional.of(readOPStaffFromRow(result)) : + Optional.empty(); + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + /** + * Creates an Operation object with the given result of a read SQL-Row. + * @param rs the result of a read SQL-Row + * @return the created operation object + * @throws SQLException Error while reading the SQL-Row + */ + + private static OPStaff readOPStaffFromRow(ResultSet rs) throws SQLException { + + return new OPStaff( + new Id<>(rs.getString("id")), + Role.valueOf(rs.getString("role")), + Specialty.valueOf(rs.getString("specialty")), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index 9586021..62d3ad2 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -194,7 +194,7 @@ public final class Tests } } - @Test + //@Test public void testCreateOPStaff_1() { try { repo.saveOPStaff(testOPStaff); @@ -203,7 +203,7 @@ public final class Tests } } - @Test + //@Test public void testCreateOPStaff_2() throws Exception { OPStaff.CreateOPStaff createCommand = new OPStaff.CreateOPStaff( Role.SURGEON, @@ -213,4 +213,19 @@ public final class Tests OPStaff createOPStaff = opStaffService.process(createCommand); } + @Test + public void testGetOPStaff() { + + try{ + var id = new Id<OPStaff>("3333"); + Optional<OPStaff> opStaff = opStaffService.getOPStaff(id); + System.out.println(opStaff); + } catch (Exception e){ + e.printStackTrace(); + } + + //assertEquals(operation.date(), testDate); + } } + + -- GitLab