Skip to content

Commit

Permalink
feat/chatroom-dto-add-lastChatCreated: 채팅방 GET 요청 시 마지막 채팅 시간 DTO에 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyouung committed Jan 5, 2025
1 parent 98d34ba commit 447cf4a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/dife/api/model/dto/ChatroomResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface ChatroomResponseDto {
Set<MemberRestrictedResponseDto> getMembers();

String getLastChat();

LocalDateTime getLastChatCreated();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ public class GroupChatroomResponseDto implements ChatroomResponseDto {
private Boolean isEntered;

private String lastChat;

private LocalDateTime lastChatCreated;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public class SingleChatroomResponseDto implements ChatroomResponseDto {
private Long unreadChatsCount = 0L;

private String lastChat;

private LocalDateTime lastChatCreated;
}
17 changes: 17 additions & 0 deletions src/main/java/com/dife/api/service/ChatroomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public SingleChatroomResponseDto getSingleChatroom(Long id, String memberEmail)

changeChatroomMemberToMemberSet(chatroom, responseDto);
responseDto.setLastChat(getLastChat(chatroom, member));
responseDto.setLastChatCreated(getLastChatCreated(chatroom, member));
responseDto.setUnreadChatsCount(getChatCounts(chatroomMember));
return responseDto;
}
Expand Down Expand Up @@ -475,6 +476,22 @@ private String getLastChat(Chatroom chatroom, Member member) {
return chats.isEmpty() ? "" : chats.get(0).getMessage();
}

private LocalDateTime getLastChatCreated(Chatroom chatroom, Member member) {
ChatroomMember chatroomMember =
chatroomMemberRepository
.findByChatroomAndMember(chatroom, member)
.orElseThrow(ChatroomNotFoundException::new);

LocalDateTime last_joined_at = chatroomMember.getLastJoinedAt();

List<Chat> lastChats = chatroom.getChats().stream()
.filter(chat -> chat.getCreated().isAfter(last_joined_at))
.sorted(Comparator.comparing(Chat::getCreated).reversed())
.toList();

return lastChats.isEmpty() ? null : lastChats.get(0).getCreated();
}

public GroupChatroomResponseDto getGroupChatroom(Long id, String memberEmail) {

Chatroom chatroom = chatroomRepository.findById(id).orElseThrow(ChatroomNotFoundException::new);
Expand Down

0 comments on commit 447cf4a

Please sign in to comment.