@Test public void likeCommentThatDoesntExistThrowException() { Long commentId = 1L; when(ecoNewsCommentRepo.findById(commentId)).thenReturn(java.util.Optional.ofNullable(null)); NotFoundException notFoundException = assertThrows(NotFoundException.class, () -> ecoNewsCommentService.like(commentId, getUser())); assertEquals(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION, notFoundException.getMessage()); }
public void like(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } ecoNewsCommentRepo.save(comment); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { public void like(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } ecoNewsCommentRepo.save(comment); } }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { public void like(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } ecoNewsCommentRepo.save(comment); }  }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { public void like(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } ecoNewsCommentRepo.save(comment); }  @Override AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest,
                                             User user); @Override PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user); @Override void deleteById(Long id, User user); @Override @Transactional void update(String text, Long id, User user); void like(Long id, User user); @Override @Transactional int countLikes(Long id); @Override int countReplies(Long id); @Override int countOfComments(Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> getAllActiveComments(Pageable pageable, User user, Long ecoNewsId); }
EcoNewsCommentServiceImpl implements EcoNewsCommentService { public void like(Long id, User user) { EcoNewsComment comment = ecoNewsCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (comment.getUsersLiked().contains(user)) { comment.getUsersLiked().remove(user); } else { comment.getUsersLiked().add(user); } ecoNewsCommentRepo.save(comment); }  @Override AddEcoNewsCommentDtoResponse save(Long econewsId, AddEcoNewsCommentDtoRequest addEcoNewsCommentDtoRequest,
                                             User user); @Override PageableDto<EcoNewsCommentDto> findAllComments(Pageable pageable, User user, Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> findAllReplies(Pageable pageable, Long parentCommentId, User user); @Override void deleteById(Long id, User user); @Override @Transactional void update(String text, Long id, User user); void like(Long id, User user); @Override @Transactional int countLikes(Long id); @Override int countReplies(Long id); @Override int countOfComments(Long ecoNewsId); @Override PageableDto<EcoNewsCommentDto> getAllActiveComments(Pageable pageable, User user, Long ecoNewsId);  }