diff --git a/backend/emm-sale/src/main/java/com/emmsale/member/domain/Member.java b/backend/emm-sale/src/main/java/com/emmsale/member/domain/Member.java index 51003a9d1..4f916de76 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/member/domain/Member.java +++ b/backend/emm-sale/src/main/java/com/emmsale/member/domain/Member.java @@ -35,20 +35,23 @@ public class Member extends BaseEntity { private String githubUsername; public Member(final Long id, final Long githubId, final String imageUrl, final String name, + final String description, final String githubUsername) { this.id = id; this.githubId = githubId; this.imageUrl = imageUrl; this.name = name; - this.description = DEFAULT_DESCRIPTION; + this.description = description; this.githubUsername = githubUsername; } + public Member(final Long id, final Long githubId, final String imageUrl, final String name, + final String githubUsername) { + this(id, githubId, imageUrl, name, DEFAULT_DESCRIPTION, githubUsername); + } + public Member(final Long githubId, final String imageUrl, final String githubUsername) { - this.githubId = githubId; - this.imageUrl = imageUrl; - this.description = DEFAULT_DESCRIPTION; - this.githubUsername = githubUsername; + this(null, githubId, imageUrl, null, DEFAULT_DESCRIPTION, githubUsername); } public void updateName(final String name) { diff --git a/backend/emm-sale/src/main/java/com/emmsale/message_room/application/RoomQueryService.java b/backend/emm-sale/src/main/java/com/emmsale/message_room/application/RoomQueryService.java index f690c55b3..93da4734d 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/message_room/application/RoomQueryService.java +++ b/backend/emm-sale/src/main/java/com/emmsale/message_room/application/RoomQueryService.java @@ -51,9 +51,6 @@ public List findAll(final Member loginMember, final Long memberId) .map(messageOverview -> RoomResponse.from( messageOverview, - memberRepository.findById(messageOverview.getSenderId()) - .orElseThrow(() -> new MemberException(NOT_FOUND_MEMBER)), - // TODO: 2023/11/05 N+1 문제 findInterlocutor(interlocutorIdPerRoomExceptMe.get(messageOverview.getRoomUUID())) )) .collect(Collectors.toList()); diff --git a/backend/emm-sale/src/main/java/com/emmsale/message_room/application/dto/RoomResponse.java b/backend/emm-sale/src/main/java/com/emmsale/message_room/application/dto/RoomResponse.java index 22cd71023..222fcd79b 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/message_room/application/dto/RoomResponse.java +++ b/backend/emm-sale/src/main/java/com/emmsale/message_room/application/dto/RoomResponse.java @@ -17,7 +17,6 @@ public class RoomResponse { public static RoomResponse from( final MessageOverview messageOverview, - final Member recentMessageSender, final Member interlocutor ) { return new RoomResponse( @@ -26,7 +25,7 @@ public static RoomResponse from( MessageResponse.from(new Message( messageOverview.getId(), messageOverview.getContent(), - recentMessageSender, + messageOverview.getSender(), messageOverview.getRoomUUID(), messageOverview.getCreatedAt())) ); diff --git a/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/MessageDao.java b/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/MessageDao.java index 7627785a5..21f991a99 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/MessageDao.java +++ b/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/MessageDao.java @@ -1,6 +1,7 @@ package com.emmsale.message_room.infrastructure.persistence; +import com.emmsale.member.domain.Member; import com.emmsale.message_room.infrastructure.persistence.dto.MessageOverview; import java.util.List; import lombok.RequiredArgsConstructor; @@ -14,9 +15,16 @@ public class MessageDao { private static final RowMapper ROW_MAPPER = (rs, rowNum) -> new MessageOverview( - rs.getLong("id"), + rs.getLong("m2.id"), rs.getString("content"), - rs.getLong("sender_id"), + new Member( + rs.getLong("sender.id"), + rs.getLong("github_id"), + rs.getString("image_url"), + rs.getString("name"), + rs.getString("description"), + rs.getString("github_username") + ), rs.getTimestamp("created_at").toLocalDateTime(), rs.getString("room_id") ); @@ -27,12 +35,15 @@ public List findRecentlyMessages(final Long memberId) { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder - .append("SELECT m2.id, m2.content, m2.sender_id, m2.created_at, m2.room_id ") + .append("SELECT m2.id, m2.content, m2.sender_id, m2.created_at, m2.room_id, ") + .append( + "sender.id, sender.github_id, sender.image_url, sender.name, sender.description, sender.github_username ") .append("FROM room r ") .append("JOIN (SELECT room_id, MAX(created_at) as max_created_at ") .append(" FROM message ") .append(" GROUP BY room_id) m1 ON r.uuid = m1.room_id ") .append("JOIN message m2 ON m1.room_id = m2.room_id AND m1.max_created_at = m2.created_at ") + .append("JOIN member sender ON m2.sender_id = sender.id ") .append("WHERE r.member_id = ? ") .append("AND m2.created_at > r.last_exited_time ") .append("ORDER BY m2.created_at DESC"); diff --git a/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/dto/MessageOverview.java b/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/dto/MessageOverview.java index f31bc0fd8..3bd09f8db 100644 --- a/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/dto/MessageOverview.java +++ b/backend/emm-sale/src/main/java/com/emmsale/message_room/infrastructure/persistence/dto/MessageOverview.java @@ -1,5 +1,6 @@ package com.emmsale.message_room.infrastructure.persistence.dto; +import com.emmsale.member.domain.Member; import java.time.LocalDateTime; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -10,7 +11,7 @@ public class MessageOverview { private final Long id; private final String content; - private final Long senderId; + private final Member sender; private final LocalDateTime createdAt; private final String roomUUID; } diff --git a/backend/emm-sale/src/test/java/com/emmsale/message_room/application/RoomQueryServiceTest.java b/backend/emm-sale/src/test/java/com/emmsale/message_room/application/RoomQueryServiceTest.java index cd1ccb5af..7ecdbfeda 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/message_room/application/RoomQueryServiceTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/message_room/application/RoomQueryServiceTest.java @@ -107,7 +107,7 @@ void test_findAll() throws Exception { final MessageOverview messageOverview1 = new MessageOverview( resultMessage1.getId(), resultMessage1.getContent(), - resultMessage1.getSender().getId(), + resultMessage1.getSender(), resultMessage1.getCreatedAt(), resultMessage1.getRoomId() ); @@ -115,15 +115,15 @@ void test_findAll() throws Exception { final MessageOverview messageOverview2 = new MessageOverview( resultMessage2.getId(), resultMessage2.getContent(), - resultMessage2.getSender().getId(), + resultMessage2.getSender(), resultMessage2.getCreatedAt(), resultMessage2.getRoomId() ); final List expect = List.of( - RoomResponse.from(messageOverview1, resultMessage1.getSender(), + RoomResponse.from(messageOverview1, memberRepository.findById(room1Interlocutor.getId()).get()), - RoomResponse.from(messageOverview2, resultMessage2.getSender(), + RoomResponse.from(messageOverview2, memberRepository.findById(room2Interlocutor.getId()).get()) ); diff --git a/backend/emm-sale/src/test/java/com/emmsale/message_room/infrastructure/persistence/MessageDaoTest.java b/backend/emm-sale/src/test/java/com/emmsale/message_room/infrastructure/persistence/MessageDaoTest.java index 3aa1f63ff..c8ac8696d 100644 --- a/backend/emm-sale/src/test/java/com/emmsale/message_room/infrastructure/persistence/MessageDaoTest.java +++ b/backend/emm-sale/src/test/java/com/emmsale/message_room/infrastructure/persistence/MessageDaoTest.java @@ -109,14 +109,14 @@ void test_findRecentlyMessages() throws Exception { new MessageOverview( resultMessage1.getId(), resultMessage1.getContent(), - resultMessage1.getSender().getId(), + resultMessage1.getSender(), resultMessage1.getCreatedAt(), resultMessage1.getRoomId() ), new MessageOverview( resultMessage2.getId(), resultMessage2.getContent(), - resultMessage2.getSender().getId(), + resultMessage2.getSender(), resultMessage2.getCreatedAt(), resultMessage2.getRoomId() ) @@ -128,6 +128,7 @@ void test_findRecentlyMessages() throws Exception { //then Assertions.assertThat(actual) .usingRecursiveComparison() + .ignoringFields("sender.createdAt", "sender.updatedAt") .isEqualTo(expect); } }