@Test void likeTest() { TipsAndTricksComment tipsAndTricksComment = ModelUtils.getTipsAndTricksComment(); User user = ModelUtils.getUser(); when(tipsAndTricksCommentRepo.findById(anyLong())).thenReturn(Optional.of(tipsAndTricksComment)); when(tipsAndTricksCommentRepo.save(tipsAndTricksComment)).thenReturn(tipsAndTricksComment); tipsAndTricksComment.setUsersLiked(new HashSet<>()); tipsAndTricksCommentService.like(1L, user); assertTrue(tipsAndTricksComment.getUsersLiked().contains(user)); }
@Override public void like(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } tipsAndTricksCommentRepo.save(comment); }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public void like(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } tipsAndTricksCommentRepo.save(comment); } }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public void like(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } tipsAndTricksCommentRepo.save(comment); }  }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public void like(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } tipsAndTricksCommentRepo.save(comment); }  @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 void like(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } tipsAndTricksCommentRepo.save(comment); }  @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);  }