From 05e8275b4ce6c1e1f4ec3c3481d562453877606b Mon Sep 17 00:00:00 2001 From: nayonsoso Date: Mon, 18 Aug 2025 01:49:22 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=EB=A9=98=ED=8B=B0=EC=9D=98=20?= =?UTF-8?q?=EB=A9=98=ED=86=A0=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=8B=9C,=20chatRoomId=EB=A5=BC=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=ED=95=98=EC=97=AC=20=EC=9D=91=EB=8B=B5=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/repository/ChatRoomRepository.java | 2 ++ .../dto/MentoringForMenteeResponse.java | 8 ++++--- .../dto/MentoringForMentorResponse.java | 5 ++-- .../mentor/service/MentoringQueryService.java | 24 ++++++++++++++++--- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java b/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java index ad815dbe1..cdb0dbfa6 100644 --- a/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java +++ b/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java @@ -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 findAllByMentoringIdIn(List mentoringIds); } diff --git a/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMenteeResponse.java b/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMenteeResponse.java index 7f2591555..65e1e0120 100644 --- a/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMenteeResponse.java +++ b/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMenteeResponse.java @@ -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 ); } } diff --git a/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMentorResponse.java b/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMentorResponse.java index 54622d821..8a41fba84 100644 --- a/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMentorResponse.java +++ b/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMentorResponse.java @@ -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; @@ -9,7 +10,7 @@ public record MentoringForMentorResponse( String profileImageUrl, String nickname, boolean isChecked, - boolean isConfirmed, + VerifyStatus verifyStatus, ZonedDateTime createdAt ) { @@ -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() ); } diff --git a/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java b/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java index a102b9f63..b821b57fb 100644 --- a/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java +++ b/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java @@ -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; @@ -33,6 +35,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 getMentoringsForMentee( @@ -47,15 +50,30 @@ public SliceResponse getMentoringsForMentee( mentoringSlice.toList(), Mentoring::getMentorId ); + Map mentoringIdToChatRoomId = mapMentoringIdToChatRoomIdWithBatchQuery(mentoringSlice.getContent()); List content = new ArrayList<>(); - for (Entry entry : mentoringToPartnerUser.entrySet()) { - content.add(MentoringForMenteeResponse.of(entry.getKey(), entry.getValue())); + for (Mentoring mentoring : mentoringToPartnerUser.keySet()) { + content.add(MentoringForMenteeResponse.of( + mentoring, + mentoringToPartnerUser.get(mentoring), + mentoringIdToChatRoomId.get(mentoring.getId()) + )); } - return SliceResponse.of(content, mentoringSlice); } + // N+1 을 해결하면서 멘토링의 채팅방 정보 조회 + private Map mapMentoringIdToChatRoomIdWithBatchQuery(List mentorings) { + List mentoringIds = mentorings.stream() + .map(Mentoring::getId) + .distinct() + .toList(); + List chatRooms = chatRoomRepository.findAllByMentoringIdIn(mentoringIds); + return chatRooms.stream() + .collect(Collectors.toMap(ChatRoom::getMentoringId, ChatRoom::getId)); + } + @Transactional(readOnly = true) public SliceResponse getMentoringsForMentor(long siteUserId, Pageable pageable) { Mentor mentor = mentorRepository.findBySiteUserId(siteUserId) From 0625d395128443b4dfcb3b6d83c8e36e5bf5e1e9 Mon Sep 17 00:00:00 2001 From: nayonsoso Date: Mon, 18 Aug 2025 01:49:43 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test:=20=EB=A9=98=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?=EC=B1=84=ED=8C=85=EB=B0=A9=20=ED=94=BD=EC=8A=A4=EC=B3=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../solidconnection/chat/fixture/ChatRoomFixture.java | 7 +++++++ .../chat/fixture/ChatRoomFixtureBuilder.java | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixture.java b/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixture.java index cf80313bc..717852a4c 100644 --- a/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixture.java +++ b/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixture.java @@ -15,4 +15,11 @@ public class ChatRoomFixture { .isGroup(isGroup) .create(); } + + public ChatRoom 멘토링_채팅방(long mentoringId) { + return chatRoomFixtureBuilder.chatRoom() + .mentoringId(mentoringId) + .isGroup(false) + .create(); + } } diff --git a/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixtureBuilder.java b/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixtureBuilder.java index bf7ed3387..9bbb3e988 100644 --- a/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixtureBuilder.java +++ b/src/test/java/com/example/solidconnection/chat/fixture/ChatRoomFixtureBuilder.java @@ -12,6 +12,7 @@ public class ChatRoomFixtureBuilder { private final ChatRoomRepository chatRoomRepository; private boolean isGroup; + private Long mentoringId; public ChatRoomFixtureBuilder chatRoom() { return new ChatRoomFixtureBuilder(chatRoomRepository); @@ -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); } } From 6bed161a74f197972ff33b2f47c04f244d01095d Mon Sep 17 00:00:00 2001 From: nayonsoso Date: Mon, 18 Aug 2025 01:50:22 +0900 Subject: [PATCH 3/4] =?UTF-8?q?test:=20=EB=A9=98=ED=8B=B0=EC=9D=98=20?= =?UTF-8?q?=EB=A9=98=ED=86=A0=EB=A7=81=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=8B=9C=20=EC=B1=84=ED=8C=85=EB=B0=A9=20ID=20?= =?UTF-8?q?=ED=8F=AC=ED=95=A8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mentor/service/MentoringQueryServiceTest.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java b/src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java index d8146dc51..867af6e56 100644 --- a/src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java +++ b/src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java @@ -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; @@ -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; @@ -146,10 +151,12 @@ 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 @@ -157,10 +164,10 @@ class 멘티의_멘토링_목록_조회_테스트 { 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()) ); } From 7c81da24f4539417d4a28b8c195576c3a05553d0 Mon Sep 17 00:00:00 2001 From: nayonsoso Date: Mon, 18 Aug 2025 02:19:26 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=EB=A9=98=ED=86=A0=EB=A7=81=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=88=9C=EC=84=9C=EB=A5=BC=20=EB=B3=B4?= =?UTF-8?q?=EC=9E=A5=ED=95=98=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - map이 아니라 순서가 보장되는 slice를 기준으로 순회하며 응답을 생성하도록 --- .../mentor/service/MentoringQueryService.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java b/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java index b821b57fb..1ef037706 100644 --- a/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java +++ b/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java @@ -19,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; @@ -53,7 +52,7 @@ public SliceResponse getMentoringsForMentee( Map mentoringIdToChatRoomId = mapMentoringIdToChatRoomIdWithBatchQuery(mentoringSlice.getContent()); List content = new ArrayList<>(); - for (Mentoring mentoring : mentoringToPartnerUser.keySet()) { + for (Mentoring mentoring : mentoringSlice) { content.add(MentoringForMenteeResponse.of( mentoring, mentoringToPartnerUser.get(mentoring), @@ -86,8 +85,8 @@ public SliceResponse getMentoringsForMentor(long sit ); List content = new ArrayList<>(); - for (Entry 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);