feat: implemented a simplified chatroom-service
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chatroom / api / ChatroomController.java
1 package de.juplo.kafka.chatroom.api;
2
3 import de.juplo.kafka.chatroom.domain.Chatroom;
4 import de.juplo.kafka.chatroom.domain.Message;
5 import lombok.RequiredArgsConstructor;
6 import org.springframework.web.bind.annotation.*;
7
8 import java.time.Clock;
9 import java.time.LocalDateTime;
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.UUID;
14
15
16 @RestController
17 @RequiredArgsConstructor
18 public class ChatroomController
19 {
20   private final Map<UUID, Chatroom> chatrooms = new HashMap<>();
21   private final Clock clock;
22
23
24   @PostMapping("create")
25   public Chatroom create(@RequestBody String name)
26   {
27     Chatroom chatroom = new Chatroom(UUID.randomUUID(), name);
28     chatrooms.put(chatroom.getId(), chatroom);
29     return chatroom;
30   }
31
32   @GetMapping("list")
33   public Collection<Chatroom> list()
34   {
35     return chatrooms.values();
36   }
37
38   @PutMapping("post/{chatroomId}/{username}/{messageId}")
39   public MessageTo post(
40       @PathVariable UUID chatroomId,
41       @PathVariable String username,
42       @PathVariable UUID messageId,
43       @RequestBody String message)
44   {
45     return MessageTo.from(
46         chatrooms
47             .get(chatroomId)
48             .addMessage(
49                 messageId,
50                 LocalDateTime.now(clock),
51                 username,
52                 message));
53   }
54 }