Paging

텅텅텅·2022년 6월 12일
0

Spring TIL

목록 보기
5/13
<Controller>
@GetMapping("/api/products")
public Page<Product> getProducts(
  @RequestParam("page") int page,
  @RequestParam("size") int size,
  @RequestParam("sortBy") String sortBy,
  @RequestParam("isAsc") boolean isAsc,
  @AuthentictionPrincipal UserDetailsIml userDetails){
Long userId = userDetails.getUser().getId();
page = page - 1;
return productService.getProducts(userId, page, size, sortBy, isAsc);
}
<Service>
public Page<Product> getProducts(Long userId, int page, int size, String sortBy, boolean isAsc){
  	Sort.Direction direction = isAsc ? Sort.Direction.ASC : Sort.Direction.DESC;
  	Sort sort = Sort.by(direction, sortBy);
  	Pageable pageable = PageRequest.of(page, size, sort);
	return productRepository.findAllByUserId(userId);
}
<Repository>
public interface ProductRepository extends JpaRepository<Product, Long>{
	Page<Product> findAllByUserId(Long userId, Pageable pageable);
}
profile
아무것도모르오

0개의 댓글