Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -35,4 +35,6 @@ SELECT COUNT(cm) FROM ChatMessage cm
long countUnreadMessages(@Param("chatRoomId") long chatRoomId, @Param("userId") long userId);

boolean existsByMentoringId(long mentoringId);

List<ChatRoom> findAllByMentoringIdIn(List<Long> mentoringIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ public record MentoringForMenteeResponse(
String profileImageUrl,
String nickname,
boolean isChecked,
ZonedDateTime createdAt
ZonedDateTime createdAt,
Long chatRoomId
) {

public static MentoringForMenteeResponse of(Mentoring mentoring, SiteUser partner) {
public static MentoringForMenteeResponse of(Mentoring mentoring, SiteUser partner, Long chatRoomId) {
return new MentoringForMenteeResponse(
mentoring.getId(),
partner.getProfileImageUrl(),
partner.getNickname(),
mentoring.getCheckedAtByMentee() != null,
mentoring.getCreatedAt()
mentoring.getCreatedAt(),
chatRoomId
);
Comment on lines +16 to 24
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

chatRoomId을 Long로 선언하여, null 의 가능성을 알려줍니다.
멘토링이 승인되지 않은 상태이면, 채팅방이 생기지 않아 chatRoomId가 null 이 됩니다.

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.solidconnection.mentor.dto;

import com.example.solidconnection.common.VerifyStatus;
import com.example.solidconnection.mentor.domain.Mentoring;
import com.example.solidconnection.siteuser.domain.SiteUser;
import java.time.ZonedDateTime;
Expand All @@ -9,7 +10,7 @@ public record MentoringForMentorResponse(
String profileImageUrl,
String nickname,
boolean isChecked,
boolean isConfirmed,
VerifyStatus verifyStatus,
ZonedDateTime createdAt
) {

Expand All @@ -19,7 +20,7 @@ public static MentoringForMentorResponse of(Mentoring mentoring, SiteUser partne
partner.getProfileImageUrl(),
partner.getNickname(),
mentoring.getCheckedAtByMentor() != null,
mentoring.getConfirmedAt() != null,
mentoring.getVerifyStatus(),
mentoring.getCreatedAt()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_NOT_FOUND;

import com.example.solidconnection.chat.domain.ChatRoom;
import com.example.solidconnection.chat.repository.ChatRoomRepository;
import com.example.solidconnection.common.VerifyStatus;
import com.example.solidconnection.common.dto.SliceResponse;
import com.example.solidconnection.common.exception.CustomException;
Expand All @@ -17,7 +19,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -33,6 +34,7 @@ public class MentoringQueryService {
private final MentoringRepository mentoringRepository;
private final MentorRepository mentorRepository;
private final SiteUserRepository siteUserRepository;
private final ChatRoomRepository chatRoomRepository;

@Transactional(readOnly = true)
public SliceResponse<MentoringForMenteeResponse> getMentoringsForMentee(
Expand All @@ -47,15 +49,30 @@ public SliceResponse<MentoringForMenteeResponse> getMentoringsForMentee(
mentoringSlice.toList(),
Mentoring::getMentorId
);
Map<Long, Long> mentoringIdToChatRoomId = mapMentoringIdToChatRoomIdWithBatchQuery(mentoringSlice.getContent());

List<MentoringForMenteeResponse> content = new ArrayList<>();
for (Entry<Mentoring, SiteUser> entry : mentoringToPartnerUser.entrySet()) {
content.add(MentoringForMenteeResponse.of(entry.getKey(), entry.getValue()));
for (Mentoring mentoring : mentoringSlice) {
content.add(MentoringForMenteeResponse.of(
mentoring,
mentoringToPartnerUser.get(mentoring),
mentoringIdToChatRoomId.get(mentoring.getId())
));
}

return SliceResponse.of(content, mentoringSlice);
}

// N+1 을 해결하면서 멘토링의 채팅방 정보 조회
private Map<Long, Long> mapMentoringIdToChatRoomIdWithBatchQuery(List<Mentoring> mentorings) {
List<Long> mentoringIds = mentorings.stream()
.map(Mentoring::getId)
.distinct()
.toList();
List<ChatRoom> chatRooms = chatRoomRepository.findAllByMentoringIdIn(mentoringIds);
return chatRooms.stream()
.collect(Collectors.toMap(ChatRoom::getMentoringId, ChatRoom::getId));
}
Comment on lines +65 to +74
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

멘토링 각각에 대한 채팅방 ID를 찾는 방법은 여러가지가 있을 것 같은데..
일단은 기존의 방식대로 구현했습니다😅


@Transactional(readOnly = true)
public SliceResponse<MentoringForMentorResponse> getMentoringsForMentor(long siteUserId, Pageable pageable) {
Mentor mentor = mentorRepository.findBySiteUserId(siteUserId)
Expand All @@ -68,8 +85,8 @@ public SliceResponse<MentoringForMentorResponse> getMentoringsForMentor(long sit
);

List<MentoringForMentorResponse> content = new ArrayList<>();
for (Entry<Mentoring, SiteUser> entry : mentoringToPartnerUser.entrySet()) {
content.add(MentoringForMentorResponse.of(entry.getKey(), entry.getValue()));
for (Mentoring mentoring : mentoringSlice) {
content.add(MentoringForMentorResponse.of(mentoring, mentoringToPartnerUser.get(mentoring)));
}

return SliceResponse.of(content, mentoringSlice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public class ChatRoomFixture {
.isGroup(isGroup)
.create();
}

public ChatRoom 멘토링_채팅방(long mentoringId) {
return chatRoomFixtureBuilder.chatRoom()
.mentoringId(mentoringId)
.isGroup(false)
.create();
}
Comment on lines +18 to +24
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

"멘토링"으로 생긴 채팅방이 필요해서 픽스쳐 추가했는데,
제가 채팅 부분을 잘 아는게 아니라... 혹시 실수한 부분이 있다면 알려주세요!

Copy link
Member

Choose a reason for hiding this comment

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

구현하신 것처럼 mentoringId 만 추가하면 될 듯 합니다 !

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ChatRoomFixtureBuilder {
private final ChatRoomRepository chatRoomRepository;

private boolean isGroup;
private Long mentoringId;

public ChatRoomFixtureBuilder chatRoom() {
return new ChatRoomFixtureBuilder(chatRoomRepository);
Expand All @@ -22,8 +23,13 @@ public ChatRoomFixtureBuilder isGroup(boolean isGroup) {
return this;
}

public ChatRoomFixtureBuilder mentoringId(long mentoringId) {
this.mentoringId = mentoringId;
return this;
}

public ChatRoom create() {
ChatRoom chatRoom = new ChatRoom(isGroup);
ChatRoom chatRoom = new ChatRoom(mentoringId, isGroup);
return chatRoomRepository.save(chatRoom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;

import com.example.solidconnection.chat.domain.ChatRoom;
import com.example.solidconnection.chat.fixture.ChatRoomFixture;
import com.example.solidconnection.common.VerifyStatus;
import com.example.solidconnection.common.dto.SliceResponse;
import com.example.solidconnection.common.exception.CustomException;
Expand Down Expand Up @@ -45,6 +47,9 @@ class MentoringQueryServiceTest {
@Autowired
private MentoringRepository mentoringRepository;

@Autowired
private ChatRoomFixture chatRoomFixture;

private SiteUser mentorUser1, mentorUser2;
private SiteUser menteeUser1, menteeUser2, menteeUser3;
private Mentor mentor1, mentor2, mentor3;
Expand Down Expand Up @@ -146,21 +151,23 @@ class 멘티의_멘토링_목록_조회_테스트 {
}

@Test
void 승인된_멘토링_목록을_조회한다() {
void 승인된_멘토링_목록과_대응하는_채팅방을_조회한다() {
// given
Mentoring mentoring1 = mentoringFixture.승인된_멘토링(mentor1.getId(), menteeUser1.getId());
Mentoring mentoring2 = mentoringFixture.승인된_멘토링(mentor2.getId(), menteeUser1.getId());
ChatRoom mentoringChatRoom1 = chatRoomFixture.멘토링_채팅방(mentoring1.getId());
ChatRoom mentoringChatRoom2 = chatRoomFixture.멘토링_채팅방(mentoring2.getId());
mentoringFixture.대기중_멘토링(mentor3.getId(), menteeUser1.getId());

// when
SliceResponse<MentoringForMenteeResponse> response = mentoringQueryService.getMentoringsForMentee(
menteeUser1.getId(), VerifyStatus.APPROVED, pageable);

// then
assertThat(response.content()).extracting(MentoringForMenteeResponse::mentoringId)
assertThat(response.content()).extracting(MentoringForMenteeResponse::mentoringId, MentoringForMenteeResponse::chatRoomId)
.containsExactlyInAnyOrder(
mentoring1.getId(),
mentoring2.getId()
tuple(mentoring1.getId(), mentoringChatRoom1.getId()),
tuple(mentoring2.getId(), mentoringChatRoom2.getId())
);
}

Expand Down
Loading