From d4172bf6a8771ca759b3973bebe6b1bca5add738 Mon Sep 17 00:00:00 2001 From: dainshon Date: Sat, 17 Feb 2024 04:38:41 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=B1=84=ED=8C=85=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=EC=8B=9C=20userId=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit user별 채팅 가져오기 위함 --- .../java/com/kuit/chatdiary/controller/ChatController.java | 6 +++--- .../java/com/kuit/chatdiary/repository/ChatRepository.java | 5 ++++- src/main/java/com/kuit/chatdiary/service/ChatService.java | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/kuit/chatdiary/controller/ChatController.java b/src/main/java/com/kuit/chatdiary/controller/ChatController.java index b8f7d350..631efc30 100644 --- a/src/main/java/com/kuit/chatdiary/controller/ChatController.java +++ b/src/main/java/com/kuit/chatdiary/controller/ChatController.java @@ -20,11 +20,11 @@ public class ChatController { private ChatService chatService; @GetMapping("/get") - public ResponseEntity> getChats(@RequestParam Long chatId) { - List chats = chatService.getChats(chatId); + public ResponseEntity> getChats(@RequestParam Long userId, @RequestParam Long chatId) { + List chats = chatService.getChats(userId, chatId); if (chats == null) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Collections.emptyList()); } - return ResponseEntity.ok(chatService.getChats(chatId)); + return ResponseEntity.ok(chatService.getChats(userId, chatId)); } } \ No newline at end of file diff --git a/src/main/java/com/kuit/chatdiary/repository/ChatRepository.java b/src/main/java/com/kuit/chatdiary/repository/ChatRepository.java index 4e0bc56b..1b534bee 100644 --- a/src/main/java/com/kuit/chatdiary/repository/ChatRepository.java +++ b/src/main/java/com/kuit/chatdiary/repository/ChatRepository.java @@ -2,12 +2,15 @@ import com.kuit.chatdiary.domain.Chat; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; public interface ChatRepository extends JpaRepository { List findTop10ByMember_UserIdOrderByChatIdDesc(Long userId); - List findTop10ByChatIdGreaterThan(Long lastChatId); + @Query("SELECT c FROM chat c WHERE c.member.userId = :userId AND c.chatId > :lastChatId ORDER BY c.chatId DESC") + List findTop10ByUserIdAndChatIdGreaterThanOrderByChatIdDesc(@Param("userId") Long userId, @Param("lastChatId") Long lastChatId); List findTopByMember_UserIdOrderByChatIdDesc(Long userId); } diff --git a/src/main/java/com/kuit/chatdiary/service/ChatService.java b/src/main/java/com/kuit/chatdiary/service/ChatService.java index 0e49ae28..5f558f51 100644 --- a/src/main/java/com/kuit/chatdiary/service/ChatService.java +++ b/src/main/java/com/kuit/chatdiary/service/ChatService.java @@ -70,8 +70,8 @@ public String extractGptResponse(String jsonResponse) throws JsonProcessingExcep return ""; } - public List getChats(Long chatId) { - List chats = chatRepository.findTop10ByChatIdGreaterThan(chatId); + public List getChats(Long userId, Long lastChatId) { + List chats = chatRepository.findTop10ByUserIdAndChatIdGreaterThanOrderByChatIdDesc(userId, lastChatId); if (chats.isEmpty()) { return null; }