diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java b/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java index 7c28766..df162d8 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/controller/ChatRoomController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -48,9 +49,11 @@ public ResponseEntity createRoom( */ @GetMapping("/room") public ResponseEntity> getAllChatRoom( - @AuthenticationPrincipal UserDetailsImpl userDetailsImpl + @AuthenticationPrincipal UserDetailsImpl userDetailsImpl, + @RequestParam(defaultValue = "1") int pageNo, + @RequestParam(defaultValue = "10") int pageSize ) { - List chatRooms = chatRoomService.findChatRoomByUserId(userDetailsImpl.getUsername()); + List chatRooms = chatRoomService.findChatRoomByUserId(userDetailsImpl.getUsername(), pageNo, pageSize); return ResponseEntity.ok(chatRooms); } diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/repository/ChatRoomRepository.java b/src/main/java/com/teamjo/techeermarket/domain/chats/repository/ChatRoomRepository.java index ca2a682..9a12d17 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/repository/ChatRoomRepository.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/repository/ChatRoomRepository.java @@ -2,6 +2,8 @@ import com.teamjo.techeermarket.domain.chats.entity.ChatRoom; import java.util.List; +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; @@ -17,5 +19,5 @@ public interface ChatRoomRepository extends JpaRepository { + "c.sellerEmail, " + "c.buyerEmail " + "FROM ChatRoom c WHERE :userEmail LIKE c.buyerEmail OR :userEmail LIKE c.sellerEmail") - List findByUserIn(@Param("userEmail") String userEmail); + Page findByUserIn(@Param("userEmail") String userEmail, Pageable pageable); } diff --git a/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java b/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java index ce97fa9..51dfa81 100644 --- a/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java +++ b/src/main/java/com/teamjo/techeermarket/domain/chats/service/ChatRoomService.java @@ -17,6 +17,10 @@ import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -49,8 +53,9 @@ public ChatCreateRes createChatRoom(Long productId, String buyer) { } @Transactional(readOnly = true) - public List findChatRoomByUserId(String userEmail) { - List results = chatRoomRepository.findByUserIn(userEmail); + public List findChatRoomByUserId(String userEmail, int pageNo, int pageSize) { + Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by("id").descending()); // 1페이지부터 시작하도록 + Page results = chatRoomRepository.findByUserIn(userEmail, pageable); List chatRoomResponse = new ArrayList<>(); for (Object[] result : results) {