From 51158ffc9b9edd9494dcbbf54d43739fae50dffb Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Sat, 25 Oct 2025 16:37:52 +0900 Subject: [PATCH 1/8] =?UTF-8?q?refactor:=20=EB=A9=98=ED=86=A0=EC=9D=98=20?= =?UTF-8?q?=EB=A9=98=ED=86=A0=EB=A7=81=20=EC=A1=B0=ED=9A=8C=20=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=EC=97=90=EC=84=9C=20mentoringId=EA=B0=80=20=EC=95=84?= =?UTF-8?q?=EB=8B=88=EB=9D=BC=20roomId=EB=A5=BC=20=ED=8F=AC=ED=95=A8?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/MentoringForMentorResponse.java | 6 ++-- .../mentor/service/MentoringQueryService.java | 30 +++++++++++-------- .../service/MentoringQueryServiceTest.java | 17 ++++++----- 3 files changed, 31 insertions(+), 22 deletions(-) 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 8a41fba84..46791d071 100644 --- a/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMentorResponse.java +++ b/src/main/java/com/example/solidconnection/mentor/dto/MentoringForMentorResponse.java @@ -6,7 +6,7 @@ import java.time.ZonedDateTime; public record MentoringForMentorResponse( - long mentoringId, + Long roomId, String profileImageUrl, String nickname, boolean isChecked, @@ -14,9 +14,9 @@ public record MentoringForMentorResponse( ZonedDateTime createdAt ) { - public static MentoringForMentorResponse of(Mentoring mentoring, SiteUser partner) { + public static MentoringForMentorResponse of(Mentoring mentoring, SiteUser partner, Long roomId) { return new MentoringForMentorResponse( - mentoring.getId(), + roomId, partner.getProfileImageUrl(), partner.getNickname(), mentoring.getCheckedAtByMentor() != null, 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 57753f983..606990068 100644 --- a/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java +++ b/src/main/java/com/example/solidconnection/mentor/service/MentoringQueryService.java @@ -118,17 +118,6 @@ public SliceResponse getMentoringsForMentee( 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) @@ -140,9 +129,15 @@ public SliceResponse getMentoringsForMentor(long sit Mentoring::getMenteeId ); + Map mentoringIdToChatRoomId = mapMentoringIdToChatRoomIdWithBatchQuery(mentoringSlice.getContent()); + List content = new ArrayList<>(); for (Mentoring mentoring : mentoringSlice) { - content.add(MentoringForMentorResponse.of(mentoring, mentoringToPartnerUser.get(mentoring))); + content.add(MentoringForMentorResponse.of( + mentoring, + mentoringToPartnerUser.get(mentoring), + mentoringIdToChatRoomId.get(mentoring.getId()) + )); } return SliceResponse.of(content, mentoringSlice); @@ -165,4 +160,15 @@ private Map mapMentoringToPartnerUserWithBatchQuery( mentoring -> partnerIdToPartnerUsermap.get(getPartnerId.apply(mentoring)) )); } + + // 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)); + } } 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 cb680b986..959d8e491 100644 --- a/src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java +++ b/src/test/java/com/example/solidconnection/mentor/service/MentoringQueryServiceTest.java @@ -97,15 +97,18 @@ class 멘토의_멘토링_목록_조회_테스트 { Mentoring mentoring2 = mentoringFixture.승인된_멘토링(mentor1.getId(), menteeUser2.getId()); Mentoring mentoring3 = mentoringFixture.거절된_멘토링(mentor1.getId(), menteeUser3.getId()); + ChatRoom chatRoom2 = chatRoomFixture.멘토링_채팅방(mentoring2.getId()); + // when SliceResponse response = mentoringQueryService.getMentoringsForMentor(mentorUser1.getId(), pageable); // then - assertThat(response.content()).extracting(MentoringForMentorResponse::mentoringId) + assertThat(response.content()) + .extracting(MentoringForMentorResponse::verifyStatus, MentoringForMentorResponse::roomId) .containsExactlyInAnyOrder( - mentoring1.getId(), - mentoring2.getId(), - mentoring3.getId() + tuple(VerifyStatus.PENDING, null), + tuple(VerifyStatus.APPROVED, chatRoom2.getId()), + tuple(VerifyStatus.REJECTED, null) ); } @@ -137,10 +140,10 @@ class 멘토의_멘토링_목록_조회_테스트 { // then assertThat(response.content()) - .extracting(MentoringForMentorResponse::mentoringId, MentoringForMentorResponse::isChecked) + .extracting(MentoringForMentorResponse::nickname, MentoringForMentorResponse::isChecked) .containsExactlyInAnyOrder( - tuple(mentoring1.getId(), false), - tuple(mentoring2.getId(), true) + tuple(menteeUser1.getNickname(), false), + tuple(menteeUser2.getNickname(), true) ); } From e366144210b6ddb63c37534bc849b996a267a2fd Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Sat, 25 Oct 2025 17:08:57 +0900 Subject: [PATCH 2/8] =?UTF-8?q?refactor:=20=ED=8C=8C=ED=8A=B8=EB=84=88?= =?UTF-8?q?=EA=B0=80=20=EB=A9=98=ED=86=A0=EC=9D=B8=20=EA=B2=BD=EC=9A=B0=20?= =?UTF-8?q?partnerId=EB=8A=94=20mentorId=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AS IS: 멘토/멘티 모두 partnerId가 siteUserId - TO BE: 멘티: siteUserId, 멘토: mentorId --- .../chat/dto/ChatParticipantResponse.java | 2 +- .../solidconnection/chat/service/ChatService.java | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java b/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java index ffa6b9b8c..adf8206ca 100644 --- a/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java +++ b/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java @@ -1,7 +1,7 @@ package com.example.solidconnection.chat.dto; public record ChatParticipantResponse( - long partnerId, + long partnerId, // 멘티는 siteUserId, 멘토는 mentorId String nickname, String profileUrl ) { diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 78530d752..674caef88 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -15,6 +15,7 @@ import com.example.solidconnection.chat.dto.ChatMessageSendRequest; import com.example.solidconnection.chat.dto.ChatMessageSendResponse; import com.example.solidconnection.chat.dto.ChatParticipantResponse; +import com.example.solidconnection.chat.dto.ChatRoomData; import com.example.solidconnection.chat.dto.ChatRoomListResponse; import com.example.solidconnection.chat.dto.ChatRoomResponse; import com.example.solidconnection.chat.repository.ChatMessageRepository; @@ -23,7 +24,8 @@ import com.example.solidconnection.chat.repository.ChatRoomRepository; import com.example.solidconnection.common.dto.SliceResponse; import com.example.solidconnection.common.exception.CustomException; -import com.example.solidconnection.chat.dto.ChatRoomData; +import com.example.solidconnection.mentor.domain.Mentor; +import com.example.solidconnection.mentor.repository.MentorRepository; import com.example.solidconnection.siteuser.domain.SiteUser; import com.example.solidconnection.siteuser.repository.SiteUserRepository; import java.util.Collections; @@ -43,6 +45,7 @@ public class ChatService { private final ChatParticipantRepository chatParticipantRepository; private final ChatReadStatusRepository chatReadStatusRepository; private final SiteUserRepository siteUserRepository; + private final MentorRepository mentorRepository; private final SimpMessageSendingOperations simpMessageSendingOperations; @@ -51,12 +54,14 @@ public ChatService(ChatRoomRepository chatRoomRepository, ChatParticipantRepository chatParticipantRepository, ChatReadStatusRepository chatReadStatusRepository, SiteUserRepository siteUserRepository, + MentorRepository mentorRepository, @Lazy SimpMessageSendingOperations simpMessageSendingOperations) { this.chatRoomRepository = chatRoomRepository; this.chatMessageRepository = chatMessageRepository; this.chatParticipantRepository = chatParticipantRepository; this.chatReadStatusRepository = chatReadStatusRepository; this.siteUserRepository = siteUserRepository; + this.mentorRepository = mentorRepository; this.simpMessageSendingOperations = simpMessageSendingOperations; } @@ -128,7 +133,13 @@ public ChatParticipantResponse getChatPartner(long siteUserId, Long roomId) { ChatParticipant partnerParticipant = findPartner(chatRoom, siteUserId); SiteUser siteUser = siteUserRepository.findById(partnerParticipant.getSiteUserId()) .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); - return ChatParticipantResponse.of(siteUser.getId(), siteUser.getNickname(), siteUser.getProfileImageUrl()); + + // 멘티는 siteUserId, 멘토는 mentorId + Long partnerId = mentorRepository.findBySiteUserId(siteUser.getId()) + .map(Mentor::getId) + .orElse(siteUser.getId()); + + return ChatParticipantResponse.of(partnerId, siteUser.getNickname(), siteUser.getProfileImageUrl()); } private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) { From 92655d695429fa0b966f7e573a17b76e00859277 Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Sat, 25 Oct 2025 20:32:44 +0900 Subject: [PATCH 3/8] =?UTF-8?q?refactor:=20=EC=9D=91=EB=8B=B5=EC=9D=98=20s?= =?UTF-8?q?enderId=EA=B0=80=20mentorId/siteUserId=EA=B0=80=20=EB=90=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/domain/ChatMessage.java | 2 +- .../chat/service/ChatService.java | 33 +++++++++++++++++-- .../mentor/repository/MentorRepository.java | 3 ++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/domain/ChatMessage.java b/src/main/java/com/example/solidconnection/chat/domain/ChatMessage.java index 170a93f05..aa7369451 100644 --- a/src/main/java/com/example/solidconnection/chat/domain/ChatMessage.java +++ b/src/main/java/com/example/solidconnection/chat/domain/ChatMessage.java @@ -28,7 +28,7 @@ public class ChatMessage extends BaseEntity { @Column(nullable = false, length = 500) private String content; - private long senderId; + private long senderId; // chat_participant의 id @ManyToOne(fetch = FetchType.LAZY) private ChatRoom chatRoom; diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 674caef88..528888d39 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -30,6 +30,10 @@ import com.example.solidconnection.siteuser.repository.SiteUserRepository; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; import org.springframework.context.annotation.Lazy; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; @@ -119,8 +123,31 @@ public SliceResponse getChatMessages(long siteUserId, long Slice chatMessages = chatMessageRepository.findByRoomIdWithPaging(roomId, pageable); + // senderId(participantId) 조회 + Set participantIds = chatMessages.getContent().stream() + .map(ChatMessage::getSenderId) + .collect(Collectors.toSet()); + + Map participantIdToParticipant = chatParticipantRepository.findAllById(participantIds).stream() + .collect(Collectors.toMap(ChatParticipant::getId, Function.identity())); + + // participants의 siteUserId의 집합 + Set siteUserIds = participantIdToParticipant.values().stream() + .map(ChatParticipant::getSiteUserId) + .collect(Collectors.toSet()); + + Map siteUserIdToMentorId = mentorRepository.findAllBySiteUserIdIn(siteUserIds.stream().toList()).stream() + .collect(Collectors.toMap(Mentor::getSiteUserId, Mentor::getId)); + List content = chatMessages.getContent().stream() - .map(this::toChatMessageResponse) + .map(message -> { + ChatParticipant senderParticipant = participantIdToParticipant.get(message.getSenderId()); + long externalSenderId = siteUserIdToMentorId.getOrDefault( + senderParticipant.getSiteUserId(), + senderParticipant.getSiteUserId() + ); + return toChatMessageResponse(message, externalSenderId); + }) .toList(); return SliceResponse.of(content, chatMessages); @@ -159,7 +186,7 @@ public void validateChatRoomParticipant(long siteUserId, long roomId) { } } - private ChatMessageResponse toChatMessageResponse(ChatMessage message) { + private ChatMessageResponse toChatMessageResponse(ChatMessage message, long externalSenderId) { List attachments = message.getChatAttachments().stream() .map(attachment -> ChatAttachmentResponse.of( attachment.getId(), @@ -173,7 +200,7 @@ private ChatMessageResponse toChatMessageResponse(ChatMessage message) { return ChatMessageResponse.of( message.getId(), message.getContent(), - message.getSenderId(), + externalSenderId, message.getCreatedAt(), attachments ); diff --git a/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java b/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java index 430602f1e..46a457229 100644 --- a/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java +++ b/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java @@ -2,6 +2,7 @@ import com.example.solidconnection.location.region.domain.Region; import com.example.solidconnection.mentor.domain.Mentor; +import java.util.List; import java.util.Optional; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; @@ -23,4 +24,6 @@ public interface MentorRepository extends JpaRepository { WHERE u.region = :region """) Slice findAllByRegion(@Param("region") Region region, Pageable pageable); + + Optional findAllBySiteUserIdIn(List list); } From 438f18090d8bcc55bfe1cc5000ad03b24b06282a Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Wed, 5 Nov 2025 09:39:08 +0900 Subject: [PATCH 4/8] =?UTF-8?q?refactor:=20senderId=EC=97=90=20=ED=95=B4?= =?UTF-8?q?=EB=8B=B9=ED=95=98=EB=8A=94=20chatParticipant=EA=B0=80=20?= =?UTF-8?q?=EC=97=86=EC=9D=84=20=EA=B2=BD=EC=9A=B0=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=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 --- .../com/example/solidconnection/chat/service/ChatService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 528888d39..0969f7afc 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -142,6 +142,9 @@ public SliceResponse getChatMessages(long siteUserId, long List content = chatMessages.getContent().stream() .map(message -> { ChatParticipant senderParticipant = participantIdToParticipant.get(message.getSenderId()); + if (senderParticipant == null) { + throw new CustomException(CHAT_PARTICIPANT_NOT_FOUND); + } long externalSenderId = siteUserIdToMentorId.getOrDefault( senderParticipant.getSiteUserId(), senderParticipant.getSiteUserId() From fedc72b15a262d081426f19a76d8674c17e8c8be Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Wed, 5 Nov 2025 09:51:28 +0900 Subject: [PATCH 5/8] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=EC=8B=9C=EA=B7=B8?= =?UTF-8?q?=EB=8B=88=EC=B2=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/solidconnection/chat/service/ChatService.java | 2 +- .../solidconnection/mentor/repository/MentorRepository.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 0969f7afc..6b92b0b9a 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -136,7 +136,7 @@ public SliceResponse getChatMessages(long siteUserId, long .map(ChatParticipant::getSiteUserId) .collect(Collectors.toSet()); - Map siteUserIdToMentorId = mentorRepository.findAllBySiteUserIdIn(siteUserIds.stream().toList()).stream() + Map siteUserIdToMentorId = mentorRepository.findAllBySiteUserIdIn(siteUserIds).stream() .collect(Collectors.toMap(Mentor::getSiteUserId, Mentor::getId)); List content = chatMessages.getContent().stream() diff --git a/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java b/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java index 46a457229..85dbbc0bf 100644 --- a/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java +++ b/src/main/java/com/example/solidconnection/mentor/repository/MentorRepository.java @@ -4,6 +4,7 @@ import com.example.solidconnection.mentor.domain.Mentor; import java.util.List; import java.util.Optional; +import java.util.Set; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; @@ -25,5 +26,5 @@ public interface MentorRepository extends JpaRepository { """) Slice findAllByRegion(@Param("region") Region region, Pageable pageable); - Optional findAllBySiteUserIdIn(List list); + List findAllBySiteUserIdIn(Set siteUserIds); } From 610814b1cae7fd190c01b4048d9c4da4f8e76367 Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Thu, 6 Nov 2025 13:38:23 +0900 Subject: [PATCH 6/8] =?UTF-8?q?refactor:=20getChatMessages=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EC=97=90=EC=84=9C=20=EC=9D=91=EB=8B=B5?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20siteUserId=EB=A5=BC=20=EB=84=98=EA=B2=A8?= =?UTF-8?q?=EC=A3=BC=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AS IS: mentorId(mentor) / siteUserId(mentee) - TO BE: siteUserId(all) --- .../chat/dto/ChatMessageResponse.java | 2 +- .../solidconnection/chat/service/ChatService.java | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/dto/ChatMessageResponse.java b/src/main/java/com/example/solidconnection/chat/dto/ChatMessageResponse.java index a3728b7fd..b9551b9b2 100644 --- a/src/main/java/com/example/solidconnection/chat/dto/ChatMessageResponse.java +++ b/src/main/java/com/example/solidconnection/chat/dto/ChatMessageResponse.java @@ -6,7 +6,7 @@ public record ChatMessageResponse( long id, String content, - long senderId, + long senderId, // siteUserId ZonedDateTime createdAt, List attachments ) { diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 6b92b0b9a..50951a53c 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -131,24 +131,13 @@ public SliceResponse getChatMessages(long siteUserId, long Map participantIdToParticipant = chatParticipantRepository.findAllById(participantIds).stream() .collect(Collectors.toMap(ChatParticipant::getId, Function.identity())); - // participants의 siteUserId의 집합 - Set siteUserIds = participantIdToParticipant.values().stream() - .map(ChatParticipant::getSiteUserId) - .collect(Collectors.toSet()); - - Map siteUserIdToMentorId = mentorRepository.findAllBySiteUserIdIn(siteUserIds).stream() - .collect(Collectors.toMap(Mentor::getSiteUserId, Mentor::getId)); - List content = chatMessages.getContent().stream() .map(message -> { ChatParticipant senderParticipant = participantIdToParticipant.get(message.getSenderId()); if (senderParticipant == null) { throw new CustomException(CHAT_PARTICIPANT_NOT_FOUND); } - long externalSenderId = siteUserIdToMentorId.getOrDefault( - senderParticipant.getSiteUserId(), - senderParticipant.getSiteUserId() - ); + long externalSenderId = senderParticipant.getSiteUserId(); return toChatMessageResponse(message, externalSenderId); }) .toList(); From cf706fba02a4c6b28667849a8369d2607889559c Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Thu, 6 Nov 2025 13:44:49 +0900 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20=ED=97=AC=ED=8D=BC=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EB=A1=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EC=84=B1=EC=9D=84=20=EB=B6=84=EC=82=B0?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/service/ChatService.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 50951a53c..b287809bb 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -123,15 +123,27 @@ public SliceResponse getChatMessages(long siteUserId, long Slice chatMessages = chatMessageRepository.findByRoomIdWithPaging(roomId, pageable); - // senderId(participantId) 조회 + Map participantIdToParticipant = buildParticipantIdToParticipantMap(chatMessages); + List content = buildChatMessageResponses(chatMessages, participantIdToParticipant); + + return SliceResponse.of(content, chatMessages); + } + + // senderId(chatParticipantId)로 chatParticipant 맵 생성 + private Map buildParticipantIdToParticipantMap(Slice chatMessages) { Set participantIds = chatMessages.getContent().stream() .map(ChatMessage::getSenderId) .collect(Collectors.toSet()); - Map participantIdToParticipant = chatParticipantRepository.findAllById(participantIds).stream() + return chatParticipantRepository.findAllById(participantIds).stream() .collect(Collectors.toMap(ChatParticipant::getId, Function.identity())); + } - List content = chatMessages.getContent().stream() + private List buildChatMessageResponses( + Slice chatMessages, + Map participantIdToParticipant + ) { + return chatMessages.getContent().stream() .map(message -> { ChatParticipant senderParticipant = participantIdToParticipant.get(message.getSenderId()); if (senderParticipant == null) { @@ -141,8 +153,6 @@ public SliceResponse getChatMessages(long siteUserId, long return toChatMessageResponse(message, externalSenderId); }) .toList(); - - return SliceResponse.of(content, chatMessages); } @Transactional(readOnly = true) From 08d2ff77f35129e661c02bd8f3c07839b7507f06 Mon Sep 17 00:00:00 2001 From: seonghyeok Date: Thu, 6 Nov 2025 13:47:47 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor:=20getChatPartner=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=EC=9D=98=20=EC=9D=91=EB=8B=B5=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20siteUserId=EB=A5=BC=20=EB=84=98=EA=B2=A8=EC=A3=BC?= =?UTF-8?q?=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AS IS: mentorId(mentor) / siteUserId(mentee) - TO BE: siteUserId(all) --- .../solidconnection/chat/dto/ChatParticipantResponse.java | 2 +- .../example/solidconnection/chat/service/ChatService.java | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java b/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java index adf8206ca..18276b561 100644 --- a/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java +++ b/src/main/java/com/example/solidconnection/chat/dto/ChatParticipantResponse.java @@ -1,7 +1,7 @@ package com.example.solidconnection.chat.dto; public record ChatParticipantResponse( - long partnerId, // 멘티는 siteUserId, 멘토는 mentorId + long partnerId, // siteUserId String nickname, String profileUrl ) { diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index b287809bb..57f8cad65 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -24,7 +24,6 @@ import com.example.solidconnection.chat.repository.ChatRoomRepository; import com.example.solidconnection.common.dto.SliceResponse; import com.example.solidconnection.common.exception.CustomException; -import com.example.solidconnection.mentor.domain.Mentor; import com.example.solidconnection.mentor.repository.MentorRepository; import com.example.solidconnection.siteuser.domain.SiteUser; import com.example.solidconnection.siteuser.repository.SiteUserRepository; @@ -163,12 +162,7 @@ public ChatParticipantResponse getChatPartner(long siteUserId, Long roomId) { SiteUser siteUser = siteUserRepository.findById(partnerParticipant.getSiteUserId()) .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); - // 멘티는 siteUserId, 멘토는 mentorId - Long partnerId = mentorRepository.findBySiteUserId(siteUser.getId()) - .map(Mentor::getId) - .orElse(siteUser.getId()); - - return ChatParticipantResponse.of(partnerId, siteUser.getNickname(), siteUser.getProfileImageUrl()); + return ChatParticipantResponse.of(siteUser.getId(), siteUser.getNickname(), siteUser.getProfileImageUrl()); } private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) {