feat: implemented a simplified chatroom-service
authorKai Moritz <kai@juplo.de>
Tue, 20 Dec 2022 21:25:42 +0000 (22:25 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 25 Dec 2022 00:09:04 +0000 (01:09 +0100)
src/main/java/de/juplo/kafka/chatroom/ChatroomApplication.java
src/main/java/de/juplo/kafka/chatroom/api/ChatroomController.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chatroom/api/MessageTo.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chatroom/domain/Chatroom.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chatroom/domain/Message.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chatroom/ChatroomApplicationTests.java

index ee6f7ee..287c9ee 100644 (file)
@@ -2,13 +2,23 @@ package de.juplo.kafka.chatroom;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+
+import java.time.Clock;
+
 
 @SpringBootApplication
 public class ChatroomApplication
 {
+       @Bean
+       public Clock clock()
+       {
+               return Clock.systemDefaultZone();
+       }
+
+
        public static void main(String[] args)
        {
                SpringApplication.run(ChatroomApplication.class, args);
        }
-
 }
diff --git a/src/main/java/de/juplo/kafka/chatroom/api/ChatroomController.java b/src/main/java/de/juplo/kafka/chatroom/api/ChatroomController.java
new file mode 100644 (file)
index 0000000..e3146a9
--- /dev/null
@@ -0,0 +1,54 @@
+package de.juplo.kafka.chatroom.api;
+
+import de.juplo.kafka.chatroom.domain.Chatroom;
+import de.juplo.kafka.chatroom.domain.Message;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.*;
+
+import java.time.Clock;
+import java.time.LocalDateTime;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+
+@RestController
+@RequiredArgsConstructor
+public class ChatroomController
+{
+  private final Map<UUID, Chatroom> chatrooms = new HashMap<>();
+  private final Clock clock;
+
+
+  @PostMapping("create")
+  public Chatroom create(@RequestBody String name)
+  {
+    Chatroom chatroom = new Chatroom(UUID.randomUUID(), name);
+    chatrooms.put(chatroom.getId(), chatroom);
+    return chatroom;
+  }
+
+  @GetMapping("list")
+  public Collection<Chatroom> list()
+  {
+    return chatrooms.values();
+  }
+
+  @PutMapping("post/{chatroomId}/{username}/{messageId}")
+  public MessageTo post(
+      @PathVariable UUID chatroomId,
+      @PathVariable String username,
+      @PathVariable UUID messageId,
+      @RequestBody String message)
+  {
+    return MessageTo.from(
+        chatrooms
+            .get(chatroomId)
+            .addMessage(
+                messageId,
+                LocalDateTime.now(clock),
+                username,
+                message));
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/chatroom/api/MessageTo.java b/src/main/java/de/juplo/kafka/chatroom/api/MessageTo.java
new file mode 100644 (file)
index 0000000..54ff7f7
--- /dev/null
@@ -0,0 +1,31 @@
+package de.juplo.kafka.chatroom.api;
+
+import de.juplo.kafka.chatroom.domain.Message;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+
+@Data
+@AllArgsConstructor
+public class MessageTo
+{
+  private UUID id;
+  private Long serialNumber;
+  private LocalDateTime timestamp;
+  private String user;
+  private String text;
+
+  public static MessageTo from(Message message)
+  {
+    return
+        new MessageTo(
+            message.getId(),
+            message.getSerialNumber(),
+            message.getTimestamp(),
+            message.getUser(),
+            message.getText());
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/chatroom/domain/Chatroom.java b/src/main/java/de/juplo/kafka/chatroom/domain/Chatroom.java
new file mode 100644 (file)
index 0000000..fd7d73a
--- /dev/null
@@ -0,0 +1,37 @@
+package de.juplo.kafka.chatroom.domain;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.time.LocalDateTime;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Stream;
+
+
+@RequiredArgsConstructor
+public class Chatroom
+{
+  @Getter
+  private final UUID id;
+  @Getter
+  private final String name;
+  private final List<Message> messages = new LinkedList<>();
+
+  synchronized public Message addMessage(
+      UUID id,
+      LocalDateTime timestamp,
+      String user,
+      String text)
+  {
+    Message message = new Message(id, (long)messages.size(), timestamp, user, text);
+    messages.add(message);
+    return message;
+  }
+
+  public Stream<Message> getMessages(long firstMessage)
+  {
+    return messages.stream().filter(message -> message.getSerialNumber() >= firstMessage);
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/chatroom/domain/Message.java b/src/main/java/de/juplo/kafka/chatroom/domain/Message.java
new file mode 100644 (file)
index 0000000..d93dc1b
--- /dev/null
@@ -0,0 +1,23 @@
+package de.juplo.kafka.chatroom.domain;
+
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.ToString;
+
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+
+@RequiredArgsConstructor
+@Getter
+@EqualsAndHashCode
+@ToString
+public class Message
+{
+  private final UUID id;
+  private final Long serialNumber;
+  private final LocalDateTime timestamp;
+  private final String user;
+  private final String text;
+}
index d2e0d3b..9b23ad0 100644 (file)
@@ -6,9 +6,8 @@ import org.springframework.boot.test.context.SpringBootTest;
 @SpringBootTest
 class ChatroomApplicationTests
 {
-
        @Test
-       void contextLoads() {
+       void contextLoads()
+       {
        }
-
 }