@Test void findAllCommentsTest() { TipsAndTricksComment tipsAndTricksComment = TipsAndTricksComment.builder() .id(1L) .text("text") .user(ModelUtils.getUser()) .usersLiked(new HashSet<>()) .build(); List<TipsAndTricksComment> tipsAndTricksComments = Collections.singletonList(tipsAndTricksComment); PageRequest pageRequest = PageRequest.of(0, 2); Page<TipsAndTricksComment> page = new PageImpl<>(tipsAndTricksComments, pageRequest, tipsAndTricksComments.size()); List<TipsAndTricksCommentDto> dtoList = Collections.singletonList(tipsAndTricksCommentDto); PageableDto<TipsAndTricksCommentDto> pageableDto = new PageableDto<>(dtoList, dtoList.size(), 0,1); when(tipsAndTricksCommentRepo.findAllByParentCommentIsNullAndTipsAndTricksIdOrderByCreatedDateDesc(pageRequest, tipsAndTricksComment.getId())).thenReturn(page); when(modelMapper.map(tipsAndTricksComments.get(0), TipsAndTricksCommentDto.class)) .thenReturn(dtoList.get(0)); assertEquals(pageableDto, tipsAndTricksCommentService .findAllComments(pageRequest, ModelUtils.getUser(), tipsAndTricksComment.getId())); }
@Override public PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId) { Page<TipsAndTricksComment> pages = tipsAndTricksCommentRepo.findAllByParentCommentIsNullAndTipsAndTricksIdOrderByCreatedDateDesc( pageable, tipsAndTricksId); List<TipsAndTricksCommentDto> tipsAndTricksCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(tipsAndTricksComment -> modelMapper.map(tipsAndTricksComment, TipsAndTricksCommentDto.class)) .map(comment -> { comment .setReplies(tipsAndTricksCommentRepo .countTipsAndTricksCommentByParentCommentIdAndDeletedFalse(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId) { Page<TipsAndTricksComment> pages = tipsAndTricksCommentRepo.findAllByParentCommentIsNullAndTipsAndTricksIdOrderByCreatedDateDesc( pageable, tipsAndTricksId); List<TipsAndTricksCommentDto> tipsAndTricksCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(tipsAndTricksComment -> modelMapper.map(tipsAndTricksComment, TipsAndTricksCommentDto.class)) .map(comment -> { comment .setReplies(tipsAndTricksCommentRepo .countTipsAndTricksCommentByParentCommentIdAndDeletedFalse(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); } }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId) { Page<TipsAndTricksComment> pages = tipsAndTricksCommentRepo.findAllByParentCommentIsNullAndTipsAndTricksIdOrderByCreatedDateDesc( pageable, tipsAndTricksId); List<TipsAndTricksCommentDto> tipsAndTricksCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(tipsAndTricksComment -> modelMapper.map(tipsAndTricksComment, TipsAndTricksCommentDto.class)) .map(comment -> { comment .setReplies(tipsAndTricksCommentRepo .countTipsAndTricksCommentByParentCommentIdAndDeletedFalse(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId) { Page<TipsAndTricksComment> pages = tipsAndTricksCommentRepo.findAllByParentCommentIsNullAndTipsAndTricksIdOrderByCreatedDateDesc( pageable, tipsAndTricksId); List<TipsAndTricksCommentDto> tipsAndTricksCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(tipsAndTricksComment -> modelMapper.map(tipsAndTricksComment, TipsAndTricksCommentDto.class)) .map(comment -> { comment .setReplies(tipsAndTricksCommentRepo .countTipsAndTricksCommentByParentCommentIdAndDeletedFalse(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  @Override AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId,
                                                   AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest,
                                                   User user); @Override PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId); @Override List<TipsAndTricksCommentDto> findAllReplies(Long parentCommentId); @Override void deleteById(Long id, User user); @Override void update(String text, Long id, User user); @Override void like(Long id, User user); @Override int countLikes(Long id); @Override int countReplies(Long id); @Override int countComments(Long tipAndTricksId); }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId) { Page<TipsAndTricksComment> pages = tipsAndTricksCommentRepo.findAllByParentCommentIsNullAndTipsAndTricksIdOrderByCreatedDateDesc( pageable, tipsAndTricksId); List<TipsAndTricksCommentDto> tipsAndTricksCommentDtos = pages .stream() .map(comment -> { comment.setCurrentUserLiked(comment.getUsersLiked().contains(user)); return comment; }) .map(tipsAndTricksComment -> modelMapper.map(tipsAndTricksComment, TipsAndTricksCommentDto.class)) .map(comment -> { comment .setReplies(tipsAndTricksCommentRepo .countTipsAndTricksCommentByParentCommentIdAndDeletedFalse(comment.getId())); return comment; }) .collect(Collectors.toList()); return new PageableDto<>( tipsAndTricksCommentDtos, pages.getTotalElements(), pages.getPageable().getPageNumber(), pages.getTotalPages() ); }  @Override AddTipsAndTricksCommentDtoResponse save(Long tipsandtricksId,
                                                   AddTipsAndTricksCommentDtoRequest addTipsAndTricksCommentDtoRequest,
                                                   User user); @Override PageableDto<TipsAndTricksCommentDto> findAllComments(Pageable pageable, User user, Long tipsAndTricksId); @Override List<TipsAndTricksCommentDto> findAllReplies(Long parentCommentId); @Override void deleteById(Long id, User user); @Override void update(String text, Long id, User user); @Override void like(Long id, User user); @Override int countLikes(Long id); @Override int countReplies(Long id); @Override int countComments(Long tipAndTricksId);  }