From 6835d5b8d3c1eab44e60d8d3d8a90a075ea3f819 Mon Sep 17 00:00:00 2001 From: Lee Haewon Date: Sat, 7 Dec 2024 17:52:44 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/chat/application/ChatService.java | 28 +++++++------------ .../repository/ChattingRoomRepository.java | 10 ++++++- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/on/server/domain/chat/application/ChatService.java b/src/main/java/com/on/server/domain/chat/application/ChatService.java index 5258c8f..7bcd191 100644 --- a/src/main/java/com/on/server/domain/chat/application/ChatService.java +++ b/src/main/java/com/on/server/domain/chat/application/ChatService.java @@ -57,19 +57,13 @@ public class ChatService { public Page getCompanyChatRoomList(User user, Pageable pageable) { // '동행 구하기' 채팅방 목록 - Page chattingRoomList = chattingRoomRepository.findChattingRoomByChatUserOneOrChatUserTwo(user, user, pageable); - - Integer roomCount = (int) chattingRoomList.stream() - .filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.COMPANY) - .count(); + Page chattingRoomList = chattingRoomRepository.findByChatUserOneOrChatUserTwoAndChattingRoomType(user, ChatType.COMPANY, pageable); List roomListDto = chattingRoomList.stream() - .filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.COMPANY) // ChatType.COMPANY로 필터링 + .filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.COMPANY) .map(chattingRoom -> { Chat chat = chatRepository.findFirstByChattingRoomOrderByCreatedAtDesc(chattingRoom); - - SpecialChat specialChat = specialChatRepository.findByChattingRoom(chattingRoom); CompanyPost companyPost = specialChat.getCompanyPost(); @@ -92,21 +86,19 @@ public Page getCompanyChatRoomList(User user, Pageable p .sorted(Comparator.comparing(CompanyRoomDto::getLastChatTime).reversed()) // 최신순 정렬 .toList(); + CompanyChatRoomListDto companyChatRoomListDto = CompanyChatRoomListDto.builder() - .roomCount(roomCount) + .roomCount(roomListDto.size()) .roomList(roomListDto) .build(); - return new PageImpl<>(List.of(companyChatRoomListDto), pageable, roomListDto.size()); + return new PageImpl<>(List.of(companyChatRoomListDto), pageable, chattingRoomList.getTotalElements()); } + public Page getMarketChatRoomList(User user, Pageable pageable) { // '중고 거래' 채팅방 목록 - Page chattingRoomList = chattingRoomRepository.findChattingRoomByChatUserOneOrChatUserTwo(user, user, pageable); - - Integer roomCount = (int) chattingRoomList.stream() - .filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.MARKET) - .count(); + Page chattingRoomList = chattingRoomRepository.findByChatUserOneOrChatUserTwoAndChattingRoomType(user, ChatType.MARKET, pageable); List roomListDto = chattingRoomList.stream() .filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.MARKET) @@ -140,13 +132,13 @@ public Page getMarketChatRoomList(User user, Pageable pag .sorted(Comparator.comparing(MarketRoomDto::getLastChatTime).reversed()) // 최신순 정렬 .toList(); + MarketChatRoomListDto marketChatRoomListDto = MarketChatRoomListDto.builder() - .roomCount(roomCount) + .roomCount(roomListDto.size()) .roomList(roomListDto) .build(); - return new PageImpl<>(List.of(marketChatRoomListDto), pageable, roomListDto.size()); - + return new PageImpl<>(List.of(marketChatRoomListDto), pageable, chattingRoomList.getTotalElements()); } /* chatUserOne, chatUserTwo, specialChat 비교하여 기존에 존재하는 채팅방인지 찾는 메소드 */ diff --git a/src/main/java/com/on/server/domain/chat/domain/repository/ChattingRoomRepository.java b/src/main/java/com/on/server/domain/chat/domain/repository/ChattingRoomRepository.java index 48dc416..334c49d 100644 --- a/src/main/java/com/on/server/domain/chat/domain/repository/ChattingRoomRepository.java +++ b/src/main/java/com/on/server/domain/chat/domain/repository/ChattingRoomRepository.java @@ -6,12 +6,20 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; 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 ChattingRoomRepository extends JpaRepository { - Page findChattingRoomByChatUserOneOrChatUserTwo(User user, User user1, Pageable pageable); + @Query("SELECT cr FROM ChattingRoom cr WHERE (cr.chatUserOne = :user OR cr.chatUserTwo = :user) AND cr.chattingRoomType = :chattingRoomType") + Page findByChatUserOneOrChatUserTwoAndChattingRoomType( + @Param("user") User user, + @Param("chattingRoomType") ChatType chattingRoomType, + Pageable pageable + ); List findChattingRoomByChatUserOneAndChatUserTwoAndChattingRoomType(User user, User chatUserTwo, ChatType chatType);