Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] feat: 채팅방 목록 조회 시 최근 메시지를 같이 보여주는 API 생성 #735

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
public record ChatRoomDetailV2(
Long chatRoomId,
int memberCount,
String clubName,
String clubImageUrl,
String title,
String imageUrl,
String recentMessage,
LocalDateTime recentMessageCreatedAt
) {

public ChatRoomDetailV2(ChatRoom chatRoom, String clubName, String clubImageUrl,
public ChatRoomDetailV2(ChatRoom chatRoom, String title, String imageUrl,
String recentMessage, LocalDateTime recentMessageCreatedAt) {
this(
chatRoom.getId(),
chatRoom.countMembers(),
clubName,
clubImageUrl,
title,
imageUrl,
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍😊👍

recentMessage,
recentMessageCreatedAt
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,38 @@ public FindMyChatRoomResponse findMine(Long memberId) {
return new FindMyChatRoomResponse(member.getId(), chatRoomDetails);
}

public FindMyChatRoomResponseV2 findMineV2(Long memberId) {
Member member = memberRepository.getById(memberId);
List<ChatRoom> chatRooms = chatRoomRepository.findMine(memberId);
public FindMyChatRoomResponseV2 findMineV2(Long myMemberId) {
Member member = memberRepository.getById(myMemberId);
List<ChatRoom> chatRooms = chatRoomRepository.findMine(myMemberId);
List<ChatRoomDetailV2> chatRoomDetails = chatRooms.stream()
.map(this::toChatRoomDetail)
.map(chatRoom -> toChatRoomDetail(chatRoom, myMemberId))
.toList();
return new FindMyChatRoomResponseV2(member.getId(), chatRoomDetails);
}

private ChatRoomDetailV2 toChatRoomDetail(ChatRoom chatRoom) {
Optional<Club> club = clubRepository.findByChatRoomId(chatRoom.getId());
String clubName = club.map(c -> c.getTitle().getValue()).orElse("");
String clubImageUrl = club.map(Club::getImageUrl).orElse("");

private ChatRoomDetailV2 toChatRoomDetail(ChatRoom chatRoom, Long myMemberId) {
Optional<ChatMessage> recentMessage = chatMessageRepository.findRecentByChatRoomId(chatRoom.getId());
String content = recentMessage.map(ChatMessage::getContent).orElse(null);
LocalDateTime createdAt = recentMessage.map(ChatMessage::getCreatedAt).orElse(null);

return new ChatRoomDetailV2(chatRoom, clubName, clubImageUrl, content, createdAt);
if (chatRoom.isGroupChat()) {
Club club = clubRepository.findByChatRoomId(chatRoom.getId())
.orElseThrow(() -> new FriendoglyException("채팅방에 해당하는 모임이 존재하지 않습니다."));
return new ChatRoomDetailV2(chatRoom, club.getTitle().getValue(), club.getImageUrl(), content, createdAt);
}

if (chatRoom.isPrivateChat()) {
Optional<Member> otherMember = chatRoom.findMembers().stream()
.filter(member -> myMemberId.equals(member.getId()))
.findAny();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상대방 Member를 조회해야 하는데 자기 자신으로 filtering 하고 있습니다 🥸

Optional<Member> otherMember = chatRoom.findMembers()
                    .stream()
                    .filter(member -> !myMemberId.equals(member.getId()))
                    .findAny();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉....!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트의 부재가 크군요 테스트까지 작성했슴다! 😭

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 리뷰의 중요성 🥸🥸

String title = otherMember.map(member -> member.getName().getValue())
.orElse(null);
String imageUrl = otherMember.map(Member::getImageUrl)
.orElse(null);
return new ChatRoomDetailV2(chatRoom, title, imageUrl, content, createdAt);
}

throw new FriendoglyException("올바르지 않은 유형의 채팅방입니다.");
}

public List<FindChatRoomMembersInfoResponse> findMemberInfo(Long memberId, Long chatRoomId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ INSERT INTO chat_message (chat_room_id, message_type, member_id, content, create
.extracting(ChatRoomDetailV2::chatRoomId)
.containsExactly(chatRoom1.getId(), chatRoom2.getId()),
() -> assertThat(response.chatRooms())
.extracting(ChatRoomDetailV2::clubName)
.extracting(ChatRoomDetailV2::title)
.containsExactly("모임 제목1", "모임 제목2"),
() -> assertThat(response.chatRooms())
.extracting(ChatRoomDetailV2::memberCount)
.containsExactly(2, 2),
() -> assertThat(response.chatRooms())
.extracting(ChatRoomDetailV2::clubImageUrl)
.extracting(ChatRoomDetailV2::imageUrl)
.containsExactly("https://image.com", "https://image.com"),
() -> assertThat(response.chatRooms())
.extracting(ChatRoomDetailV2::recentMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void savePrivate() throws Exception {
.andExpect(status().isOk());
}

@DisplayName("내가 참여한 채팅방 목록 조회 - v1")
@DisplayName("내가 참여한 채팅방 목록 조회 (v1)")
@Test
void findMine() throws Exception {
ChatRoomDetail detail1 = new ChatRoomDetail(3L, 5, "신천동 소형견 모임", "https://image1.com/image.jpg");
Expand Down Expand Up @@ -162,9 +162,12 @@ void findMineV2() throws Exception {
fieldWithPath("data.myMemberId").description("나의 Member ID"),
fieldWithPath("data.chatRooms.[].chatRoomId").description("채팅방 ID"),
fieldWithPath("data.chatRooms.[].memberCount").description("채팅방 현재 인원"),
fieldWithPath("data.chatRooms.[].clubName").description("모임 이름 (모임 채팅방 아닌 경우 빈 문자열)"),
fieldWithPath("data.chatRooms.[].clubImageUrl").description("모임 이미지 URL (모임 채팅방 아닌 경우 빈 문자열)"),
fieldWithPath("data.chatRooms.[].recentMessage").description("가장 최근에 전송된 메시지 (채팅 하나도 없으면 null)"),
fieldWithPath("data.chatRooms.[].title").description(
"모임 이름 혹은 상대방 이름 (1대1 채팅방에서 상대방이 없으면 null)"),
fieldWithPath("data.chatRooms.[].imageUrl").description(
"모임 이미지 URL 혹은 상대방 프로필 사진 URL (nullable)"),
fieldWithPath("data.chatRooms.[].recentMessage").description(
"가장 최근에 전송된 메시지 (채팅 하나도 없으면 null)"),
fieldWithPath("data.chatRooms.[].recentMessageCreatedAt").description(
"가장 최근에 메시지가 전송된 시간 (채팅 하나도 없으면 null)")
)
Expand Down
Loading