From 14c6a8c34de8b46b7f1655558727fc1276afb036 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 6 Jan 2023 00:44:44 +0100 Subject: [PATCH] refactor: Fixed return-types of the controller - Renamed and moved `ChatroomInfo` to `ChatroomTo` - Switched return-types from `Chatroom` to `ChatroomTo` --- .../chat/backend/ChatBackendApplication.java | 2 +- .../backend/api/ChatBackendController.java | 13 +++---- .../ChatroomInfo.java => api/ChatroomTo.java} | 8 ++--- .../kafka/chat/backend/domain/ChatHome.java | 5 +-- .../LocalJsonFilesStorageStrategy.java | 36 +++++++++---------- .../backend/persistence/StorageStrategy.java | 5 +-- 6 files changed, 36 insertions(+), 33 deletions(-) rename src/main/java/de/juplo/kafka/chat/backend/{persistence/ChatroomInfo.java => api/ChatroomTo.java} (58%) diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java index 37eeeee4..0700496e 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java @@ -21,7 +21,7 @@ public class ChatBackendApplication @PreDestroy public void onExit() { - storageStrategy.writeChatrooms(Flux.fromIterable(chatHome.list())); + storageStrategy.writeChatrooms(Flux.fromStream(chatHome.list())); } public static void main(String[] args) diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index 7d1655db..8fccaa9b 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -13,6 +13,7 @@ import java.time.LocalDateTime; import java.util.Collection; import java.util.Optional; import java.util.UUID; +import java.util.stream.Stream; @RestController @@ -24,21 +25,21 @@ public class ChatBackendController @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 list() + public Stream list() { - return chatHome.list(); + return chatHome.list().map(chatroom -> ChatroomTo.from(chatroom)); } @GetMapping("get/{chatroomId}") - public Optional get(@PathVariable UUID chatroomId) + public Optional get(@PathVariable UUID chatroomId) { - return chatHome.getChatroom(chatroomId); + return chatHome.getChatroom(chatroomId).map(chatroom -> ChatroomTo.from(chatroom)); } @PutMapping("put/{chatroomId}/{username}/{messageId}") diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/ChatroomInfo.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatroomTo.java similarity index 58% rename from src/main/java/de/juplo/kafka/chat/backend/persistence/ChatroomInfo.java rename to src/main/java/de/juplo/kafka/chat/backend/api/ChatroomTo.java index 6536175d..c4ad7848 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/ChatroomInfo.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatroomTo.java @@ -1,4 +1,4 @@ -package de.juplo.kafka.chat.backend.persistence; +package de.juplo.kafka.chat.backend.api; import de.juplo.kafka.chat.backend.domain.Chatroom; import lombok.Data; @@ -6,15 +6,15 @@ import lombok.Data; import java.util.UUID; @Data -public class ChatroomInfo +public class ChatroomTo { private UUID id; private String name; - public static ChatroomInfo from(Chatroom chatroom) + public static ChatroomTo from(Chatroom chatroom) { - ChatroomInfo info = new ChatroomInfo(); + ChatroomTo info = new ChatroomTo(); info.id = chatroom.getId(); info.name = chatroom.getName(); return info; diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java index 2b5152c9..d04bd738 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHome.java @@ -3,6 +3,7 @@ package de.juplo.kafka.chat.backend.domain; import lombok.RequiredArgsConstructor; import java.util.*; +import java.util.stream.Stream; @RequiredArgsConstructor @@ -24,8 +25,8 @@ public class ChatHome return Optional.ofNullable(chatrooms.get(id)); } - public Collection list() + public Stream list() { - return chatrooms.values(); + return chatrooms.values().stream(); } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategy.java index 40fa2bc4..a90e2ebd 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/LocalJsonFilesStorageStrategy.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonGenerator; 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; @@ -20,7 +21,6 @@ import java.nio.file.NoSuchFileException; 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; @@ -83,9 +83,9 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy { 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) { @@ -114,12 +114,12 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy if (parser.nextToken() != JsonToken.START_ARRAY) throw new IllegalStateException("Expected content to be an array"); - Sinks.Many many = Sinks.many().unicast().onBackpressureBuffer(); + Sinks.Many 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(); } @@ -127,10 +127,10 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy return many .asFlux() - .map(chatroomInfo -> + .map(chatroomTo -> { LinkedHashMap messages = - readMessages(chatroomInfo) + readMessages(chatroomTo) .collect(Collectors.toMap( Message::getKey, Function.identity(), @@ -143,7 +143,7 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy 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) @@ -158,10 +158,10 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy } @Override - public void writeMessages(ChatroomInfo chatroomInfo, Flux messageFlux) + public void writeMessages(ChatroomTo chatroomTo, Flux 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); @@ -217,10 +217,10 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy } @Override - public Flux readMessages(ChatroomInfo chatroomInfo) + public Flux 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 = @@ -249,7 +249,7 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy log.info( "{} does not exist - starting with empty chat for {}", path, - chatroomInfo); + chatroomTo); return Flux.empty(); } catch (IOException e) @@ -263,8 +263,8 @@ public class LocalJsonFilesStorageStrategy implements StorageStrategy 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")); } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java index 3c268ca6..38a9e98f 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java @@ -1,5 +1,6 @@ package de.juplo.kafka.chat.backend.persistence; +import de.juplo.kafka.chat.backend.api.ChatroomTo; import de.juplo.kafka.chat.backend.domain.Chatroom; import de.juplo.kafka.chat.backend.domain.Message; import reactor.core.publisher.Flux; @@ -9,6 +10,6 @@ public interface StorageStrategy { void writeChatrooms(Flux chatroomFlux); Flux readChatrooms(); - void writeMessages(ChatroomInfo chatroomInfo, Flux messageFlux); - Flux readMessages(ChatroomInfo chatroomInfo); + void writeMessages(ChatroomTo chatroomTo, Flux messageFlux); + Flux readMessages(ChatroomTo chatroomTo); } -- 2.20.1