diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 10b040b6904d875971310aeec3a913e6377455b7..95eae49b8407de71d4151fa5b76a5ccc812fda82 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -108,4 +108,14 @@ public interface Repository { void save(PreparationNote preparationNote); PreparationNote deletePreparationNote(Id<PreparationNote> id); + + Id<Room> roomId(); + + Optional<Room> findRoom(Id<Room> id); + + void save(Room room); + + Room deleteRoom(Id<Room> id); + + } diff --git a/opp/core/src/main/java/Room.java b/opp/core/src/main/java/Room.java index d4928475e0da141e1bd394beddaea61a98cecfac..84df18408b1fd9d0be5d9f4b57e7a39e8f33f93f 100644 --- a/opp/core/src/main/java/Room.java +++ b/opp/core/src/main/java/Room.java @@ -1,72 +1,28 @@ import java.time.Instant; -import java.util.Objects; -public class Room { - private String roomId; // Eindeutige Identifikation des Raum - private String roomName;// Name des Raums - private int capacity; // Kapazität des Raums +public record Room ( + Id<Room> id, + String roomName, + Instant lastUpdate +) { - // Konstruktor zur Initialisierung der Raum-Attribute - public Room(String RoomId, String roomName, int capacity) { - this.roomId = roomId; - //this.roomName = RoomName; - this.capacity = capacity; - } + public static sealed interface Command permits + Create, + Update, + Delete + {} - // Getter und Setter Methoden für die Attribute - //public String getRoomId() { - // return RoomId; - //} + public static record Create( + String roomName + ) implements Command {} - //public void setRoomId(String roomId) { - // this.id = RoomId; - //} + public static record Update( + Id<Room> id, + String roomName + ) implements Command{} - public String getRoomName() { - return roomName; - } + public static record Delete( + Id<Room> id + ) implements Command{} - public void setRoomName(String roomName) { - this.roomName = roomName; - } - - public int getCapacity() { - return capacity; - } - - public void setCapacity(int capacity) { - this.capacity = capacity; - } - - // toString Methode zur Darstellung des Raums als String - @Override - public String toString() { - return "Room{" + - "roomId='" + roomId + '\'' + - ", roomName='" + roomName + '\'' + - ", capacity=" + capacity + - '}'; - } - - // equals Methode zum Vergleichen von zwei Room Objekten - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Room room = (Room) o; - return capacity == room.capacity && - Objects.equals(roomId, room.roomId) && - Objects.equals(roomName, room.roomName); - } - // hashCode Methode zur Berechnung des Hashcodes eines Room Objekts - @Override - public int hashCode() { - return Objects.hash(roomId, roomName, capacity); - } } - -//public record Room( -// Id<Room> roomId, -// String roomName, -// Instant lastUpdate -//) { diff --git a/opp/core/src/main/java/RoomCommand.java b/opp/core/src/main/java/RoomCommand.java deleted file mode 100644 index 64b61b069287535d1df32231d151a2cef1404809..0000000000000000000000000000000000000000 --- a/opp/core/src/main/java/RoomCommand.java +++ /dev/null @@ -1,27 +0,0 @@ -public class RoomCommand { - private String roomName; - private int capacity; - - // Konstruktor zur Initialisierung der Kommando-Attribute - public RoomCommand(String roomName, int capacity) { - this.roomName = roomName; - this.capacity = capacity; - } - - // Getter und Setter Methoden für die Attribute - public String getRoomName() { - return roomName; - } - - public void setRoomName(String roomName) { - this.roomName = roomName; - } - - public int getCapacity() { - return capacity; - } - - public void setCapacity(int capacity) { - this.capacity = capacity; - } -} diff --git a/opp/core/src/main/java/RoomService.java b/opp/core/src/main/java/RoomService.java index 35eb0b2002c13e28dd4d8ccd673d1503ddfa5820..9d6b01fc451755c7a1932b770fb8c24623bb3284 100644 --- a/opp/core/src/main/java/RoomService.java +++ b/opp/core/src/main/java/RoomService.java @@ -1,9 +1,8 @@ -import java.util.List; +import java.util.Optional; public interface RoomService { - Room createRoom(RoomCommand command); // Methode zum Erstellen eines neuen Raums - Room updateRoom(String roomId, RoomCommand command); // Methode zum Aktualisieren eines Raums - void deleteRoom(String roomId); // Methode zum Löschen eines Raums - Room getRoomById(String roomId); // Methode zum Abrufen eines Raums nach ID - List<Room> getAllRooms(); // Methode zum Abrufen aller Räume + + Room process(Room.Command cmd) throws Exception; + + Optional<Room> findRoom(Id<Room> id); } diff --git a/opp/core/src/main/java/RoomServiceImpl.java b/opp/core/src/main/java/RoomServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..8c927895975eaab8bc85ea00f7469181ef3b3d26 --- /dev/null +++ b/opp/core/src/main/java/RoomServiceImpl.java @@ -0,0 +1,65 @@ +import java.time.Instant; +import java.util.Optional; + +public class RoomServiceImpl implements RoomService { + + private final Repository repo; + + public RoomServiceImpl(Repository repo) { + this.repo = repo; + } + + @Override + public Room process(Room.Command cmd) throws Exception { + return switch (cmd){ + case Room.Create cr -> create(cr); + case Room.Update up -> update(up); + case Room.Delete del -> delete(del); + }; + } + + public Room create(Room.Create cr) throws Exception { + Room room = new Room( + repo.roomId(), + cr.roomName(), + Instant.now()); + + repo.save(room); + + return room; + } + + public Optional<Room> findRoom(Id<Room> id){ + return repo.findRoom(id); + } + + public Room update(Room.Update up) throws Exception { + + Optional<Room> foundRoom = repo.findRoom(up.id()); + + Room room = null; + if (foundRoom.isPresent()) { + Room currentRoom = foundRoom.get(); + + room = new Room( + currentRoom.id(), + up.roomName(), + Instant.now()); + + repo.save(room); + } + + return room; + } + + private Room delete(Room.Delete del) throws Exception { + + Optional<Room> returnedRoom = repo.findRoom(del.id()); + + if(returnedRoom.isPresent()) { + return repo.deleteRoom(del.id()); + } + return null; + } +} + diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index 0888d713edbdaa4b98919d31f184d4b495b701fb..9b19fac04e400df8c864d1912e3390c2b3a8ebc9 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -110,6 +110,15 @@ class JDBCRepository implements Repository ); """; + private static final String CREATE_ROOM_TABLE = """ + CREATE TABLE IF NOT EXISTS room( + id VARCHAR(50) PRIMARY KEY, + roomName VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL + ); + """; + + //endregion @@ -126,6 +135,7 @@ class JDBCRepository implements Repository stmt.execute(CREATE_OPERATION_TABLE); stmt.execute(CREATE_OP_STAFF_IN_OPERATION_TEAM_TABLE); stmt.execute(CREATE_PREPARATIONNOTE_TABLE); + stmt.execute(CREATE_ROOM_TABLE); } catch (SQLException e){ throw new RuntimeException(e); @@ -924,6 +934,93 @@ class JDBCRepository implements Repository return null; } + @Override + public Id<Room> roomId() { + + var id = new Id<Room>(randomUUID().toString()); + + return findRoom(id).isEmpty() ? id : roomId(); + } + + public Optional<Room> findRoom(Id<Room> id){ + try ( + var result = + conn.createStatement() + .executeQuery("SELECT * FROM room WHERE id = " +sqlValue(id.value()) + ";") + ){ + return + result.next() ? + Optional.of(readRoomFromRow(result)) : + Optional.empty(); + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + private static Room readRoomFromRow(ResultSet rs) throws SQLException { + + return new Room( + new Id<>(rs.getString("id")), + rs.getString("roomName"), + rs.getTimestamp("lastUpdate").toInstant() + ); + } + + @Override + public void save(Room room) { + try ( + var stmt = conn.createStatement() + ){ + var sql = + findRoom(room.id()).isPresent() ? + updateSQL(room) : + insertSQL(room); + + stmt.executeUpdate(sql); + + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + private static String updateSQL(Room room){ + return + "UPDATE room SET " + + "roomName = " + sqlValue(room.roomName()) + "," + + "lastUpdate = " + sqlValue(room.lastUpdate()) + " " + + "WHERE id = " + sqlValue(room.id().value()) + ";"; + } + + private static String insertSQL(Room room){ + return + "INSERT INTO room(" + + "id,roomName,lastUpdate" + + ") VALUES (" + + sqlValue(room.id().value()) + "," + + sqlValue(room.roomName()) + "," + + sqlValue(room.lastUpdate()) + + ");"; + } + + @Override + public Room deleteRoom(Id<Room> id) { + var room = findRoom(id); + + if(room.isPresent()) { + var sql = "DELETE FROM room WHERE id =" + quoted(id.value()) + ";"; + + try { + conn.createStatement().executeUpdate(sql); + return room.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 67a6005a79a8ac0e3bc6bd91261d9a40bbc11900..8c29b66e51b2e024d1f4c33b74c5093af1c201e5 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -40,6 +40,10 @@ public final class Tests private static PreparationNoteService preparationNoteService; + private static Room testRoom = null; + + private static RoomService roomService; + @BeforeClass public static void init() throws Exception { @@ -101,6 +105,12 @@ public final class Tests Instant.now() ); + testRoom = new Room( + new Id<>("room1111"), + ("Test Raum"), + Instant.now() + ); + operationService = new OperationServiceImpl(repo); opStaffService = new OPStaffServiceImpl(repo); @@ -109,6 +119,8 @@ public final class Tests preparationNoteService = new PreparationNoteImpl(repo); + roomService = new RoomServiceImpl(repo); + } @Ignore @@ -234,6 +246,59 @@ public final class Tests throw new Exception(); } } + @Ignore + @Test + public void testCreateRoom() throws Exception{ + + Room.Create createCommand = new Room.Create( + "Neuen Raum erstellt mit Command" + ); + + Room createdRoom = roomService.process(createCommand); + + assertNotNull("Room should be created", createdRoom); + assertNotNull("Room ID should not be null", createdRoom.id()); + assertTrue( + repo.findRoom(createdRoom.id()).isPresent() + ); + } + @Ignore + @Test + public void testRoomUpdate() throws Exception { + // repo.save(testRoom); + Room.Update updateCommand = new Room.Update( + testRoom.id(), + ("COmmand wurde hier geupdaet!?!?!?!?") + ); + + Room updateRoom = roomService.process(updateCommand); + + Optional<Room> readRoom = repo.findRoom(updateRoom.id()); + + if (readRoom.isPresent()) { + assertNotEquals(readRoom.get().roomName(), testRoom.roomName()); + + assertEquals(readRoom.get().id(), testRoom.id()); + + assertNotEquals(readRoom.get().lastUpdate(), testRoom.lastUpdate()); + } else { + throw new Exception(); + } + } + + @Test + public void testDeleteRoom() throws Exception { + + // repo.save(testRoom); + + Room.Delete deleteCommand = new Room.Delete( + testRoom.id() + ); + + Room deleteRoom = roomService.process(deleteCommand); + + assertFalse(repo.findRoom(deleteRoom.id()).isPresent()); + } @Ignore @Test