Skip to content

Commit

Permalink
refactor: 무한 스크롤 위해 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
fnzl54 committed Feb 25, 2024
1 parent 749cfb4 commit 1faafc1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,9 +49,11 @@ public ResponseEntity<ChatCreateRes> createRoom(
*/
@GetMapping("/room")
public ResponseEntity<List<ChatRoomRes>> getAllChatRoom(
@AuthenticationPrincipal UserDetailsImpl userDetailsImpl
@AuthenticationPrincipal UserDetailsImpl userDetailsImpl,
@RequestParam(defaultValue = "1") int pageNo,
@RequestParam(defaultValue = "10") int pageSize
) {
List<ChatRoomRes> chatRooms = chatRoomService.findChatRoomByUserId(userDetailsImpl.getUsername());
List<ChatRoomRes> chatRooms = chatRoomService.findChatRoomByUserId(userDetailsImpl.getUsername(), pageNo, pageSize);

return ResponseEntity.ok(chatRooms);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,5 +19,5 @@ public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
+ "c.sellerEmail, "
+ "c.buyerEmail "
+ "FROM ChatRoom c WHERE :userEmail LIKE c.buyerEmail OR :userEmail LIKE c.sellerEmail")
List<Object[]> findByUserIn(@Param("userEmail") String userEmail);
Page<Object[]> findByUserIn(@Param("userEmail") String userEmail, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,8 +53,9 @@ public ChatCreateRes createChatRoom(Long productId, String buyer) {
}

@Transactional(readOnly = true)
public List<ChatRoomRes> findChatRoomByUserId(String userEmail) {
List<Object[]> results = chatRoomRepository.findByUserIn(userEmail);
public List<ChatRoomRes> findChatRoomByUserId(String userEmail, int pageNo, int pageSize) {
Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.by("id").descending()); // 1페이지부터 시작하도록
Page<Object[]> results = chatRoomRepository.findByUserIn(userEmail, pageable);

List<ChatRoomRes> chatRoomResponse = new ArrayList<>();
for (Object[] result : results) {
Expand Down

0 comments on commit 1faafc1

Please sign in to comment.