@Test void deleteByIdTest() { TipsAndTricksComment tipsAndTricksComment = ModelUtils.getTipsAndTricksComment(); TipsAndTricksComment reply = TipsAndTricksComment.builder().id(2L).text("some text").build(); tipsAndTricksComment.setComments(Collections.singletonList(reply)); when(tipsAndTricksCommentRepo.findById(anyLong())).thenReturn(Optional.of(tipsAndTricksComment)); when(tipsAndTricksCommentRepo.save(tipsAndTricksComment)).thenReturn(tipsAndTricksComment); tipsAndTricksCommentService.deleteById(1L, ModelUtils.getUser()); assertTrue(reply.isDeleted()); assertTrue(tipsAndTricksComment.isDeleted()); }
@Override public void deleteById(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } if (comment.getComments() != null) { comment.getComments().forEach(c -> c.setDeleted(true)); } comment.setDeleted(true); tipsAndTricksCommentRepo.save(comment); }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public void deleteById(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } if (comment.getComments() != null) { comment.getComments().forEach(c -> c.setDeleted(true)); } comment.setDeleted(true); tipsAndTricksCommentRepo.save(comment); } }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public void deleteById(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } if (comment.getComments() != null) { comment.getComments().forEach(c -> c.setDeleted(true)); } comment.setDeleted(true); tipsAndTricksCommentRepo.save(comment); }  }
TipsAndTricksCommentServiceImpl implements TipsAndTricksCommentService { @Override public void deleteById(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } if (comment.getComments() != null) { comment.getComments().forEach(c -> c.setDeleted(true)); } comment.setDeleted(true); 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 deleteById(Long id, User user) { TipsAndTricksComment comment = tipsAndTricksCommentRepo.findById(id) .orElseThrow(() -> new NotFoundException(ErrorMessage.COMMENT_NOT_FOUND_EXCEPTION)); if (user.getRole() != ROLE.ROLE_ADMIN && user.getRole() != ROLE.ROLE_MODERATOR && !user.getId().equals(comment.getUser().getId())) { throw new BadRequestException(ErrorMessage.USER_HAS_NO_PERMISSION); } if (comment.getComments() != null) { comment.getComments().forEach(c -> c.setDeleted(true)); } comment.setDeleted(true); 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);  }