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);
}
-
}
--- /dev/null
+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));
+ }
+}
--- /dev/null
+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());
+ }
+}
--- /dev/null
+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);
+ }
+}
--- /dev/null
+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;
+}
@SpringBootTest
class ChatroomApplicationTests
{
-
@Test
- void contextLoads() {
+ void contextLoads()
+ {
}
-
}