Skip to content

Commit afe27ab

Browse files
authored
feat: 채팅방 partner 정보를 조회하는 API 추가 (#457)
* feat: 채팅방 partner 정보를 조회하는 API 추가 * test: 채팅방 partner 정보를 조회 테스트 추가
1 parent 151c9ad commit afe27ab

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

src/main/java/com/example/solidconnection/chat/controller/ChatController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.solidconnection.chat.controller;
22

33
import com.example.solidconnection.chat.dto.ChatMessageResponse;
4+
import com.example.solidconnection.chat.dto.ChatParticipantResponse;
45
import com.example.solidconnection.chat.dto.ChatRoomListResponse;
56
import com.example.solidconnection.chat.service.ChatService;
67
import com.example.solidconnection.common.dto.SliceResponse;
@@ -41,6 +42,15 @@ public ResponseEntity<SliceResponse<ChatMessageResponse>> getChatMessages(
4142
return ResponseEntity.ok(response);
4243
}
4344

45+
@GetMapping("rooms/{room-id}/partner")
46+
public ResponseEntity<ChatParticipantResponse> getChatPartner(
47+
@AuthorizedUser long siteUserId,
48+
@PathVariable("room-id") Long roomId
49+
) {
50+
ChatParticipantResponse response = chatService.getChatPartner(siteUserId, roomId);
51+
return ResponseEntity.ok(response);
52+
}
53+
4454
@PutMapping("/rooms/{room-id}/read")
4555
public ResponseEntity<Void> markChatMessagesAsRead(
4656
@AuthorizedUser long siteUserId,

src/main/java/com/example/solidconnection/chat/service/ChatService.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ private ChatRoomResponse toChatRoomResponse(ChatRoom chatRoom, long siteUserId)
8484
return ChatRoomResponse.of(chatRoom.getId(), lastChatMessage, lastReceivedTime, partner, unReadCount);
8585
}
8686

87-
private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) {
88-
if (chatRoom.isGroup()) {
89-
throw new CustomException(INVALID_CHAT_ROOM_STATE);
90-
}
91-
return chatRoom.getChatParticipants().stream()
92-
.filter(participant -> participant.getSiteUserId() != siteUserId)
93-
.findFirst()
94-
.orElseThrow(() -> new CustomException(CHAT_PARTNER_NOT_FOUND));
95-
}
96-
9787
@Transactional(readOnly = true)
9888
public SliceResponse<ChatMessageResponse> getChatMessages(long siteUserId, long roomId, Pageable pageable) {
9989
validateChatRoomParticipant(siteUserId, roomId);
@@ -107,6 +97,26 @@ public SliceResponse<ChatMessageResponse> getChatMessages(long siteUserId, long
10797
return SliceResponse.of(content, chatMessages);
10898
}
10999

100+
@Transactional(readOnly = true)
101+
public ChatParticipantResponse getChatPartner(long siteUserId, Long roomId) {
102+
ChatRoom chatRoom = chatRoomRepository.findById(roomId)
103+
.orElseThrow(() -> new CustomException(INVALID_CHAT_ROOM_STATE));
104+
ChatParticipant partnerParticipant = findPartner(chatRoom, siteUserId);
105+
SiteUser siteUser = siteUserRepository.findById(partnerParticipant.getSiteUserId())
106+
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
107+
return ChatParticipantResponse.of(siteUser.getId(), siteUser.getNickname(), siteUser.getProfileImageUrl());
108+
}
109+
110+
private ChatParticipant findPartner(ChatRoom chatRoom, long siteUserId) {
111+
if (chatRoom.isGroup()) {
112+
throw new CustomException(INVALID_CHAT_ROOM_STATE);
113+
}
114+
return chatRoom.getChatParticipants().stream()
115+
.filter(participant -> participant.getSiteUserId() != siteUserId)
116+
.findFirst()
117+
.orElseThrow(() -> new CustomException(CHAT_PARTNER_NOT_FOUND));
118+
}
119+
110120
public void validateChatRoomParticipant(long siteUserId, long roomId) {
111121
boolean isParticipant = chatParticipantRepository.existsByChatRoomIdAndSiteUserId(roomId, siteUserId);
112122
if (!isParticipant) {

src/test/java/com/example/solidconnection/chat/service/ChatServiceTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.example.solidconnection.chat.dto.ChatMessageResponse;
1515
import com.example.solidconnection.chat.dto.ChatMessageSendRequest;
1616
import com.example.solidconnection.chat.dto.ChatMessageSendResponse;
17+
import com.example.solidconnection.chat.dto.ChatParticipantResponse;
1718
import com.example.solidconnection.chat.dto.ChatRoomListResponse;
1819
import com.example.solidconnection.chat.fixture.ChatAttachmentFixture;
1920
import com.example.solidconnection.chat.fixture.ChatMessageFixture;
@@ -309,6 +310,28 @@ void setUp() {
309310
}
310311
}
311312

313+
@Nested
314+
class 채팅방_파트너_정보를_조회한다 {
315+
316+
@Test
317+
void 채팅방_파트너를_정상_조회한다() {
318+
// given
319+
ChatRoom chatRoom = chatRoomFixture.채팅방(false);
320+
chatParticipantFixture.참여자(user.getId(), chatRoom);
321+
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);
322+
323+
// when
324+
ChatParticipantResponse response = chatService.getChatPartner(user.getId(), chatRoom.getId());
325+
326+
// then
327+
assertAll(
328+
() -> assertThat(response.partnerId()).isEqualTo(mentor1.getId()),
329+
() -> assertThat(response.nickname()).isEqualTo(mentor1.getNickname()),
330+
() -> assertThat(response.profileUrl()).isEqualTo(mentor1.getProfileImageUrl())
331+
);
332+
}
333+
}
334+
312335
@Nested
313336
class 채팅_메시지_읽음을_처리한다 {
314337

0 commit comments

Comments
 (0)