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.
[feat] 채팅방 목록 조회시 가장 최근에 전송된 채팅방이 위쪽으로 오도록 코드 개선 (#144)
- Loading branch information
1 parent
6c3b17d
commit 235dacc
Showing
9 changed files
with
83 additions
and
23 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
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
42 changes: 42 additions & 0 deletions
42
backend/src/main/java/codesquard/app/api/chat/ChatService.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,42 @@ | ||
package codesquard.app.api.chat; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.context.request.async.DeferredResult; | ||
|
||
import codesquard.app.api.chat.response.ChatLogListResponse; | ||
import codesquard.app.api.response.ApiResponse; | ||
import codesquard.app.domain.oauth.support.Principal; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ChatService { | ||
|
||
private static final int DEFAULT_READ_MESSAGE_SIZE = 10; | ||
|
||
private final Map<DeferredResult<ApiResponse<ChatLogListResponse>>, Long> chatRequests = | ||
new ConcurrentHashMap<>(); | ||
|
||
private final ChatLogService chatLogService; | ||
|
||
public void onMessage(Long chatRoomId, Principal sender) { | ||
for (Map.Entry<DeferredResult<ApiResponse<ChatLogListResponse>>, Long> entry : this.chatRequests.entrySet()) { | ||
DeferredResult<ApiResponse<ChatLogListResponse>> key = entry.getKey(); | ||
Long cursor = entry.getValue(); | ||
key.setResult(ApiResponse.ok("채팅 메시지 목록 조회가 완료되었습니다.", | ||
chatLogService.readMessages(chatRoomId, sender, cursor, Pageable.ofSize(DEFAULT_READ_MESSAGE_SIZE)))); | ||
} | ||
} | ||
|
||
public void putMessageIndex(DeferredResult<ApiResponse<ChatLogListResponse>> deferredResult, Long messageIndex) { | ||
chatRequests.put(deferredResult, messageIndex); | ||
} | ||
|
||
public void removeMessageIndex(DeferredResult<ApiResponse<ChatLogListResponse>> deferredResult) { | ||
chatRequests.remove(deferredResult); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import static org.hamcrest.Matchers.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.anyLong; | ||
import static org.mockito.BDDMockito.anyString; | ||
import static org.mockito.BDDMockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; | ||
|
@@ -41,6 +42,7 @@ | |
import codesquard.app.api.chat.response.ChatLogListResponse; | ||
import codesquard.app.api.chat.response.ChatLogMessageResponse; | ||
import codesquard.app.api.chat.response.ChatLogSendResponse; | ||
import codesquard.app.api.member.MemberService; | ||
import codesquard.app.domain.category.Category; | ||
import codesquard.app.domain.chat.ChatLog; | ||
import codesquard.app.domain.chat.ChatRoom; | ||
|
@@ -60,6 +62,12 @@ class ChatLogRestControllerTest extends ControllerTestSupport { | |
@MockBean | ||
private ChatLogService chatLogService; | ||
|
||
@MockBean | ||
private ChatService chatService; | ||
|
||
@MockBean | ||
private MemberService memberService; | ||
|
||
@Autowired | ||
private PageableHandlerMethodArgumentResolver pageableHandlerMethodArgumentResolver; | ||
|
||
|
@@ -99,6 +107,11 @@ public void sendMessage() throws Exception { | |
any(Principal.class))) | ||
.willReturn(response); | ||
|
||
Member receiver = createMember("avatarUrl", "[email protected]", "bruni"); | ||
given(memberService.findMemberByLoginId( | ||
anyString() | ||
)).willReturn(receiver); | ||
|
||
// when & then | ||
mockMvc.perform(post("/api/chats/1") | ||
.content(objectMapper.writeValueAsString(requestBody)) | ||
|
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