Skip to content

Commit 44184e2

Browse files
authored
refactor: 멘티의 멘토링 목록 조회 시 연관된 채팅방 ID를 함께 응답하도록 (#465)
* refactor: 멘티의 멘토링 목록 조회 시, chatRoomId를 포함하여 응답하도록 * test: 멘토링 채팅방 픽스쳐 추가 * test: 멘티의 멘토링 목록 조회 시 채팅방 ID 포함 테스트 추가 * refactor: 멘토링 조회 순서를 보장하도록 - map이 아니라 순서가 보장되는 slice를 기준으로 순회하며 응답을 생성하도록
1 parent 35e979a commit 44184e2

6 files changed

Lines changed: 55 additions & 14 deletions

File tree

src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ SELECT COUNT(cm) FROM ChatMessage cm
3535
long countUnreadMessages(@Param("chatRoomId") long chatRoomId, @Param("userId") long userId);
3636

3737
boolean existsByMentoringId(long mentoringId);
38+
39+
List<ChatRoom> findAllByMentoringIdIn(List<Long> mentoringIds);
3840
}

src/main/java/com/example/solidconnection/mentor/dto/MentoringForMenteeResponse.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ public record MentoringForMenteeResponse(
99
String profileImageUrl,
1010
String nickname,
1111
boolean isChecked,
12-
ZonedDateTime createdAt
12+
ZonedDateTime createdAt,
13+
Long chatRoomId
1314
) {
1415

15-
public static MentoringForMenteeResponse of(Mentoring mentoring, SiteUser partner) {
16+
public static MentoringForMenteeResponse of(Mentoring mentoring, SiteUser partner, Long chatRoomId) {
1617
return new MentoringForMenteeResponse(
1718
mentoring.getId(),
1819
partner.getProfileImageUrl(),
1920
partner.getNickname(),
2021
mentoring.getCheckedAtByMentee() != null,
21-
mentoring.getCreatedAt()
22+
mentoring.getCreatedAt(),
23+
chatRoomId
2224
);
2325
}
2426
}

src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
import com.example.solidconnection.chat.domain.ChatRoom;
6+
import com.example.solidconnection.chat.repository.ChatRoomRepository;
57
import com.example.solidconnection.common.VerifyStatus;
68
import com.example.solidconnection.common.dto.SliceResponse;
79
import com.example.solidconnection.common.exception.CustomException;
@@ -17,7 +19,6 @@
1719
import java.util.ArrayList;
1820
import java.util.List;
1921
import java.util.Map;
20-
import java.util.Map.Entry;
2122
import java.util.function.Function;
2223
import java.util.stream.Collectors;
2324
import lombok.RequiredArgsConstructor;
@@ -33,6 +34,7 @@ public class MentoringQueryService {
3334
private final MentoringRepository mentoringRepository;
3435
private final MentorRepository mentorRepository;
3536
private final SiteUserRepository siteUserRepository;
37+
private final ChatRoomRepository chatRoomRepository;
3638

3739
@Transactional(readOnly = true)
3840
public SliceResponse<MentoringForMenteeResponse> getMentoringsForMentee(
@@ -47,15 +49,30 @@ public SliceResponse<MentoringForMenteeResponse> getMentoringsForMentee(
4749
mentoringSlice.toList(),
4850
Mentoring::getMentorId
4951
);
52+
Map<Long, Long> mentoringIdToChatRoomId = mapMentoringIdToChatRoomIdWithBatchQuery(mentoringSlice.getContent());
5053

5154
List<MentoringForMenteeResponse> content = new ArrayList<>();
52-
for (Entry<Mentoring, SiteUser> entry : mentoringToPartnerUser.entrySet()) {
53-
content.add(MentoringForMenteeResponse.of(entry.getKey(), entry.getValue()));
55+
for (Mentoring mentoring : mentoringSlice) {
56+
content.add(MentoringForMenteeResponse.of(
57+
mentoring,
58+
mentoringToPartnerUser.get(mentoring),
59+
mentoringIdToChatRoomId.get(mentoring.getId())
60+
));
5461
}
55-
5662
return SliceResponse.of(content, mentoringSlice);
5763
}
5864

65+
// N+1 을 해결하면서 멘토링의 채팅방 정보 조회
66+
private Map<Long, Long> mapMentoringIdToChatRoomIdWithBatchQuery(List<Mentoring> mentorings) {
67+
List<Long> mentoringIds = mentorings.stream()
68+
.map(Mentoring::getId)
69+
.distinct()
70+
.toList();
71+
List<ChatRoom> chatRooms = chatRoomRepository.findAllByMentoringIdIn(mentoringIds);
72+
return chatRooms.stream()
73+
.collect(Collectors.toMap(ChatRoom::getMentoringId, ChatRoom::getId));
74+
}
75+
5976
@Transactional(readOnly = true)
6077
public SliceResponse<MentoringForMentorResponse> getMentoringsForMentor(long siteUserId, Pageable pageable) {
6178
Mentor mentor = mentorRepository.findBySiteUserId(siteUserId)
@@ -68,8 +85,8 @@ public SliceResponse<MentoringForMentorResponse> getMentoringsForMentor(long sit
6885
);
6986

7087
List<MentoringForMentorResponse> content = new ArrayList<>();
71-
for (Entry<Mentoring, SiteUser> entry : mentoringToPartnerUser.entrySet()) {
72-
content.add(MentoringForMentorResponse.of(entry.getKey(), entry.getValue()));
88+
for (Mentoring mentoring : mentoringSlice) {
89+
content.add(MentoringForMentorResponse.of(mentoring, mentoringToPartnerUser.get(mentoring)));
7390
}
7491

7592
return SliceResponse.of(content, mentoringSlice);

src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixture.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ public class ChatRoomFixture {
1515
.isGroup(isGroup)
1616
.create();
1717
}
18+
19+
public ChatRoom 멘토링_채팅방(long mentoringId) {
20+
return chatRoomFixtureBuilder.chatRoom()
21+
.mentoringId(mentoringId)
22+
.isGroup(false)
23+
.create();
24+
}
1825
}

src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixtureBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ChatRoomFixtureBuilder {
1212
private final ChatRoomRepository chatRoomRepository;
1313

1414
private boolean isGroup;
15+
private Long mentoringId;
1516

1617
public ChatRoomFixtureBuilder chatRoom() {
1718
return new ChatRoomFixtureBuilder(chatRoomRepository);
@@ -22,8 +23,13 @@ public ChatRoomFixtureBuilder isGroup(boolean isGroup) {
2223
return this;
2324
}
2425

26+
public ChatRoomFixtureBuilder mentoringId(long mentoringId) {
27+
this.mentoringId = mentoringId;
28+
return this;
29+
}
30+
2531
public ChatRoom create() {
26-
ChatRoom chatRoom = new ChatRoom(isGroup);
32+
ChatRoom chatRoom = new ChatRoom(mentoringId, isGroup);
2733
return chatRoomRepository.save(chatRoom);
2834
}
2935
}

src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.assertj.core.api.Assertions.assertThatCode;
55
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
66

7+
import com.example.solidconnection.chat.domain.ChatRoom;
8+
import com.example.solidconnection.chat.fixture.ChatRoomFixture;
79
import com.example.solidconnection.common.VerifyStatus;
810
import com.example.solidconnection.common.dto.SliceResponse;
911
import com.example.solidconnection.common.exception.CustomException;
@@ -45,6 +47,9 @@ class MentoringQueryServiceTest {
4547
@Autowired
4648
private MentoringRepository mentoringRepository;
4749

50+
@Autowired
51+
private ChatRoomFixture chatRoomFixture;
52+
4853
private SiteUser mentorUser1, mentorUser2;
4954
private SiteUser menteeUser1, menteeUser2, menteeUser3;
5055
private Mentor mentor1, mentor2, mentor3;
@@ -146,21 +151,23 @@ class 멘티의_멘토링_목록_조회_테스트 {
146151
}
147152

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

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

159166
// then
160-
assertThat(response.content()).extracting(MentoringForMenteeResponse::mentoringId)
167+
assertThat(response.content()).extracting(MentoringForMenteeResponse::mentoringId, MentoringForMenteeResponse::chatRoomId)
161168
.containsExactlyInAnyOrder(
162-
mentoring1.getId(),
163-
mentoring2.getId()
169+
tuple(mentoring1.getId(), mentoringChatRoom1.getId()),
170+
tuple(mentoring2.getId(), mentoringChatRoom2.getId())
164171
);
165172
}
166173

0 commit comments

Comments
 (0)