Skip to content

Commit afef246

Browse files
authored
Merge pull request #125 from kjiyun/chat
#124 Fix: 채팅 좋아요 여부 확인 기능 추가
2 parents 9288627 + a05dbe8 commit afef246

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/main/java/com/memesphere/domain/chat/controller/ChatController.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.memesphere.domain.chat.dto.request.ChatRequest;
44
import com.memesphere.domain.chat.dto.response.ChatResponse;
55
import com.memesphere.domain.chat.service.ChatService;
6+
import com.memesphere.domain.user.entity.User;
67
import com.memesphere.global.apipayload.ApiResponse;
78
import com.memesphere.global.jwt.CustomUserDetails;
89
import io.swagger.v3.oas.annotations.Operation;
@@ -36,20 +37,22 @@ public ChatResponse chat(@DestinationVariable("coin_id") Long coin_id,
3637
@Operation(summary = "코인별 채팅 전체 메시지 조회 API",
3738
description = "특정 코인의 채팅방의 전체 메시지를 보여줍니다.")
3839
public ApiResponse<Page<ChatResponse>> getChatList(@PathVariable("coin_id") Long coin_id,
39-
Pageable pageable) {
40+
Pageable pageable,
41+
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
42+
User user = customUserDetails.getUser();
4043

41-
return ApiResponse.onSuccess(chatService.getChatList(coin_id, pageable));
44+
return ApiResponse.onSuccess(chatService.getChatList(coin_id, pageable, user));
4245
}
4346

4447
//최신 댓글 조회 Api
4548
@GetMapping("/chat/{coin_id}/latest")
4649
@Operation(summary = "코인별 최신 댓글 조회 API",
4750
description = "특정 코인에 대한 최신 댓글을 반환합니다. 요청 시 최신 댓글 하나만 가져옵니다.")
48-
public ApiResponse<ChatResponse> getLatestMessages(
49-
@PathVariable("coin_id") Long coin_id) {
50-
51+
public ApiResponse<ChatResponse> getLatestMessages(@PathVariable("coin_id") Long coin_id,
52+
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
53+
User user = customUserDetails.getUser();
5154
// 최신 댓글을 가져오는 서비스 메서드 호출
52-
ChatResponse latestMessage = chatService.getLatestMessages(coin_id);
55+
ChatResponse latestMessage = chatService.getLatestMessages(coin_id, user);
5356

5457
return ApiResponse.onSuccess(latestMessage);
5558
}

src/main/java/com/memesphere/domain/chat/converter/ChatConverter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.memesphere.domain.chat.dto.response.ChatResponse;
88
import com.memesphere.domain.user.entity.User;
99

10+
import java.util.stream.Collectors;
11+
1012
public class ChatConverter {
1113

1214
public static Chat toChat(MemeCoin memeCoin, ChatRequest chatRequest, User user) {
@@ -18,7 +20,7 @@ public static Chat toChat(MemeCoin memeCoin, ChatRequest chatRequest, User user)
1820
.build();
1921
}
2022

21-
public static ChatResponse toChatResponse(Chat chat) {
23+
public static ChatResponse toChatResponse(Chat chat, User user) {
2224

2325
return ChatResponse.builder()
2426
.id(chat.getId())
@@ -27,6 +29,7 @@ public static ChatResponse toChatResponse(Chat chat) {
2729
.likes(chat.getChatLikeList().size())
2830
.createdAt(chat.getCreatedAt())
2931
.nickname(chat.getUser().getNickname())
32+
.isLiked(chat.getChatLikeList().contains(user))
3033
.build();
3134
}
3235

src/main/java/com/memesphere/domain/chat/dto/response/ChatResponse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class ChatResponse {
2525
@Schema(description = "좋아요 수", example = "17")
2626
private int likes;
2727

28+
@Schema(description = "좋아요 여부", example = "True")
29+
private boolean isLiked;
30+
2831
@Schema(description = "전송 시간", example = "2025-01-01T00:00:00")
2932
private LocalDateTime createdAt;
3033
}

src/main/java/com/memesphere/domain/chat/service/ChatService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@ public ChatResponse saveMessage(Long coin_id, ChatRequest chatRequest) {
4141
Chat chat = ChatConverter.toChat(memeCoin, chatRequest, user);
4242
Chat savedChat = chatRepository.save(chat);
4343

44-
return ChatConverter.toChatResponse(savedChat);
44+
return ChatConverter.toChatResponse(savedChat, user);
4545
}
4646

4747
@Transactional
48-
public Page<ChatResponse> getChatList(Long coin_id, Pageable pageable) {
48+
public Page<ChatResponse> getChatList(Long coin_id, Pageable pageable, User user) {
4949

5050
Page<Chat> chatPage = chatRepository.findAllByMemeCoin_Id(coin_id, pageable);
51-
return chatPage.map(ChatConverter::toChatResponse);
51+
52+
return chatPage.map(chat -> ChatConverter.toChatResponse(chat, user));
5253
}
5354

5455
// 최신 댓글을 가져오는 메서드
5556
@Transactional
56-
public ChatResponse getLatestMessages(Long coin_id) {
57+
public ChatResponse getLatestMessages(Long coin_id, User user) {
5758

5859
MemeCoin memeCoin = memeCoinRepository.findById(coin_id)
5960
//id에 해당하는 밈코인 없을 때
@@ -68,7 +69,7 @@ public ChatResponse getLatestMessages(Long coin_id) {
6869
}
6970

7071
// 최신 댓글을 ChatResponse로 변환하여 반환
71-
return ChatConverter.toChatResponse(latestChat);
72+
return ChatConverter.toChatResponse(latestChat, user);
7273
}
7374

7475
@Transactional

0 commit comments

Comments
 (0)