-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat #140] 채팅 요청 목록 조회 API #141
Changes from all commits
e6343b0
959a27e
dbd4c29
5c1a3ba
f67d014
b4e3101
911074e
2f083de
388e144
738af1f
8868b4e
779f5bd
51e146a
445c947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.dnd.gongmuin.chat.dto.response; | ||
|
||
import com.dnd.gongmuin.chat.domain.ChatStatus; | ||
import com.dnd.gongmuin.member.domain.JobGroup; | ||
import com.querydsl.core.annotations.QueryProjection; | ||
|
||
public record ChatProposalInfo( | ||
Long chatRoomId, | ||
String chatStatus, | ||
boolean isInquirer, | ||
Long partnerId, | ||
String partnerNickname, | ||
String partnerJobGroup, | ||
int partnerProfileImageNo | ||
) { | ||
@QueryProjection | ||
public ChatProposalInfo( | ||
Long chatRoomId, | ||
ChatStatus chatStatus, | ||
boolean isInquirer, | ||
Long partnerId, | ||
String partnerNickname, | ||
JobGroup partnerJobGroup, | ||
int partnerProfileImageNo | ||
) { | ||
this( | ||
chatRoomId, | ||
chatStatus.getLabel(), | ||
isInquirer, | ||
partnerId, | ||
partnerNickname, | ||
partnerJobGroup.getLabel(), | ||
partnerProfileImageNo | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.dnd.gongmuin.chat.dto.response; | ||
|
||
import com.dnd.gongmuin.question_post.dto.response.MemberInfo; | ||
|
||
public record ChatProposalResponse ( | ||
Long chatRoomId, | ||
String chatStatus, | ||
boolean isInquirer, | ||
MemberInfo chatPartner, | ||
String latestMessage, | ||
String messageType, | ||
String messageCreatedAt | ||
){} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아니면 더 좋게 바꿀 수 도 있을 것 같아요. Member 객체에서 memberId만 getter통해 가져오기 보다는 Member 객체가 스스로 일을 하도록 하는거죠. public class Member {
private Long memberId;
....
public boolean isSameId(Long otherId) {
return Objects.equals(otherId);
}
}
private Member getChatPartner(Member member, ChatRoom chatRoom) {
if (member.isSameId(chatRoom.getAnswerer()) {
return chatRoom.getInquirer();
}
if (member.isSameId(chatRoom.getInquirer()))) {
return chatRoom.getAnswerer();
}
throw new ValidationException(ChatErrorCode.UNAUTHORIZED_CHAT_ROOM);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵. 제안주신 부분 다음 PR에 참고해서 반영하겠습니다! 감사합니다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,13 @@ | |
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.dnd.gongmuin.chat.domain.ChatRoom; | ||
import com.dnd.gongmuin.chat.domain.ChatStatus; | ||
import com.dnd.gongmuin.chat.dto.ChatMessageMapper; | ||
import com.dnd.gongmuin.chat.dto.ChatRoomMapper; | ||
import com.dnd.gongmuin.chat.dto.request.CreateChatRoomRequest; | ||
import com.dnd.gongmuin.chat.dto.response.AcceptChatResponse; | ||
import com.dnd.gongmuin.chat.dto.response.ChatMessageResponse; | ||
import com.dnd.gongmuin.chat.dto.response.ChatProposalInfo; | ||
import com.dnd.gongmuin.chat.dto.response.ChatProposalResponse; | ||
import com.dnd.gongmuin.chat.dto.response.ChatRoomDetailResponse; | ||
import com.dnd.gongmuin.chat.dto.response.ChatRoomInfo; | ||
import com.dnd.gongmuin.chat.dto.response.ChatRoomSimpleResponse; | ||
|
@@ -94,11 +95,10 @@ public CreateChatRoomResponse createChatRoom(CreateChatRoomRequest request, Memb | |
} | ||
|
||
@Transactional(readOnly = true) | ||
public PageResponse<ChatRoomSimpleResponse> getChatRoomsByMember(Member member, List<String> chatStatuses, | ||
Pageable pageable) { | ||
public PageResponse<ChatRoomSimpleResponse> getChatRoomsByMember(Member member, Pageable pageable) { | ||
// 회원 채팅방 정보 가져오기 | ||
Slice<ChatRoomInfo> chatRoomInfos = chatRoomRepository.getChatRoomsByMember( | ||
member, ChatStatus.from(chatStatuses), pageable | ||
member, pageable | ||
); | ||
|
||
// chatRoomId 리스트 추출 | ||
|
@@ -118,6 +118,26 @@ public PageResponse<ChatRoomSimpleResponse> getChatRoomsByMember(Member member, | |
return new PageResponse<>(responses, responses.size(), chatRoomInfos.hasNext()); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public PageResponse<ChatProposalResponse> getChatProposalsByMember(Member member, Pageable pageable) { | ||
Slice<ChatProposalInfo> chatProposalInfos = chatRoomRepository.getChatProposalsByMember( | ||
member, pageable | ||
); | ||
|
||
List<Long> chatRoomIds = chatProposalInfos.stream() | ||
.map(ChatProposalInfo::chatRoomId) | ||
.toList(); | ||
|
||
List<LatestChatMessage> latestChatMessages | ||
= chatMessageQueryRepository.findLatestChatByChatRoomIds(chatRoomIds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. �가장 최신의 채팅 메시지 목록을 가져오는 메서드는 분리를 통해 중복을 줄일 수 있을 것 같아요! |
||
|
||
List<ChatProposalResponse> responses = getChatProposalResponse(latestChatMessages, | ||
chatProposalInfos); | ||
|
||
return new PageResponse<>(responses, responses.size(), chatProposalInfos.hasNext()); | ||
} | ||
|
||
|
||
@Transactional(readOnly = true) | ||
public ChatRoomDetailResponse getChatRoomById(Long chatRoomId, Member member) { | ||
ChatRoom chatRoom = getChatRoomById(chatRoomId); | ||
|
@@ -181,6 +201,26 @@ private List<ChatRoomSimpleResponse> getChatRoomSimpleResponses(List<LatestChatM | |
}).toList(); | ||
} | ||
|
||
private List<ChatProposalResponse> getChatProposalResponse(List<LatestChatMessage> latestChatMessages, | ||
Slice<ChatProposalInfo> chatProposalInfos) { | ||
// <chatRoomId, LatestMessage> -> 순서 보장 x | ||
Map<Long, LatestChatMessage> messageMap = latestChatMessages.stream() | ||
.collect(Collectors.toMap(LatestChatMessage::chatRoomId, message -> message)); | ||
|
||
// 최신순 정렬 및 변환 | ||
return chatProposalInfos.stream() | ||
.sorted( | ||
Comparator.comparing( | ||
(ChatProposalInfo info) -> messageMap.get(info.chatRoomId()).createdAt() | ||
).reversed()) | ||
.map(chatProposalInfo -> { | ||
LatestChatMessage latestMessage = messageMap.get(chatProposalInfo.chatRoomId()); | ||
return ChatRoomMapper.toChatProposalResponse( | ||
chatProposalInfo, latestMessage | ||
); | ||
}).toList(); | ||
} | ||
|
||
private ChatRoom getChatRoomById(Long id) { | ||
return chatRoomRepository.findById(id) | ||
.orElseThrow(() -> new NotFoundException(ChatErrorCode.NOT_FOUND_CHAT_ROOM)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getChatPartner()를 리팩토링해도 괜찮겠네요.
if문에서 바로 return을 던져주고 있기 때문에 else과 else if문을 사용을 줄여도 좋을 것 같아요