Skip to content

Commit

Permalink
fix: 채팅방 목록 조회 쿼리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Haewonny committed Dec 7, 2024
1 parent ee8b081 commit 6835d5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,13 @@ public class ChatService {

public Page<CompanyChatRoomListDto> getCompanyChatRoomList(User user, Pageable pageable) {
// '동행 구하기' 채팅방 목록
Page<ChattingRoom> chattingRoomList = chattingRoomRepository.findChattingRoomByChatUserOneOrChatUserTwo(user, user, pageable);

Integer roomCount = (int) chattingRoomList.stream()
.filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.COMPANY)
.count();
Page<ChattingRoom> chattingRoomList = chattingRoomRepository.findByChatUserOneOrChatUserTwoAndChattingRoomType(user, ChatType.COMPANY, pageable);

List<CompanyRoomDto> 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();

Expand All @@ -92,21 +86,19 @@ public Page<CompanyChatRoomListDto> 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<MarketChatRoomListDto> getMarketChatRoomList(User user, Pageable pageable) {
// '중고 거래' 채팅방 목록
Page<ChattingRoom> chattingRoomList = chattingRoomRepository.findChattingRoomByChatUserOneOrChatUserTwo(user, user, pageable);

Integer roomCount = (int) chattingRoomList.stream()
.filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.MARKET)
.count();
Page<ChattingRoom> chattingRoomList = chattingRoomRepository.findByChatUserOneOrChatUserTwoAndChattingRoomType(user, ChatType.MARKET, pageable);

List<MarketRoomDto> roomListDto = chattingRoomList.stream()
.filter(chattingRoom -> chattingRoom.getChattingRoomType() == ChatType.MARKET)
Expand Down Expand Up @@ -140,13 +132,13 @@ public Page<MarketChatRoomListDto> 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 비교하여 기존에 존재하는 채팅방인지 찾는 메소드 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChattingRoom, Long> {

Page<ChattingRoom> 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<ChattingRoom> findByChatUserOneOrChatUserTwoAndChattingRoomType(
@Param("user") User user,
@Param("chattingRoomType") ChatType chattingRoomType,
Pageable pageable
);

List<ChattingRoom> findChattingRoomByChatUserOneAndChatUserTwoAndChattingRoomType(User user, User chatUserTwo, ChatType chatType);

Expand Down

0 comments on commit 6835d5b

Please sign in to comment.