Enhance filter ip address service

pull/172/head
johnniang 2019-05-30 03:07:03 +08:00
parent 427a0b86e7
commit c054644035
2 changed files with 32 additions and 7 deletions

View File

@ -220,19 +220,20 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
* *
* @param comment comment dto must not be null * @param comment comment dto must not be null
*/ */
void filterIpAddress(@NonNull BaseCommentDTO comment); <T extends BaseCommentDTO> T filterIpAddress(@NonNull T comment);
/** /**
* Filters comment ip address. * Filters comment ip address.
* *
* @param comments comment dto list * @param comments comment dto list
*/ */
void filterIpAddress(@Nullable List<BaseCommentDTO> comments); <T extends BaseCommentDTO> List<T> filterIpAddress(@Nullable List<T> comments);
/** /**
* Filters comment ip address. * Filters comment ip address.
* *
* @param commentPage comment page * @param commentPage comment page
*/ */
void filterIpAddress(@NonNull Page<BaseCommentDTO> commentPage); <T extends BaseCommentDTO> Page<T> filterIpAddress(@NonNull Page<T> commentPage);
} }

View File

@ -471,26 +471,50 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
} }
@Override @Override
public void filterIpAddress(BaseCommentDTO comment) { public <T extends BaseCommentDTO> T filterIpAddress(@NonNull T comment) {
Assert.notNull(comment, "Base comment dto must not be null"); Assert.notNull(comment, "Base comment dto must not be null");
// Clear ip address // Clear ip address
comment.setIpAddress(""); comment.setIpAddress("");
// Handle base comment vo
if (comment instanceof BaseCommentVO) {
BaseCommentVO baseCommentVO = (BaseCommentVO) comment;
Queue<BaseCommentVO> commentQueue = new LinkedList<>();
commentQueue.offer(baseCommentVO);
while (!commentQueue.isEmpty()) {
BaseCommentVO current = commentQueue.poll();
// Clear ip address
current.setIpAddress("");
if (!CollectionUtils.isEmpty(current.getChildren())) {
// Add children
commentQueue.addAll(current.getChildren());
}
}
}
return comment;
} }
@Override @Override
public void filterIpAddress(List<BaseCommentDTO> comments) { public <T extends BaseCommentDTO> List<T> filterIpAddress(List<T> comments) {
if (CollectionUtils.isEmpty(comments)) { if (CollectionUtils.isEmpty(comments)) {
return; return Collections.emptyList();
} }
comments.forEach(this::filterIpAddress); comments.forEach(this::filterIpAddress);
return comments;
} }
@Override @Override
public void filterIpAddress(Page<BaseCommentDTO> commentPage) { public <T extends BaseCommentDTO> Page<T> filterIpAddress(Page<T> commentPage) {
Assert.notNull(commentPage, "Comment page must not be null"); Assert.notNull(commentPage, "Comment page must not be null");
commentPage.forEach(this::filterIpAddress); commentPage.forEach(this::filterIpAddress);
return commentPage;
} }
/** /**