Refactor pageVosBy in BaseCommentService

pull/146/head
johnniang 2019-04-28 22:51:58 +08:00
parent 51c9bbea89
commit 0dbe543cff
3 changed files with 34 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.cache.lock.CacheLock;
import run.halo.app.exception.BadRequestException;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.service.OptionService;
@ -34,6 +35,7 @@ public class RecoveryController {
@PostMapping("migrations/v0_4_3")
@ApiOperation("Migrates from halo v0.4.3")
@CacheLock
public void migrateFromVersion_0_4_3(
@ApiParam("This file content type should be json")
@RequestPart("file") MultipartFile file) {

View File

@ -29,6 +29,7 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
* %s: parent commentator author name
* %s: comment content
*/
@Deprecated
String COMMENT_TEMPLATE = "<a href='#comment-id-%d'>@%s</a> %s";
/**
@ -157,7 +158,7 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
/**
* Target must exist.
*
* @param targetId target id must not be null
* @param targetId target id must not be null (post id, sheet id or journal id)
*/
void targetMustExist(@NonNull Integer targetId);
}

View File

@ -105,17 +105,10 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
// List all the top comments (Caution: This list will be cleared)
List<COMMENT> comments = baseCommentRepository.findAllByPostIdAndStatus(postId, CommentStatus.PUBLISHED);
// Init the top virtual comment
BaseCommentVO topVirtualComment = new BaseCommentVO();
topVirtualComment.setId(0L);
topVirtualComment.setChildren(new LinkedList<>());
Comparator<BaseCommentVO> commentVOComparator = buildCommentComparator(pageable.getSortOr(Sort.by(Sort.Direction.DESC, "createTime")));
// Concrete the comment tree
concreteTree(topVirtualComment, new LinkedList<>(comments), commentVOComparator);
List<BaseCommentVO> topComments = topVirtualComment.getChildren();
// Convert to vo
List<BaseCommentVO> topComments = convertToVo(comments, commentVOComparator);
List<BaseCommentVO> pageContent;
@ -370,6 +363,30 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
};
}
/**
* Converts to base comment vo tree.
*
* @param comments comments list could be null
* @param comparator comment comparator could be null
* @return a comment vo tree
*/
@NonNull
protected List<BaseCommentVO> convertToVo(@Nullable List<COMMENT> comments, @Nullable Comparator<BaseCommentVO> comparator) {
if (CollectionUtils.isEmpty(comments)) {
return Collections.emptyList();
}
// Init the top virtual comment
BaseCommentVO topVirtualComment = new BaseCommentVO();
topVirtualComment.setId(0L);
topVirtualComment.setChildren(new LinkedList<>());
// Concrete the comment tree
concreteTree(topVirtualComment, new LinkedList<>(comments), comparator);
return topVirtualComment.getChildren();
}
/**
* Concretes comment tree.
*
@ -379,9 +396,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
*/
protected void concreteTree(@NonNull BaseCommentVO parentComment,
@Nullable Collection<COMMENT> comments,
@NonNull Comparator<BaseCommentVO> commentComparator) {
@Nullable Comparator<BaseCommentVO> commentComparator) {
Assert.notNull(parentComment, "Parent comment must not be null");
Assert.notNull(commentComparator, "Comment comparator must not be null");
if (CollectionUtils.isEmpty(comments)) {
return;
@ -419,8 +435,10 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
// Recursively concrete the children
parentComment.getChildren().forEach(childComment -> concreteTree(childComment, comments, commentComparator));
// Sort the children
if (commentComparator != null) {
parentComment.getChildren().sort(commentComparator);
}
}
}
}