Skip to content

Commit

Permalink
상품 상세 페이지 응답 형식 통일 (#148)
Browse files Browse the repository at this point in the history
* feat: 상품 상세 페이지 응답 통일

* fix: `lastMessageId`대신` `nextMessageId`로 수정

* fix: null 필드 ignored 처리

* docs: API문서 수정

* fix: 채팅 메시지 로직 수정

* fix: 채팅 메시지 로직 수정

* fix: 채팅 메시지 로직 수정
  • Loading branch information
23Yong authored Oct 6, 2023
1 parent 0e3e1e1 commit 2fb1870
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +27,8 @@
@RestController
public class ChatController {

private final Map<DeferredResult<ApiResponse<ChatLogResponse>>, Long> chatRequests = new ConcurrentHashMap<>();
private final Map<DeferredResult<ApiResponse<ChatLogResponse>>, ChatData> chatRequests = new ConcurrentHashMap<>();
private final Map<DeferredResult<ApiResponse<CustomSlice<ChatRoomResponse>>>, Long> chatRoomRequests = new ConcurrentHashMap<>();

private final ChatLogService chatLogService;
private final ChatRoomService chatRoomService;

Expand All @@ -38,7 +39,7 @@ public DeferredResult<ApiResponse<ChatLogResponse>> readAll(
@Auth Long memberId) {
DeferredResult<ApiResponse<ChatLogResponse>> 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));

Expand Down Expand Up @@ -89,7 +90,11 @@ public ApiResponse<Void> 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));
}

Expand All @@ -110,4 +115,13 @@ public ApiResponse<Map<String, Long>> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class ChatLogResponse {
private String chatPartnerName;
private ItemSimpleResponse item;
private List<SimpleChatLog> chat;
private Long lastMessageId;
private Long nextMessageId;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +15,6 @@ public class ItemDetailResponse {
private final Boolean isSeller;
private final List<String> imageUrls;
private final String seller;
@JsonInclude(Include.NON_NULL)
private final String status;
private final String title;
private final String categoryName;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public interface ChatLogRepository extends JpaRepository<ChatLog, Long> {
@Query("UPDATE ChatLog chatLog SET chatLog.readCount = 0 WHERE chatLog.chatRoom.id = :chatRoomId")
void updateReadCountByChatRoomId(@Param("chatRoomId") Long chatRoomId);

List<ChatLog> findAllByChatRoom_IdAndIdIsGreaterThan(Long chatRoomId, Long id);
@Query("SELECT chatLog FROM ChatLog chatLog WHERE chatLog.chatRoom.id = :chatRoomId AND chatLog.id > :id")
List<ChatLog> findAllByChatRoom_IdAndIdIsGreaterThan(@Param("chatRoomId") Long chatRoomId, @Param("id") Long id);
}
36 changes: 19 additions & 17 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Content-Length: 3190
Content-Length: 3192

{
"statusCode" : 200,
Expand All @@ -1525,7 +1525,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1536,7 +1536,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1547,7 +1547,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1558,7 +1558,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1569,7 +1569,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1580,7 +1580,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1591,7 +1591,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1602,7 +1602,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1613,7 +1613,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand All @@ -1624,7 +1624,7 @@ <h3 id="_상품_목록"><a class="link" href="#_상품_목록">상품 목록</a>
"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",
Expand Down Expand Up @@ -1769,7 +1769,7 @@ <h3 id="_상품_상세"><a class="link" href="#_상품_상세">상품 상세</a>
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Content-Length: 421
Content-Length: 473

{
"statusCode" : 200,
Expand All @@ -1781,12 +1781,14 @@ <h3 id="_상품_상세"><a class="link" href="#_상품_상세">상품 상세</a>
"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
}
}</code></pre>
</div>
Expand Down Expand Up @@ -3460,7 +3462,7 @@ <h3 id="_채팅_메시지_목록_조회"><a class="link" href="#_채팅_메시
"isMe" : false,
"message" : "네 안녕하세요"
} ],
"lastMessageId" : 12
"nextMessageId" : 12
}
}</code></pre>
</div>
Expand Down Expand Up @@ -3557,7 +3559,7 @@ <h5 id="_response_fields_21"><a class="link" href="#_response_fields_21">Respons
<td class="tableblock halign-left valign-top"><p class="tableblock">채팅 메시지</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>data.lastMessageId</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>data.nextMessageId</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Number</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">마지막으로 읽은 메시지 ID, 다음 요청으로 사용할 쿼리 파라미터</p></td>
</tr>
Expand All @@ -3572,7 +3574,7 @@ <h5 id="_response_fields_21"><a class="link" href="#_response_fields_21">Respons
<div id="footer">
<div id="footer-text">
Version 0.0.1-SNAPSHOT<br>
Last updated 2023-10-05 21:02:45 +0900
Last updated 2023-10-06 10:24:19 +0900
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/github.min.css">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, 다음 요청으로 사용할 쿼리 파라미터")
)
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("채팅방 아이디, 구매자인 경우에만 응답에 포함")
)
));
}
Expand Down

0 comments on commit 2fb1870

Please sign in to comment.