package de.juplo.kafka.chat.backend.api;
import de.juplo.kafka.chat.backend.domain.ChatHome;
-import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
+import de.juplo.kafka.chat.backend.domain.UnknownChatroomException;
import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
import lombok.RequiredArgsConstructor;
-import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
-import java.time.Clock;
-import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;
public class ChatBackendController
{
private final ChatHome chatHome;
- private final Clock clock;
private final StorageStrategy storageStrategy;
@PostMapping("create")
- public ChatroomTo create(@RequestBody String name)
+ public ChatRoomTo create(@RequestBody String name)
{
- return ChatroomTo.from(chatHome.createChatroom(name));
+ return ChatRoomTo.from(chatHome.createChatroom(name));
}
@GetMapping("list")
- public Stream<ChatroomTo> list()
+ public Stream<ChatRoomTo> list()
{
- return chatHome.list().map(chatroom -> ChatroomTo.from(chatroom));
+ return chatHome.list().map(chatroom -> ChatRoomTo.from(chatroom));
}
@GetMapping("list/{chatroomId}")
}
@GetMapping("get/{chatroomId}")
- public Optional<ChatroomTo> get(@PathVariable UUID chatroomId)
+ public Optional<ChatRoomTo> get(@PathVariable UUID chatroomId)
{
- return chatHome.getChatroom(chatroomId).map(chatroom -> ChatroomTo.from(chatroom));
+ return chatHome.getChatroom(chatroomId).map(chatroom -> ChatRoomTo.from(chatroom));
}
@PutMapping("put/{chatroomId}/{username}/{messageId}")
}
public Mono<MessageTo> put(
- Chatroom chatroom,
+ ChatRoom chatroom,
String username,
Long messageId,
String text)
chatroom
.addMessage(
messageId,
- LocalDateTime.now(clock),
username,
text)
.switchIfEmpty(chatroom.getMessage(username, messageId))
}
private Mono<MessageTo> get(
- Chatroom chatroom,
+ ChatRoom chatroom,
String username,
Long messageId)
{
.orElseThrow(() -> new UnknownChatroomException(chatroomId));
}
- private Flux<ServerSentEvent<MessageTo>> listen(Chatroom chatroom)
+ private Flux<ServerSentEvent<MessageTo>> listen(ChatRoom chatroom)
{
return chatroom
.listen()