diff --git a/src/main/java/kr/codesquad/secondhand/presentation/ChatController.java b/src/main/java/kr/codesquad/secondhand/presentation/ChatController.java index 66555654b..075df3196 100644 --- a/src/main/java/kr/codesquad/secondhand/presentation/ChatController.java +++ b/src/main/java/kr/codesquad/secondhand/presentation/ChatController.java @@ -8,6 +8,8 @@ import kr.codesquad.secondhand.presentation.dto.chat.ChatRequest; import kr.codesquad.secondhand.presentation.dto.chat.ChatRoomResponse; import kr.codesquad.secondhand.presentation.support.Auth; +import lombok.AllArgsConstructor; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; @@ -25,9 +27,8 @@ @RestController public class ChatController { - private final Map>, Long> chatRequests = new ConcurrentHashMap<>(); + private final Map>, ChatData> chatRequests = new ConcurrentHashMap<>(); private final Map>>, Long> chatRoomRequests = new ConcurrentHashMap<>(); - private final ChatLogService chatLogService; private final ChatRoomService chatRoomService; @@ -38,7 +39,7 @@ public DeferredResult> readAll( @Auth Long memberId) { DeferredResult> deferredResult = new DeferredResult<>(10000L, new ApiResponse<>(HttpStatus.OK.value(), List.of())); - chatRequests.put(deferredResult, messageId); + chatRequests.put(deferredResult, new ChatData(chatRoomId, messageId, memberId)); deferredResult.onCompletion(() -> chatRequests.remove(deferredResult)); @@ -89,7 +90,11 @@ public ApiResponse sendMessage(@Valid @RequestBody ChatRequest request, chatLogService.sendMessage(request.getMessage(), chatRoomId, senderId); for (var entry : chatRequests.entrySet()) { - ChatLogResponse messages = chatLogService.getMessages(chatRoomId, entry.getValue(), senderId); + ChatData chatData = entry.getValue(); + if (!chatData.getChatRoomId().equals(chatRoomId)) { + continue; + } + ChatLogResponse messages = chatLogService.getMessages(chatRoomId, chatData.getChatRoomId(), chatData.getTargetMemberId()); entry.getKey().setResult(new ApiResponse<>(HttpStatus.OK.value(), messages)); } @@ -110,4 +115,13 @@ public ApiResponse> createChatRoom(@PathVariable Long itemId, Long chatRoomId = chatRoomService.createChatRoom(itemId, senderId); return new ApiResponse<>(HttpStatus.CREATED.value(), Map.of("chatRoomId", chatRoomId)); } + + @Getter + @AllArgsConstructor + private static class ChatData { + + private Long chatRoomId; + private Long messageId; + private Long targetMemberId; + } } diff --git a/src/main/java/kr/codesquad/secondhand/presentation/dto/chat/ChatLogResponse.java b/src/main/java/kr/codesquad/secondhand/presentation/dto/chat/ChatLogResponse.java index 5956eae98..1ebf27c3b 100644 --- a/src/main/java/kr/codesquad/secondhand/presentation/dto/chat/ChatLogResponse.java +++ b/src/main/java/kr/codesquad/secondhand/presentation/dto/chat/ChatLogResponse.java @@ -13,5 +13,5 @@ public class ChatLogResponse { private String chatPartnerName; private ItemSimpleResponse item; private List chat; - private Long lastMessageId; + private Long nextMessageId; } diff --git a/src/main/java/kr/codesquad/secondhand/presentation/dto/item/ItemDetailResponse.java b/src/main/java/kr/codesquad/secondhand/presentation/dto/item/ItemDetailResponse.java index aa9605005..0da280e10 100644 --- a/src/main/java/kr/codesquad/secondhand/presentation/dto/item/ItemDetailResponse.java +++ b/src/main/java/kr/codesquad/secondhand/presentation/dto/item/ItemDetailResponse.java @@ -1,7 +1,5 @@ package kr.codesquad.secondhand.presentation.dto.item; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; import kr.codesquad.secondhand.domain.item.Item; import kr.codesquad.secondhand.domain.itemimage.ItemImage; import lombok.Builder; @@ -17,7 +15,6 @@ public class ItemDetailResponse { private final Boolean isSeller; private final List imageUrls; private final String seller; - @JsonInclude(Include.NON_NULL) private final String status; private final String title; private final String categoryName; @@ -27,9 +24,7 @@ public class ItemDetailResponse { private final int wishCount; private final int viewCount; private final Long price; - @JsonInclude(Include.NON_NULL) private final Boolean isInWishList; - @JsonInclude(Include.NON_NULL) private final Long chatRoomId; @Builder diff --git a/src/main/java/kr/codesquad/secondhand/repository/chat/ChatLogRepository.java b/src/main/java/kr/codesquad/secondhand/repository/chat/ChatLogRepository.java index 772930157..2865c54c8 100644 --- a/src/main/java/kr/codesquad/secondhand/repository/chat/ChatLogRepository.java +++ b/src/main/java/kr/codesquad/secondhand/repository/chat/ChatLogRepository.java @@ -14,5 +14,6 @@ public interface ChatLogRepository extends JpaRepository { @Query("UPDATE ChatLog chatLog SET chatLog.readCount = 0 WHERE chatLog.chatRoom.id = :chatRoomId") void updateReadCountByChatRoomId(@Param("chatRoomId") Long chatRoomId); - List findAllByChatRoom_IdAndIdIsGreaterThan(Long chatRoomId, Long id); + @Query("SELECT chatLog FROM ChatLog chatLog WHERE chatLog.chatRoom.id = :chatRoomId AND chatLog.id > :id") + List findAllByChatRoom_IdAndIdIsGreaterThan(@Param("chatRoomId") Long chatRoomId, @Param("id") Long id); } diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index c4b172f2c..856547524 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -1514,7 +1514,7 @@

상품 목록 Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Content-Type: application/json -Content-Length: 3190 +Content-Length: 3192 { "statusCode" : 200, @@ -1525,7 +1525,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737513", + "createdAt" : "2023-10-06T10:57:13.760221", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1536,7 +1536,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737527", + "createdAt" : "2023-10-06T10:57:13.760363", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1547,7 +1547,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.73753", + "createdAt" : "2023-10-06T10:57:13.760369", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1558,7 +1558,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737532", + "createdAt" : "2023-10-06T10:57:13.760371", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1569,7 +1569,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737533", + "createdAt" : "2023-10-06T10:57:13.760372", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1580,7 +1580,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737535", + "createdAt" : "2023-10-06T10:57:13.760373", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1591,7 +1591,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737536", + "createdAt" : "2023-10-06T10:57:13.760374", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1602,7 +1602,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737538", + "createdAt" : "2023-10-06T10:57:13.760375", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1613,7 +1613,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.737539", + "createdAt" : "2023-10-06T10:57:13.760377", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1624,7 +1624,7 @@

상품 목록 "thumbnailUrl" : "url", "title" : "title", "tradingRegion" : "범안 1동", - "createdAt" : "2023-10-05T21:06:34.73754", + "createdAt" : "2023-10-06T10:57:13.760378", "price" : 10000, "status" : "판매중", "sellerId" : "sellerId", @@ -1769,7 +1769,7 @@

상품 상세 Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers Content-Type: application/json -Content-Length: 421 +Content-Length: 473 { "statusCode" : 200, @@ -1781,12 +1781,14 @@

상품 상세 "status" : "판매중", "title" : "선풍기", "categoryName" : "가전/잡화", - "createdAt" : "2023-10-05T21:06:34.553765", + "createdAt" : "2023-10-06T10:57:13.415916", "content" : "content", "chatCount" : 1, "wishCount" : 10, "viewCount" : 100, - "price" : 10000 + "price" : 10000, + "isInWishList" : null, + "chatRoomId" : null } } @@ -3460,7 +3462,7 @@

@@ -3557,7 +3559,7 @@
Respons

채팅 메시지

-

data.lastMessageId

+

data.nextMessageId

Number

마지막으로 읽은 메시지 ID, 다음 요청으로 사용할 쿼리 파라미터

@@ -3572,7 +3574,7 @@
Respons diff --git a/src/test/java/kr/codesquad/secondhand/documentation/chat/ChatDocumentationTest.java b/src/test/java/kr/codesquad/secondhand/documentation/chat/ChatDocumentationTest.java index 7fe8e24ef..34ab34c04 100644 --- a/src/test/java/kr/codesquad/secondhand/documentation/chat/ChatDocumentationTest.java +++ b/src/test/java/kr/codesquad/secondhand/documentation/chat/ChatDocumentationTest.java @@ -90,7 +90,7 @@ void readAllChatLogs() throws Exception { fieldWithPath("data.chat[].messageId").type(NUMBER).description("채팅 메시지 ID"), fieldWithPath("data.chat[].isMe").type(BOOLEAN).description("내가 보낸 메시지인지 여부"), fieldWithPath("data.chat[].message").type(STRING).description("채팅 메시지"), - fieldWithPath("data.lastMessageId").type(NUMBER).description("마지막으로 읽은 메시지 ID, 다음 요청으로 사용할 쿼리 파라미터") + fieldWithPath("data.nextMessageId").type(NUMBER).description("마지막으로 읽은 메시지 ID, 다음 요청으로 사용할 쿼리 파라미터") ) )); } diff --git a/src/test/java/kr/codesquad/secondhand/documentation/item/ItemDocumentationTest.java b/src/test/java/kr/codesquad/secondhand/documentation/item/ItemDocumentationTest.java index bac303645..a56e80bb1 100644 --- a/src/test/java/kr/codesquad/secondhand/documentation/item/ItemDocumentationTest.java +++ b/src/test/java/kr/codesquad/secondhand/documentation/item/ItemDocumentationTest.java @@ -372,7 +372,9 @@ void read() throws Exception { fieldWithPath("data.chatCount").type(NUMBER).description("채팅 수"), fieldWithPath("data.wishCount").type(NUMBER).description("관심 수"), fieldWithPath("data.viewCount").type(NUMBER).description("조회 수"), - fieldWithPath("data.price").type(NUMBER).description("상품 가격") + fieldWithPath("data.price").type(NUMBER).description("상품 가격"), + fieldWithPath("data.isInWishList").ignored().type(BOOLEAN).description("관심 목록에 있는지 여부, 구매자인 경우에만 응답에 포함"), + fieldWithPath("data.chatRoomId").ignored().type(NUMBER).description("채팅방 아이디, 구매자인 경우에만 응답에 포함") ) )); }