forked from codesquad-members-2023/second-hand-max
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from max2023-4th-project-01/feat/chat-list
feat: 채팅 목록 조회 기능 구현 #89
- Loading branch information
Showing
11 changed files
with
279 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
be/src/main/java/kr/codesquad/chat/dto/response/ChatMemberDataResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package kr.codesquad.chat.dto.response; | ||
|
||
import kr.codesquad.chat.dto.vo.ChatRoomListVo; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ChatMemberDataResponse { | ||
private Long id; | ||
private String profileImageUrl; | ||
private String nickname; | ||
|
||
public static ChatMemberDataResponse from(ChatRoomListVo chatRoomListVo) { | ||
return ChatMemberDataResponse.builder().id(chatRoomListVo.getSenderId()).profileImageUrl( | ||
chatRoomListVo.getSenderProfileImageUrl()).nickname(chatRoomListVo.getSenderNickname()).build(); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
be/src/main/java/kr/codesquad/chat/dto/response/ChatRoomListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package kr.codesquad.chat.dto.response; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ChatRoomListResponse { | ||
private final List<UserChatRoomListResponse> chatRooms; | ||
} |
17 changes: 17 additions & 0 deletions
17
be/src/main/java/kr/codesquad/chat/dto/response/ItemDataResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package kr.codesquad.chat.dto.response; | ||
|
||
import kr.codesquad.chat.dto.vo.ChatRoomListVo; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ItemDataResponse { | ||
private Long id; | ||
private String thumbnailUrl; | ||
|
||
public static ItemDataResponse from(ChatRoomListVo chatRoomListVo) { | ||
return ItemDataResponse.builder().id(chatRoomListVo.getItemId()).thumbnailUrl( | ||
chatRoomListVo.getItemThumbnailUrl()).build(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
be/src/main/java/kr/codesquad/chat/dto/response/UserChatRoomListResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package kr.codesquad.chat.dto.response; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Map; | ||
|
||
import kr.codesquad.chat.dto.vo.ChatRoomListVo; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class UserChatRoomListResponse { | ||
private Long id; | ||
private ChatMemberDataResponse chatMember; | ||
private String recentMessage; | ||
private int unreadCount; | ||
private ZonedDateTime updatedAt; | ||
private ItemDataResponse item; | ||
|
||
public static UserChatRoomListResponse of(ChatRoomListVo chatRoom, Map<Long, Integer> unreadCounts) { | ||
return UserChatRoomListResponse.builder() | ||
.id(chatRoom.getId()) | ||
.chatMember(ChatMemberDataResponse.from(chatRoom)) | ||
.recentMessage(chatRoom.getRecentMessage()) | ||
.unreadCount(unreadCounts.getOrDefault(chatRoom.getId(), 0)) | ||
.updatedAt(chatRoom.getUpdatedAt()) | ||
.item(ItemDataResponse.from(chatRoom)) | ||
.build(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
be/src/main/java/kr/codesquad/chat/dto/vo/ChatMessageCountDataVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package kr.codesquad.chat.dto.vo; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ChatMessageCountDataVo { | ||
private Long chatRoomId; | ||
private boolean isRead; | ||
private Long senderId; | ||
|
||
@Builder | ||
public ChatMessageCountDataVo(Long chatRoomId, boolean isRead, Long senderId) { | ||
this.chatRoomId = chatRoomId; | ||
this.isRead = isRead; | ||
this.senderId = senderId; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
be/src/main/java/kr/codesquad/chat/dto/vo/ChatRoomListVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package kr.codesquad.chat.dto.vo; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ChatRoomListVo { | ||
private Long id; | ||
private Long senderId; | ||
private String senderProfileImageUrl; | ||
private String senderNickname; | ||
private String recentMessage; | ||
private int unreadCount; | ||
private ZonedDateTime updatedAt; | ||
private Long itemId; | ||
private String itemThumbnailUrl; | ||
|
||
@Builder | ||
public ChatRoomListVo(Long id, Long senderId, String senderProfileImageUrl, String senderNickname, | ||
String recentMessage, | ||
int unreadCount, ZonedDateTime updatedAt, Long itemId, String itemThumbnailUrl) { | ||
this.id = id; | ||
this.senderId = senderId; | ||
this.senderProfileImageUrl = senderProfileImageUrl; | ||
this.senderNickname = senderNickname; | ||
this.recentMessage = recentMessage; | ||
this.unreadCount = unreadCount; | ||
this.updatedAt = updatedAt; | ||
this.itemId = itemId; | ||
this.itemThumbnailUrl = itemThumbnailUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
be/src/main/java/kr/codesquad/chat/repository/ChatRoomListRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package kr.codesquad.chat.repository; | ||
|
||
import static kr.codesquad.chat.entity.QChatMessage.*; | ||
import static kr.codesquad.chat.entity.QChatRoom.*; | ||
import static kr.codesquad.item.entity.QItem.*; | ||
import static kr.codesquad.user.entity.QUser.*; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.JPAExpressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
|
||
import kr.codesquad.chat.dto.vo.ChatMessageCountDataVo; | ||
import kr.codesquad.chat.dto.vo.ChatRoomListVo; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class ChatRoomListRepository { | ||
private final JPAQueryFactory queryFactory; | ||
|
||
public List<ChatRoomListVo> findAllBy(Long itemId, Long userId) { | ||
return queryFactory | ||
.select(Projections.fields(ChatRoomListVo.class, | ||
chatRoom.id, | ||
user.id.as("senderId"), | ||
user.profileImageUrl.as("senderProfileImageUrl"), | ||
user.nickname.as("senderNickname"), | ||
item.id.as("itemId"), | ||
item.thumbnailUrl.as("itemThumbnailUrl"), | ||
chatMessage.content.as("recentMessage"), | ||
chatMessage.createdAt.as("updatedAt") | ||
)) | ||
.from(chatRoom) | ||
.leftJoin(user).on(chatRoom.senderId.eq(user.id)) | ||
.leftJoin(item).on(chatRoom.itemId.eq(item.id)) | ||
.leftJoin(chatMessage).on( | ||
chatMessage.chatRoomId.eq(chatRoom.id) | ||
.and(chatMessage.createdAt.eq( | ||
JPAExpressions | ||
.select(chatMessage.createdAt.max()) | ||
.from(chatMessage) | ||
.where(chatMessage.chatRoomId.eq(chatRoom.id)) | ||
)) | ||
) | ||
.where(item.userId.eq(userId).or(chatRoom.senderId.eq(userId)), equalItemId(itemId)).fetch(); | ||
} | ||
|
||
public List<ChatMessageCountDataVo> countUnreadChatMessage(Long userId) { | ||
return queryFactory | ||
.select(Projections.fields(ChatMessageCountDataVo.class, | ||
chatRoom.id.as("chatRoomId"), | ||
chatMessage.isRead, | ||
chatMessage.senderId | ||
)) | ||
.from(chatRoom) | ||
.leftJoin(item).on(chatRoom.itemId.eq(item.id)) | ||
.leftJoin(chatMessage).on(chatMessage.chatRoomId.eq(chatRoom.id)) | ||
.where(chatRoom.senderId.eq(userId).or(item.userId.eq(userId)), chatMessage.isRead.eq(false)) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression equalItemId(Long itemId) { | ||
if (itemId == null) { | ||
return null; | ||
} | ||
|
||
return item.id.eq(itemId); | ||
} | ||
|
||
private BooleanExpression equalSenderId(Long userId) { | ||
return chatRoom.senderId.eq(userId); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters