@Service public class PostsServiceImpl implements PostsService { @Autowired private PostsRepository postsRepository; @Override public PostResponse getAllPosts(Integer pageNo , Integer pageSize, String sortBy , String sortDir){ Sort sort = sortDir.equalsIgnoreCase(Sort.Direction.ASC.name())? Sort.by(sortBy).ascending():Sort.by(sortBy).descending(); Pageable pg = PageRequest.of(pageNo , pageSize , sort); Page<Posts> posts = postsRepository.findAll(pg); List<Posts> postsList = posts.getContent(); List<PostDto> data = postsList.stream().map(this::mapToDTO).collect(Collectors.toList()); PostResponse postResponse = new PostResponse(); postResponse.setData(data); postResponse.setPageNo(posts.getNumber() + 1); postResponse.setPageSize(posts.getSize()); postResponse.setTotalElement(posts.getTotalElements()); postResponse.setLast(posts.isLast()); postResponse.setTotalPages(posts.getTotalPages()); return postResponse; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter