@@ -6,11 +6,12 @@ import com.backgu.amaker.api.chat.dto.ChatQuery
6
6
import com.backgu.amaker.api.chat.dto.ChatWithUserDto
7
7
import com.backgu.amaker.api.chat.dto.DefaultChatWithUserDto
8
8
import com.backgu.amaker.api.chat.dto.EventChatWithUserDto
9
- import com.backgu.amaker.api.chat.service.query.ChatQueryService
10
9
import com.backgu.amaker.api.event.dto.EventWithUserDto
10
+ import com.backgu.amaker.application.chat.service.ChatQueryService
11
11
import com.backgu.amaker.application.chat.service.ChatRoomService
12
12
import com.backgu.amaker.application.chat.service.ChatRoomUserService
13
13
import com.backgu.amaker.application.chat.service.ChatService
14
+ import com.backgu.amaker.application.chat.service.ChatUserCacheService
14
15
import com.backgu.amaker.application.event.service.EventAssignedUserService
15
16
import com.backgu.amaker.application.event.service.EventService
16
17
import com.backgu.amaker.application.user.service.UserService
@@ -20,6 +21,7 @@ import com.backgu.amaker.domain.chat.Chat
20
21
import com.backgu.amaker.domain.chat.ChatRoom
21
22
import com.backgu.amaker.domain.chat.ChatRoomUser
22
23
import com.backgu.amaker.domain.chat.ChatType
24
+ import com.backgu.amaker.domain.chat.DefaultChatWithUser
23
25
import com.backgu.amaker.domain.event.Event
24
26
import com.backgu.amaker.domain.event.EventAssignedUser
25
27
import com.backgu.amaker.domain.user.User
@@ -36,6 +38,7 @@ class ChatFacadeService(
36
38
private val eventService : EventService ,
37
39
private val chatQueryService : ChatQueryService ,
38
40
private val eventAssignedUserService : EventAssignedUserService ,
41
+ private val chatUserCacheService : ChatUserCacheService ,
39
42
) {
40
43
@Transactional
41
44
fun createChat (
@@ -50,6 +53,8 @@ class ChatFacadeService(
50
53
val chat: Chat = chatService.save(chatRoom.createChat(user, chatCreateDto.content, chatType))
51
54
chatRoomService.save(chatRoom.updateLastChatId(chat))
52
55
56
+ chatUserCacheService.saveDefaultChat(chatRoomId, chat.createDefaultChatWithUser(user))
57
+
53
58
return DefaultChatWithUserDto .of(chat, user)
54
59
}
55
60
@@ -59,16 +64,37 @@ class ChatFacadeService(
59
64
chatQuery : ChatQuery ,
60
65
): ChatListDto {
61
66
markMostRecentChatAsRead(chatQuery.chatRoomId, userId)
62
- val chatList = chatQueryService.findPreviousChatList(chatQuery.chatRoomId, chatQuery.cursor, chatQuery.size)
67
+
68
+ val cachedChats =
69
+ chatUserCacheService.findPreviousChats(chatQuery.chatRoomId, chatQuery.cursor, chatQuery.size)
70
+ val dbQuerySize = chatQuery.size - cachedChats.size
71
+
72
+ if (dbQuerySize <= 0 ) {
73
+ return ChatListDto .of(
74
+ chatQuery,
75
+ cachedChats.map { ChatWithUserDto .of(it) },
76
+ )
77
+ }
78
+
79
+ val firstChatId = cachedChats.firstOrNull()?.id ? : chatQuery.cursor
80
+ val chatListFromDB =
81
+ chatQueryService.findPreviousChatList(chatQuery.chatRoomId, firstChatId, dbQuerySize)
63
82
64
83
val eventMap =
65
- eventService.findEventByIdsToMap(chatList .filter { ChatType .isEventChat(it.chatType) }.map { it.id })
84
+ eventService.findEventByIdsToMap(chatListFromDB .filter { ChatType .isEventChat(it.chatType) }.map { it.id })
66
85
val eventUserMap = eventAssignedUserService.findByEventIdsToEventIdMapped(eventMap.keys.toList())
67
86
val userMap = userService.findAllByUserIdsToMap(eventUserMap.values.flatten().map { it.userId })
68
87
69
88
return ChatListDto .of(
70
89
chatQuery,
71
- chatList.map { mapToChatWithUser(it, eventMap, eventUserMap, userMap) },
90
+ chatListFromDB.map {
91
+ mapToChatWithUser(
92
+ it,
93
+ eventMap,
94
+ eventUserMap,
95
+ userMap,
96
+ )
97
+ } + cachedChats.map { ChatWithUserDto .of(it) },
72
98
)
73
99
}
74
100
@@ -78,7 +104,17 @@ class ChatFacadeService(
78
104
chatQuery : ChatQuery ,
79
105
): ChatListDto {
80
106
markMostRecentChatAsRead(chatQuery.chatRoomId, userId)
107
+
108
+ println (" ===================" )
109
+ chatUserCacheService
110
+ .findAfterChats(chatQuery.chatRoomId, chatQuery.cursor, chatQuery.size)
111
+ ?.let { return ChatListDto .of(chatQuery, it.map { chatWithUser -> ChatWithUserDto .of(chatWithUser) }) }
112
+
113
+ println (" ===================" )
81
114
val chatList = chatQueryService.findAfterChatList(chatQuery.chatRoomId, chatQuery.cursor, chatQuery.size)
115
+ for (defaultChatWithUser in chatList) {
116
+ println (defaultChatWithUser.id)
117
+ }
82
118
83
119
val eventMap =
84
120
eventService.findEventByIdsToMap(chatList.filter { ChatType .isEventChat(it.chatType) }.map { it.id })
@@ -97,9 +133,17 @@ class ChatFacadeService(
97
133
chatRoomId : Long ,
98
134
): ChatWithUserDto <* > {
99
135
val chatRoomUser = markMostRecentChatAsRead(chatRoomId, userId)
136
+
137
+ val cachedChat =
138
+ chatRoomUser.lastReadChatId?.let {
139
+ chatUserCacheService.findChat(chatRoomId, it)
140
+ }
141
+
142
+ if (cachedChat != null ) return ChatWithUserDto .of(cachedChat)
143
+
100
144
val chat = chatQueryService.getOneWithUser(chatRoomUser.lastReadChatId)
101
145
102
- if (! ChatType .isEventChat(chat.chatType)) return chat
146
+ if (! ChatType .isEventChat(chat.chatType)) return DefaultChatWithUserDto .of( chat)
103
147
104
148
val event = eventService.getEventById(chat.id)
105
149
val eventAssignedUsers = eventAssignedUserService.findAllByEventId(event.id)
@@ -112,7 +156,8 @@ class ChatFacadeService(
112
156
)
113
157
}
114
158
115
- private fun markMostRecentChatAsRead (
159
+ @Transactional
160
+ fun markMostRecentChatAsRead (
116
161
chatRoomId : Long ,
117
162
userId : String ,
118
163
): ChatRoomUser {
@@ -123,7 +168,7 @@ class ChatFacadeService(
123
168
}
124
169
125
170
private fun mapToChatWithUser (
126
- chat : DefaultChatWithUserDto ,
171
+ chat : DefaultChatWithUser ,
127
172
eventMap : Map <Long , Event >,
128
173
eventUserMap : Map <Long , List <EventAssignedUser >>,
129
174
userMap : Map <String , User >,
@@ -134,6 +179,6 @@ class ChatFacadeService(
134
179
val finishedNumber = eventUserMap[event.id]?.count { it.isFinished } ? : 0
135
180
EventChatWithUserDto .of(chat, EventWithUserDto .of(event, eventUsers, finishedNumber))
136
181
} else {
137
- chat
182
+ DefaultChatWithUserDto .of( chat)
138
183
}
139
184
}
0 commit comments