Skip to content

Commit

Permalink
revise: announcement chat filter
Browse files Browse the repository at this point in the history
  • Loading branch information
geneaky committed Oct 5, 2024
1 parent e89bed8 commit 958f3c7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 54 deletions.
29 changes: 16 additions & 13 deletions src/main/java/toy/bookchat/bookchat/domain/chat/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chat> readSlicedChat(Long userId, Long roomId, Long postCursorId, Pageable pageable) {
Slice<ChatEntity> slicedChatEntity = chatRepository.getChatRoomChats(roomId, postCursorId, pageable, userId);
Map<Long, Long> chatIdUserIdMap = slicedChatEntity.stream().collect(Collectors.toMap(ChatEntity::getId, ChatEntity::getUserId));
List<Long> userIds = slicedChatEntity.stream().map(ChatEntity::getUserId).collect(Collectors.toList());
List<User> users = userReader.readUsers(userIds);
Map<Long, User> 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<Chat> readSlicedChat(Long userId, Long roomId, Long postCursorId, Pageable pageable) {
Slice<ChatEntity> slicedChatEntity = chatRepository.getChatRoomChats(roomId, postCursorId, pageable, userId);
List<Long> userIds = slicedChatEntity.stream().map(ChatEntity::getUserId).collect(Collectors.toList());
List<User> users = userReader.readUsers(userIds);
Map<Long, User> 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());
}

}
Original file line number Diff line number Diff line change
@@ -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

{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
### 채팅 조회
GET localhost:8080/v1/api/chatrooms/1/chats?size=3
Authorization: {{jwt_token}}

0 comments on commit 958f3c7

Please sign in to comment.