import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
+import java.util.stream.Stream;
@RestController
@PostMapping("create")
- public Chatroom create(@RequestBody String name)
+ public ChatroomTo create(@RequestBody String name)
{
- return chatHome.createChatroom(name);
+ return ChatroomTo.from(chatHome.createChatroom(name));
}
@GetMapping("list")
- public Collection<Chatroom> list()
+ public Stream<ChatroomTo> list()
{
- return chatHome.list();
+ return chatHome.list().map(chatroom -> ChatroomTo.from(chatroom));
}
@GetMapping("get/{chatroomId}")
- public Optional<Chatroom> get(@PathVariable UUID chatroomId)
+ public Optional<ChatroomTo> get(@PathVariable UUID chatroomId)
{
- return chatHome.getChatroom(chatroomId);
+ return chatHome.getChatroom(chatroomId).map(chatroom -> ChatroomTo.from(chatroom));
}
@PutMapping("put/{chatroomId}/{username}/{messageId}")
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
+import de.juplo.kafka.chat.backend.api.ChatroomTo;
import de.juplo.kafka.chat.backend.api.MessageTo;
import de.juplo.kafka.chat.backend.domain.Chatroom;
import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.function.Function;
-import java.util.stream.Collector;
import java.util.stream.Collectors;
import static java.nio.file.StandardOpenOption.CREATE;
{
try
{
- ChatroomInfo chatroomInfo = ChatroomInfo.from(chatroom);
- generator.writeObject(chatroomInfo);
- writeMessages(chatroomInfo, chatroom.getMessages());
+ ChatroomTo chatroomTo = ChatroomTo.from(chatroom);
+ generator.writeObject(chatroomTo);
+ writeMessages(chatroomTo, chatroom.getMessages());
}
catch (IOException e)
{
if (parser.nextToken() != JsonToken.START_ARRAY)
throw new IllegalStateException("Expected content to be an array");
- Sinks.Many<ChatroomInfo> many = Sinks.many().unicast().onBackpressureBuffer();
+ Sinks.Many<ChatroomTo> many = Sinks.many().unicast().onBackpressureBuffer();
while (parser.nextToken() != JsonToken.END_ARRAY)
{
many
- .tryEmitNext(mapper.readValue(parser, ChatroomInfo.class))
+ .tryEmitNext(mapper.readValue(parser, ChatroomTo.class))
.orThrow();
}
return many
.asFlux()
- .map(chatroomInfo ->
+ .map(chatroomTo ->
{
LinkedHashMap<Message.MessageKey, Message> messages =
- readMessages(chatroomInfo)
+ readMessages(chatroomTo)
.collect(Collectors.toMap(
Message::getKey,
Function.identity(),
LinkedHashMap::new))
.block();
InMemoryPersistenceStrategy strategy = new InMemoryPersistenceStrategy(messages);
- return chatroomFactory.restoreChatroom(chatroomInfo.getId(), chatroomInfo.getName(), strategy);
+ return chatroomFactory.restoreChatroom(chatroomTo.getId(), chatroomTo.getName(), strategy);
});
}
catch (NoSuchFileException e)
}
@Override
- public void writeMessages(ChatroomInfo chatroomInfo, Flux<Message> messageFlux)
+ public void writeMessages(ChatroomTo chatroomTo, Flux<Message> messageFlux)
{
- Path path = chatroomPath(chatroomInfo);
- log.info("Writing messages for {} to {}", chatroomInfo, path);
+ Path path = chatroomPath(chatroomTo);
+ log.info("Writing messages for {} to {}", chatroomTo, path);
try
{
Files.createDirectories(storagePath);
}
@Override
- public Flux<Message> readMessages(ChatroomInfo chatroomInfo)
+ public Flux<Message> readMessages(ChatroomTo chatroomTo)
{
- Path path = chatroomPath(chatroomInfo);
- log.info("Reading messages for {} from {}", chatroomInfo, path);
+ Path path = chatroomPath(chatroomTo);
+ log.info("Reading messages for {} from {}", chatroomTo, path);
try
{
JsonParser parser =
log.info(
"{} does not exist - starting with empty chat for {}",
path,
- chatroomInfo);
+ chatroomTo);
return Flux.empty();
}
catch (IOException e)
return storagePath.resolve(Path.of(CHATROOMS_FILENAME));
}
- Path chatroomPath(ChatroomInfo chatroomInfo)
+ Path chatroomPath(ChatroomTo chatroomTo)
{
- return storagePath.resolve(Path.of(chatroomInfo.getId().toString() + ".json"));
+ return storagePath.resolve(Path.of(chatroomTo.getId().toString() + ".json"));
}
}