-
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.
Browse files
Browse the repository at this point in the history
[feat] 채팅방 목록 조회 API
- Loading branch information
Showing
12 changed files
with
205 additions
and
10 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
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/com/psr/psr/chat/dto/response/ChatRoomRes.kt
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,53 @@ | ||
package com.psr.psr.chat.dto.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat | ||
import com.psr.psr.chat.entity.ChatMessage | ||
import com.psr.psr.chat.entity.ChatRoom | ||
import com.psr.psr.user.entity.User | ||
import com.querydsl.core.annotations.QueryProjection | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
import java.time.LocalDateTime | ||
|
||
data class ChatRoomRes @QueryProjection constructor( | ||
@Schema(description = "채팅방 id", example = "1") | ||
val chatRoomId: Long, | ||
|
||
@Schema(description = "프로필 이미지", example = "imgUrl") | ||
val profileImgUrl: String? = null, | ||
|
||
@Schema(description = "닉네임", example = "마리") | ||
val nickname: String, | ||
|
||
@Schema(description = "최근 메시지", example = "안녕하세요!") | ||
val message: String? = null, | ||
|
||
@Schema(description = "최근 메시지 날짜", example = "11.30") | ||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM.dd") | ||
val date: LocalDateTime? = null, | ||
|
||
@Schema(description = "본인이 메시지 읽음 여부", example = "false") | ||
val isRead: Boolean, | ||
|
||
@Schema(description = "읽지 않은 메시지 수 (없으면 0)", example = "3") | ||
val numOfUnread: Int? = null | ||
|
||
) { | ||
companion object { | ||
fun toDto(chatRoom: ChatRoom, chatMessage: ChatMessage, partner: User, numOfUnread: Int): ChatRoomRes { | ||
val isRead: Boolean = | ||
if (chatMessage.senderUser == partner) chatMessage.isRead | ||
else true | ||
|
||
return ChatRoomRes( | ||
chatRoom.id!!, | ||
partner.imgUrl, | ||
partner.nickname, | ||
chatMessage.message, | ||
chatMessage.createdAt, | ||
isRead, | ||
numOfUnread | ||
) | ||
|
||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/psr/psr/chat/dto/response/GetChatRoomsRes.kt
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,15 @@ | ||
package com.psr.psr.chat.dto.response | ||
|
||
import com.querydsl.core.annotations.QueryProjection | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
|
||
data class GetChatRoomsRes @QueryProjection constructor( | ||
@Schema(description = "채팅방 리스트") | ||
val noticeLists: List<ChatRoomRes>? | ||
) { | ||
companion object { | ||
fun toDto(response: MutableList<ChatRoomRes>): GetChatRoomsRes? { | ||
return GetChatRoomsRes(response) | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/psr/psr/chat/repository/ChatMessageCustom.kt
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,11 @@ | ||
package com.psr.psr.chat.repository | ||
|
||
import com.psr.psr.chat.dto.response.GetChatRoomsRes | ||
import com.psr.psr.chat.entity.ChatRoom | ||
import com.psr.psr.user.entity.User | ||
|
||
|
||
interface ChatMessageCustom { | ||
fun getRecentChat(user: User, chatRooms: List<ChatRoom>): GetChatRoomsRes? | ||
|
||
} |
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
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/com/psr/psr/chat/repository/ChatMessageRepositoryImpl.kt
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,54 @@ | ||
package com.psr.psr.chat.repository | ||
|
||
import com.psr.psr.chat.dto.response.ChatRoomRes | ||
import com.psr.psr.chat.dto.response.GetChatRoomsRes | ||
import com.psr.psr.chat.entity.ChatMessage | ||
import com.psr.psr.chat.entity.ChatRoom | ||
import com.psr.psr.chat.entity.QChatMessage.chatMessage | ||
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS | ||
import com.psr.psr.user.entity.User | ||
import com.querydsl.jpa.impl.JPAQueryFactory | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class ChatMessageRepositoryImpl( | ||
private val queryFactory: JPAQueryFactory | ||
) : ChatMessageCustom { | ||
override fun getRecentChat(user: User, chatRooms: List<ChatRoom>): GetChatRoomsRes? { | ||
|
||
val response: MutableList<ChatRoomRes> = mutableListOf() | ||
|
||
for (chatRoom in chatRooms) { | ||
val chat: ChatMessage = queryFactory | ||
.selectFrom(chatMessage) | ||
.where( | ||
chatMessage.chatRoom.eq(chatRoom), | ||
chatMessage.status.eq(ACTIVE_STATUS), | ||
) | ||
.orderBy(chatMessage.updatedAt.desc()) | ||
.fetchFirst() | ||
|
||
val numOfUnread: Int = queryFactory | ||
.select(chatMessage) | ||
.from(chatMessage) | ||
.where( | ||
chatMessage.chatRoom.eq(chatRoom), | ||
chatMessage.status.eq(ACTIVE_STATUS), | ||
chatMessage.isRead.eq(false), | ||
!chatMessage.senderUser.eq(user) | ||
) | ||
.fetch() | ||
.size | ||
|
||
val partner: User = | ||
if (user == chatRoom.order.user) chatRoom.order.product.user | ||
else chatRoom.order.user | ||
|
||
val chatRoomRes: ChatRoomRes = ChatRoomRes.toDto(chatRoom, chat, partner, numOfUnread) | ||
response.add(chatRoomRes) | ||
} | ||
|
||
return GetChatRoomsRes.toDto(response) | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/psr/psr/chat/repository/ChatRoomCustom.kt
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,10 @@ | ||
package com.psr.psr.chat.repository | ||
|
||
import com.psr.psr.chat.entity.ChatRoom | ||
import com.psr.psr.user.entity.User | ||
|
||
|
||
interface ChatRoomCustom { | ||
fun getChatRooms(user: User): List<ChatRoom>? | ||
|
||
} |
3 changes: 1 addition & 2 deletions
3
src/main/kotlin/com/psr/psr/chat/repository/ChatRoomRepository.kt
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
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/psr/psr/chat/repository/ChatRoomRepositoryImpl.kt
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,25 @@ | ||
package com.psr.psr.chat.repository | ||
|
||
import com.psr.psr.chat.entity.ChatRoom | ||
import com.psr.psr.chat.entity.QChatRoom.chatRoom | ||
import com.psr.psr.global.Constant.UserStatus.UserStatus.ACTIVE_STATUS | ||
import com.psr.psr.user.entity.User | ||
import com.querydsl.jpa.impl.JPAQueryFactory | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class ChatRoomRepositoryImpl( | ||
private val queryFactory: JPAQueryFactory | ||
) : ChatRoomCustom { | ||
override fun getChatRooms(user: User): List<ChatRoom> { | ||
return queryFactory | ||
.selectFrom(chatRoom) | ||
.where( | ||
chatRoom.status.eq(ACTIVE_STATUS), | ||
(chatRoom.senderUser.eq(user)).or(chatRoom.receiverUser.eq(user)) | ||
) | ||
.orderBy(chatRoom.updatedAt.desc()) | ||
.fetch() | ||
} | ||
|
||
} |
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