diff --git a/backend/build.gradle b/backend/build.gradle index 892761e63..5742d09ca 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -12,8 +12,9 @@ plugins { id 'com.ewerk.gradle.plugins.querydsl' version '1.0.10' } +project.base.archivesBaseName('second_hand') +version = '0.1.0-SNAPSHOT' group = 'codesquard' -version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '11' diff --git a/backend/src/main/java/codesquard/app/api/chat/ChatLogRestController.java b/backend/src/main/java/codesquard/app/api/chat/ChatLogRestController.java index 52c4e6d4a..0d9d57d49 100644 --- a/backend/src/main/java/codesquard/app/api/chat/ChatLogRestController.java +++ b/backend/src/main/java/codesquard/app/api/chat/ChatLogRestController.java @@ -49,20 +49,20 @@ public ApiResponse sendMessage( @GetMapping("/chats/{chatRoomId}") public DeferredResult> readMessages( @PathVariable Long chatRoomId, - @RequestParam(required = false, defaultValue = "0") Long messageIndex, + @RequestParam(required = false, defaultValue = "0") Long messageId, @PageableDefault Pageable pageable, @AuthPrincipal Principal principal) { - log.info("메시지 읽기 요청 : chatRoomId={}, cursor={}, pageable={}, 요청한 아이디={}", chatRoomId, messageIndex, pageable, + log.info("메시지 읽기 요청 : chatRoomId={}, cursor={}, pageable={}, 요청한 아이디={}", chatRoomId, messageId, pageable, principal.getLoginId()); DeferredResult> deferredResult = new DeferredResult<>(10000L); - chatService.putMessageIndex(deferredResult, messageIndex); + chatService.putMessageIndex(deferredResult, messageId); deferredResult.onCompletion(() -> chatService.removeMessageIndex(deferredResult)); deferredResult.onTimeout(() -> deferredResult.setErrorResult( ApiResponse.of(HttpStatus.REQUEST_TIMEOUT, "새로운 채팅 메시지가 존재하지 않습니다.", Collections.emptyList()))); - ChatLogListResponse response = chatLogService.readMessages(chatRoomId, principal, messageIndex, pageable); + ChatLogListResponse response = chatLogService.readMessages(chatRoomId, principal, messageId, pageable); if (!response.isEmptyChat()) { deferredResult.setResult(ApiResponse.ok("채팅 메시지 목록 조회가 완료되었습니다.", response)); diff --git a/backend/src/main/java/codesquard/app/api/chat/ChatLogService.java b/backend/src/main/java/codesquard/app/api/chat/ChatLogService.java index 3945f9c70..134b59360 100644 --- a/backend/src/main/java/codesquard/app/api/chat/ChatLogService.java +++ b/backend/src/main/java/codesquard/app/api/chat/ChatLogService.java @@ -70,14 +70,13 @@ public ChatLogListResponse readMessages(Long chatRoomId, Principal principal, Lo boolean hasNext = slice.hasNext(); Long nextCursor = getNextCursor(messageResponses, hasNext); - return new ChatLogListResponse(chatPartnerName, ChatLogItemResponse.from(item), messageResponses, hasNext, - nextCursor); + return new ChatLogListResponse(chatPartnerName, ChatLogItemResponse.from(item), messageResponses, nextCursor); } private Long getNextCursor(List contents, boolean hasNext) { Long nextCursor = null; if (hasNext) { - nextCursor = contents.get(contents.size() - 1).getMessageIndex(); + nextCursor = contents.get(contents.size() - 1).getMessageId(); } return nextCursor; } diff --git a/backend/src/main/java/codesquard/app/api/chat/ChatRoomService.java b/backend/src/main/java/codesquard/app/api/chat/ChatRoomService.java index 9a010f541..0d9f5488e 100644 --- a/backend/src/main/java/codesquard/app/api/chat/ChatRoomService.java +++ b/backend/src/main/java/codesquard/app/api/chat/ChatRoomService.java @@ -3,6 +3,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; @@ -19,6 +20,7 @@ import codesquard.app.api.chat.response.ChatRoomListResponse; import codesquard.app.api.errors.errorcode.ErrorCode; import codesquard.app.api.errors.exception.NotFoundResourceException; +import codesquard.app.domain.chat.ChatLog; import codesquard.app.domain.chat.ChatLogCountRepository; import codesquard.app.domain.chat.ChatLogRepository; import codesquard.app.domain.chat.ChatRoom; @@ -79,6 +81,8 @@ public ChatRoomListResponse readAllChatRoom(Principal principal, Pageable pageab List itemIds = itemRepository.findAllByMemberId(principal.getMemberId()).stream() .map(Item::getId) .collect(Collectors.toUnmodifiableList()); + log.info("회원이 판매하는 상품 아이디 : {}", itemIds); + // 1. 내가 팔고 있는 상품들의 등록번호 조회 조건 BooleanExpression chatRoomOfSellingItemsCondition = QChatRoom.chatRoom.item.id.in(itemIds); // 2. 내가 사려고 하는 상품에 대한 채팅방 목록 조회 조건 @@ -87,9 +91,11 @@ public ChatRoomListResponse readAllChatRoom(Principal principal, Pageable pageab whereBuilder.andAnyOf(chatRoomOfSellingItemsCondition, chatRoomOfBuyingItemsCondition); Slice slice = chatRoomPaginationRepository.searchBySlice(whereBuilder, pageable); + log.info("채팅방 페이징 조회 결과 개수 : {}", slice.getSize()); List contents = slice.getContent().stream() .map(getChatRoomItemResponseMapper(newMessageMap, principal)) + .filter(Objects::nonNull) .sorted(Comparator.comparing(ChatRoomItemResponse::getLastSendTime).reversed()) .collect(Collectors.toUnmodifiableList()); boolean hasNext = slice.hasNext(); @@ -101,13 +107,19 @@ public ChatRoomListResponse readAllChatRoom(Principal principal, Pageable pageab private Function getChatRoomItemResponseMapper(Map newMessageMap, Principal principal) { - return chatRoom -> ChatRoomItemResponse.of( - chatRoom, - chatRoom.getItem(), - getChatRoomPartner(principal, chatRoom), - chatLogRepository.findFirstByChatRoomIdOrderByCreatedAtDesc(chatRoom.getId()) - .orElseThrow(() -> new NotFoundResourceException(ErrorCode.NOT_FOUND_CHAT_LOG)), - newMessageMap.getOrDefault(chatRoom.getId(), 0L)); + return chatRoom -> { + ChatLog chatLog = chatLogRepository.findFirstByChatRoomIdOrderByCreatedAtDesc(chatRoom.getId()) + .orElse(null); + if (chatLog == null) { + return null; + } + return ChatRoomItemResponse.of( + chatRoom, + chatRoom.getItem(), + getChatRoomPartner(principal, chatRoom), + chatLog, + newMessageMap.getOrDefault(chatRoom.getId(), 0L)); + }; } private Member getChatRoomPartner(Principal principal, ChatRoom chatRoom) { @@ -138,6 +150,7 @@ public ChatRoomListResponse readAllChatRoomByItem(Long itemId, Principal princip List contents = slice.getContent().stream() .map(getChatRoomItemResponseMapper(newMessageMap, principal)) + .filter(Objects::nonNull) .sorted(Comparator.comparing(ChatRoomItemResponse::getLastSendTime).reversed()) .collect(Collectors.toUnmodifiableList()); boolean hasNext = slice.hasNext(); diff --git a/backend/src/main/java/codesquard/app/api/chat/response/ChatLogListResponse.java b/backend/src/main/java/codesquard/app/api/chat/response/ChatLogListResponse.java index ecd8bb864..6413d5a13 100644 --- a/backend/src/main/java/codesquard/app/api/chat/response/ChatLogListResponse.java +++ b/backend/src/main/java/codesquard/app/api/chat/response/ChatLogListResponse.java @@ -1,7 +1,5 @@ package codesquard.app.api.chat.response; -import static codesquard.app.api.item.response.ItemResponses.*; - import java.util.Collections; import java.util.List; @@ -15,22 +13,14 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor public class ChatLogListResponse { + private String chatPartnerName; private ChatLogItemResponse item; private List chat; - private Paging paging; - - public ChatLogListResponse(String chatPartnerName, ChatLogItemResponse item, List chat, - boolean hasNext, Long nextCursor) { - this.chatPartnerName = chatPartnerName; - this.item = item; - this.chat = chat; - this.paging = Paging.create(nextCursor, hasNext); - } + private Long nextMessageId; public static ChatLogListResponse emptyResponse(String chatPartnerName, Item item) { - return new ChatLogListResponse(chatPartnerName, ChatLogItemResponse.from(item), Collections.emptyList(), false, - null); + return new ChatLogListResponse(chatPartnerName, ChatLogItemResponse.from(item), Collections.emptyList(), null); } public boolean isEmptyChat() { diff --git a/backend/src/main/java/codesquard/app/api/chat/response/ChatLogMessageResponse.java b/backend/src/main/java/codesquard/app/api/chat/response/ChatLogMessageResponse.java index 0c56b5ebd..4a189374b 100644 --- a/backend/src/main/java/codesquard/app/api/chat/response/ChatLogMessageResponse.java +++ b/backend/src/main/java/codesquard/app/api/chat/response/ChatLogMessageResponse.java @@ -11,7 +11,7 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE) public class ChatLogMessageResponse { - private Long messageIndex; + private Long messageId; private Boolean isMe; private String message; diff --git a/backend/src/main/java/codesquard/app/api/chat/response/ChatRoomCreateResponse.java b/backend/src/main/java/codesquard/app/api/chat/response/ChatRoomCreateResponse.java index 79aeb4172..607416d0d 100644 --- a/backend/src/main/java/codesquard/app/api/chat/response/ChatRoomCreateResponse.java +++ b/backend/src/main/java/codesquard/app/api/chat/response/ChatRoomCreateResponse.java @@ -1,7 +1,5 @@ package codesquard.app.api.chat.response; -import com.fasterxml.jackson.annotation.JsonProperty; - import codesquard.app.domain.chat.ChatRoom; import lombok.AccessLevel; import lombok.AllArgsConstructor; @@ -12,9 +10,8 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE) public class ChatRoomCreateResponse { - - @JsonProperty(value = "chatRoomId") - private Long id; + + private Long chatRoomId; public static ChatRoomCreateResponse from(ChatRoom chatRoom) { return new ChatRoomCreateResponse(chatRoom.getId()); diff --git a/backend/src/main/java/codesquard/app/domain/chat/ChatLogPaginationRepository.java b/backend/src/main/java/codesquard/app/domain/chat/ChatLogPaginationRepository.java index 9831bfaf4..c4d7b1f42 100644 --- a/backend/src/main/java/codesquard/app/domain/chat/ChatLogPaginationRepository.java +++ b/backend/src/main/java/codesquard/app/domain/chat/ChatLogPaginationRepository.java @@ -23,7 +23,7 @@ public class ChatLogPaginationRepository { public Slice searchBySlice(BooleanBuilder whereBuilder, Pageable pageable) { List chatLogs = queryFactory.selectFrom(chatLog) .where(whereBuilder) - .orderBy(chatLog.id.asc()) + .orderBy(chatLog.id.asc(), chatLog.createdAt.asc()) .limit(pageable.getPageSize() + 1) .fetch(); return checkLastPage(pageable, chatLogs); diff --git a/backend/src/test/java/codesquard/app/api/chat/ChatLogRestControllerTest.java b/backend/src/test/java/codesquard/app/api/chat/ChatLogRestControllerTest.java index ca1a28bef..6a6fdb382 100644 --- a/backend/src/test/java/codesquard/app/api/chat/ChatLogRestControllerTest.java +++ b/backend/src/test/java/codesquard/app/api/chat/ChatLogRestControllerTest.java @@ -140,7 +140,7 @@ public void readMessages() throws Exception { ChatLog chatLog = new ChatLog("안녕하세요. 롤러블레이브를 사고 싶습니다. 만원만 깍아주세요.", "23Yong", "carlynne", chatRoom, 1); ChatLogMessageResponse messageResponse = ChatLogMessageResponse.from(chatLog, Principal.from(buyer)); ChatLogListResponse response = new ChatLogListResponse("carlynne", itemResponse, List.of(messageResponse), - false, null); + null); given(chatLogService.readMessages(anyLong(), any(Principal.class), anyLong(), any(Pageable.class))) .willReturn(response); int chatRoomId = 1; @@ -168,7 +168,7 @@ public void readMessages() throws Exception { public void readMessagesWithTimeout() throws Exception { // given ChatLogItemResponse itemResponse = null; - ChatLogListResponse response = new ChatLogListResponse("carlynne", itemResponse, List.of(), false, null); + ChatLogListResponse response = new ChatLogListResponse("carlynne", itemResponse, List.of(), null); given(chatLogService.readMessages( anyLong(), any(Principal.class), diff --git a/backend/src/test/java/codesquard/app/api/chat/ChatRoomRestControllerTest.java b/backend/src/test/java/codesquard/app/api/chat/ChatRoomRestControllerTest.java index 858e5992c..1efeb0be3 100644 --- a/backend/src/test/java/codesquard/app/api/chat/ChatRoomRestControllerTest.java +++ b/backend/src/test/java/codesquard/app/api/chat/ChatRoomRestControllerTest.java @@ -9,7 +9,6 @@ import static org.mockito.BDDMockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.HashMap; @@ -27,7 +26,6 @@ import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import codesquard.app.ControllerTestSupport; @@ -78,7 +76,7 @@ public void setup() { public void createChatRoom() throws Exception { // given Map responseBody = new HashMap<>(); - responseBody.put("id", 1L); + responseBody.put("chatRoomId", 1L); ChatRoomCreateResponse response = objectMapper.readValue(objectMapper.writeValueAsString(responseBody), ChatRoomCreateResponse.class); @@ -93,7 +91,7 @@ public void createChatRoom() throws Exception { .andExpect(status().isCreated()) .andExpect(jsonPath("statusCode").value(equalTo(201))) .andExpect(jsonPath("message").value(equalTo("채팅방 생성을 완료하였습니다."))) - .andExpect(jsonPath("data.id").value(equalTo(1))); + .andExpect(jsonPath("data.chatRoomId").value(equalTo(1))); } @DisplayName("회원은 채팅방 목록을 요청한다") @@ -120,11 +118,7 @@ public void readAllChatRoom() throws Exception { .willReturn(response); // when & then - MvcResult asyncListener = mockMvc.perform(get("/api/chats")) - .andExpect(request().asyncStarted()) - .andReturn(); - - mockMvc.perform(asyncDispatch(asyncListener)) + mockMvc.perform(get("/api/chats")) .andExpect(status().isOk()) .andExpect(jsonPath("statusCode").value(equalTo(200))) .andExpect(jsonPath("message").value(equalTo("채팅방 목록 조회를 완료하였습니다."))) diff --git a/backend/src/test/java/codesquard/app/api/chat/ChatRoomServiceTest.java b/backend/src/test/java/codesquard/app/api/chat/ChatRoomServiceTest.java index 8b5ecec3f..3714616b9 100644 --- a/backend/src/test/java/codesquard/app/api/chat/ChatRoomServiceTest.java +++ b/backend/src/test/java/codesquard/app/api/chat/ChatRoomServiceTest.java @@ -117,7 +117,7 @@ public void createChatRoom() { // then assertAll(() -> { assertThat(response) - .extracting("id") + .extracting("chatRoomId") .isNotNull(); }); }