package de.juplo.kafka.chat.backend.api;
-import de.juplo.kafka.chat.backend.domain.ChatHome;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.domain.ChatHomeService;
import de.juplo.kafka.chat.backend.domain.ChatRoomData;
-import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
-import lombok.RequiredArgsConstructor;
+import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.UUID;
+import java.util.logging.Level;
@RestController
-@RequiredArgsConstructor
public class ChatBackendController
{
- private final ChatHome chatHome;
+ private final ChatHomeService chatHomeService;
private final StorageStrategy storageStrategy;
+ private final String loggingCategory = ChatBackendController.class.getSimpleName();
+ private final Level loggingLevel;
+ private final boolean showOperatorLine;
+
+
+ public ChatBackendController(
+ ChatHomeService chatHomeService,
+ StorageStrategy storageStrategy,
+ ChatBackendProperties properties)
+ {
+ this.chatHomeService = chatHomeService;
+ this.storageStrategy = storageStrategy;
+ this.loggingLevel = properties.getProjectreactor().getLoggingLevel();
+ this.showOperatorLine = properties.getProjectreactor().isShowOperatorLine();
+ }
@PostMapping("create")
public Mono<ChatRoomInfoTo> create(@RequestBody String name)
{
UUID chatRoomId = UUID.randomUUID();
- return chatHome
+ return chatHomeService
.createChatRoom(chatRoomId, name)
.map(ChatRoomInfoTo::from);
}
@GetMapping("list")
public Flux<ChatRoomInfoTo> list()
{
- return chatHome
+ return chatHomeService
.getChatRoomInfo()
.map(chatroomInfo -> ChatRoomInfoTo.from(chatroomInfo));
}
@GetMapping("{chatRoomId}/list")
public Flux<MessageTo> list(@PathVariable UUID chatRoomId)
{
- return chatHome
+ return chatHomeService
.getChatRoomData(chatRoomId)
.flatMapMany(chatRoomData -> chatRoomData
.getMessages()
@GetMapping("{chatRoomId}")
public Mono<ChatRoomInfoTo> get(@PathVariable UUID chatRoomId)
{
- return chatHome
+ return chatHomeService
.getChatRoomInfo(chatRoomId)
.map(chatRoomInfo -> ChatRoomInfoTo.from(chatRoomInfo));
}
@RequestBody String text)
{
return
- chatHome
+ chatHomeService
.getChatRoomData(chatRoomId)
.flatMap(chatRoomData -> put(chatRoomData, username, messageId, text));
}
@PathVariable Long messageId)
{
return
- chatHome
+ chatHomeService
.getChatRoomData(chatRoomId)
.flatMap(chatRoomData -> get(chatRoomData, username, messageId));
}
@GetMapping(path = "{chatRoomId}/listen")
public Flux<ServerSentEvent<MessageTo>> listen(@PathVariable UUID chatRoomId)
{
- return chatHome
+ return chatHomeService
.getChatRoomData(chatRoomId)
.flatMapMany(chatRoomData -> listen(chatRoomData));
}
{
return chatRoomData
.listen()
- .log()
+ .log(
+ loggingCategory,
+ loggingLevel,
+ showOperatorLine)
.map(message -> MessageTo.from(message))
.map(messageTo ->
ServerSentEvent
.build());
}
+ @GetMapping("/shards")
+ public Mono<String[]> getShardOwners()
+ {
+ return chatHomeService.getShardOwners();
+ }
+
@PostMapping("/store")
public void store()
{
- storageStrategy.write(chatHome);
+ storageStrategy
+ .write(chatHomeService)
+ .subscribe();
}
}