<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);
}