From: Kai Moritz Date: Tue, 20 Dec 2022 21:25:42 +0000 (+0100) Subject: feat: implemented a simplified chatroom-service X-Git-Tag: wip~106 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=a8164a477f0aa94f5c6e21dcf5c2ef8a8627402b;p=demos%2Fkafka%2Fchat feat: implemented a simplified chatroom-service --- diff --git a/src/main/java/de/juplo/kafka/chatroom/ChatroomApplication.java b/src/main/java/de/juplo/kafka/chatroom/ChatroomApplication.java index ee6f7ee4..287c9ee0 100644 --- a/src/main/java/de/juplo/kafka/chatroom/ChatroomApplication.java +++ b/src/main/java/de/juplo/kafka/chatroom/ChatroomApplication.java @@ -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 index 00000000..e3146a9d --- /dev/null +++ b/src/main/java/de/juplo/kafka/chatroom/api/ChatroomController.java @@ -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 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 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 index 00000000..54ff7f70 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chatroom/api/MessageTo.java @@ -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 index 00000000..fd7d73aa --- /dev/null +++ b/src/main/java/de/juplo/kafka/chatroom/domain/Chatroom.java @@ -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 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 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 index 00000000..d93dc1be --- /dev/null +++ b/src/main/java/de/juplo/kafka/chatroom/domain/Message.java @@ -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; +} diff --git a/src/test/java/de/juplo/kafka/chatroom/ChatroomApplicationTests.java b/src/test/java/de/juplo/kafka/chatroom/ChatroomApplicationTests.java index d2e0d3bc..9b23ad0f 100644 --- a/src/test/java/de/juplo/kafka/chatroom/ChatroomApplicationTests.java +++ b/src/test/java/de/juplo/kafka/chatroom/ChatroomApplicationTests.java @@ -6,9 +6,8 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class ChatroomApplicationTests { - @Test - void contextLoads() { + void contextLoads() + { } - }