diff --git a/src/main/java/toy/bookchat/bookchat/domain/chat/Sender.java b/src/main/java/toy/bookchat/bookchat/domain/chat/Sender.java index ede3be6c..29bf19c4 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chat/Sender.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chat/Sender.java @@ -7,20 +7,23 @@ @Getter public class Sender { - private Long id; - private String nickname; - private String profileImageUrl; - private Integer defaultProfileImageType; + private Long id; + private String nickname; + private String profileImageUrl; + private Integer defaultProfileImageType; - @Builder - private Sender(Long id, String nickname, String profileImageUrl, Integer defaultProfileImageType) { - this.id = id; - this.nickname = nickname; - this.profileImageUrl = profileImageUrl; - this.defaultProfileImageType = defaultProfileImageType; - } + @Builder + private Sender(Long id, String nickname, String profileImageUrl, Integer defaultProfileImageType) { + this.id = id; + this.nickname = nickname; + this.profileImageUrl = profileImageUrl; + this.defaultProfileImageType = defaultProfileImageType; + } - public static Sender from(User user) { - return new Sender(user.getId(), user.getNickname(), user.getProfileImageUrl(), user.getDefaultProfileImageType()); + public static Sender from(User user) { + if (user == null) { + return null; } + return new Sender(user.getId(), user.getNickname(), user.getProfileImageUrl(), user.getDefaultProfileImageType()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatReader.java b/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatReader.java index 101df118..21a9cc65 100644 --- a/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatReader.java +++ b/src/main/java/toy/bookchat/bookchat/domain/chat/service/ChatReader.java @@ -19,44 +19,44 @@ @Component public class ChatReader { - private final ChatRepository chatRepository; - private final UserReader userReader; - - public ChatReader(ChatRepository chatRepository, UserReader userReader) { - this.chatRepository = chatRepository; - this.userReader = userReader; - } - - public Chat readChat(Long userId, Long chatId) { - ChatEntity chatEntity = chatRepository.getUserChatRoomChat(chatId, userId).orElseThrow(ParticipantNotFoundException::new); - User user = userReader.readUser(userId); - - Sender sender = Sender.from(user); - - return Chat.builder() - .id(chatEntity.getId()) - .chatRoomId(chatEntity.getChatRoomId()) - .sender(sender) - .message(chatEntity.getMessage()) - .dispatchTime(chatEntity.getCreatedAt()) - .build(); - } - - public Slice readSlicedChat(Long userId, Long roomId, Long postCursorId, Pageable pageable) { - Slice slicedChatEntity = chatRepository.getChatRoomChats(roomId, postCursorId, pageable, userId); - Map chatIdUserIdMap = slicedChatEntity.stream().collect(Collectors.toMap(ChatEntity::getId, ChatEntity::getUserId)); - List userIds = slicedChatEntity.stream().map(ChatEntity::getUserId).collect(Collectors.toList()); - List users = userReader.readUsers(userIds); - Map userIdUserMap = users.stream().collect(Collectors.toMap(User::getId, identity())); - - return slicedChatEntity.map(sce -> - Chat.builder() - .id(sce.getId()) - .chatRoomId(sce.getChatRoomId()) - .sender(Sender.from(userIdUserMap.get(chatIdUserIdMap.get(sce.getId())))) - .message(sce.getMessage()) - .dispatchTime(sce.getCreatedAt()) - .build()); - } + private final ChatRepository chatRepository; + private final UserReader userReader; + + public ChatReader(ChatRepository chatRepository, UserReader userReader) { + this.chatRepository = chatRepository; + this.userReader = userReader; + } + + public Chat readChat(Long userId, Long chatId) { + ChatEntity chatEntity = chatRepository.getUserChatRoomChat(chatId, userId) + .orElseThrow(ParticipantNotFoundException::new); + User user = userReader.readUser(userId); + + Sender sender = Sender.from(user); + + return Chat.builder() + .id(chatEntity.getId()) + .chatRoomId(chatEntity.getChatRoomId()) + .sender(sender) + .message(chatEntity.getMessage()) + .dispatchTime(chatEntity.getCreatedAt()) + .build(); + } + + public Slice readSlicedChat(Long userId, Long roomId, Long postCursorId, Pageable pageable) { + Slice slicedChatEntity = chatRepository.getChatRoomChats(roomId, postCursorId, pageable, userId); + List userIds = slicedChatEntity.stream().map(ChatEntity::getUserId).collect(Collectors.toList()); + List users = userReader.readUsers(userIds); + Map userIdUserMap = users.stream().collect(Collectors.toMap(User::getId, identity())); + + return slicedChatEntity.map(sce -> + Chat.builder() + .id(sce.getId()) + .chatRoomId(sce.getChatRoomId()) + .sender(Sender.from(userIdUserMap.get(sce.getUserId()))) + .message(sce.getMessage()) + .dispatchTime(sce.getCreatedAt()) + .build()); + } } diff --git a/src/main/java/toy/bookchat/bookchat/localtest/rest/bookshelf/BookShelfAPI.http b/src/main/java/toy/bookchat/bookchat/localtest/rest/bookshelf/BookShelfAPI.http index a8df5778..a6a7da57 100644 --- a/src/main/java/toy/bookchat/bookchat/localtest/rest/bookshelf/BookShelfAPI.http +++ b/src/main/java/toy/bookchat/bookchat/localtest/rest/bookshelf/BookShelfAPI.http @@ -1,7 +1,6 @@ ### 서재에 책 등록 POST localhost:8080/v1/api/bookshelves -#Authorization: {{jwt_token}} -Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJCb29rQ2hhdCIsImRlZmF1bHRQcm9maWxlSW1hZ2VUeXBlIjoxLCJwcm92aWRlciI6Imdvb2dsZSIsInVzZXJOaWNrbmFtZSI6Iu2VnOudvOu0iTIyIiwidXNlck5hbWUiOiIxMDE3MzM1MTIzNDA3MjA5ODU1OTRnb29nbGUiLCJ1c2VyUHJvZmlsZUltYWdlVXJpIjoiaHR0cHM6Ly9ib29rY2hhdC1vcmlnaW5hbC5zMy5hcC1ub3J0aGVhc3QtMi5hbWF6b25hd3MuY29tL3VzZXJfcHJvZmlsZV9pbWFnZS8zMi05MC00MjAyN2Q1MzJjNDAtZTliNi00M2FiLTljYTMtNzViMDQ1OWE2YzBjLndlYnAiLCJ1c2VyUm9sZSI6IlJPTEVfVVNFUiIsImV4cCI6MTcyODEzNzUzNCwidXNlcklkIjoiMSIsImVtYWlsIjpudWxsfQ.2HpnEuxn2IqiOnCUJATGe2sY62KgFdHDEUZ6H0wf5Qc +Authorization: {{jwt_token}} Content-Type: application/json { diff --git a/src/main/java/toy/bookchat/bookchat/localtest/rest/chat/ChatAPI.http b/src/main/java/toy/bookchat/bookchat/localtest/rest/chat/ChatAPI.http index 57174fb4..5c04cc8e 100644 --- a/src/main/java/toy/bookchat/bookchat/localtest/rest/chat/ChatAPI.http +++ b/src/main/java/toy/bookchat/bookchat/localtest/rest/chat/ChatAPI.http @@ -1,2 +1,3 @@ ### 채팅 조회 GET localhost:8080/v1/api/chatrooms/1/chats?size=3 +Authorization: {{jwt_token}}