mirror of https://github.com/halo-dev/halo
Refactor pageVosBy in BaseCommentService
parent
51c9bbea89
commit
0dbe543cff
|
@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestPart;
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import run.halo.app.cache.lock.CacheLock;
|
||||||
import run.halo.app.exception.BadRequestException;
|
import run.halo.app.exception.BadRequestException;
|
||||||
import run.halo.app.model.properties.PrimaryProperties;
|
import run.halo.app.model.properties.PrimaryProperties;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
|
@ -34,6 +35,7 @@ public class RecoveryController {
|
||||||
|
|
||||||
@PostMapping("migrations/v0_4_3")
|
@PostMapping("migrations/v0_4_3")
|
||||||
@ApiOperation("Migrates from halo v0.4.3")
|
@ApiOperation("Migrates from halo v0.4.3")
|
||||||
|
@CacheLock
|
||||||
public void migrateFromVersion_0_4_3(
|
public void migrateFromVersion_0_4_3(
|
||||||
@ApiParam("This file content type should be json")
|
@ApiParam("This file content type should be json")
|
||||||
@RequestPart("file") MultipartFile file) {
|
@RequestPart("file") MultipartFile file) {
|
||||||
|
|
|
@ -29,6 +29,7 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
|
||||||
* %s: parent commentator author name
|
* %s: parent commentator author name
|
||||||
* %s: comment content
|
* %s: comment content
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
String COMMENT_TEMPLATE = "<a href='#comment-id-%d'>@%s</a> %s";
|
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.
|
* 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);
|
void targetMustExist(@NonNull Integer targetId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,17 +105,10 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
// List all the top comments (Caution: This list will be cleared)
|
// List all the top comments (Caution: This list will be cleared)
|
||||||
List<COMMENT> comments = baseCommentRepository.findAllByPostIdAndStatus(postId, CommentStatus.PUBLISHED);
|
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")));
|
Comparator<BaseCommentVO> commentVOComparator = buildCommentComparator(pageable.getSortOr(Sort.by(Sort.Direction.DESC, "createTime")));
|
||||||
|
|
||||||
// Concrete the comment tree
|
// Convert to vo
|
||||||
concreteTree(topVirtualComment, new LinkedList<>(comments), commentVOComparator);
|
List<BaseCommentVO> topComments = convertToVo(comments, commentVOComparator);
|
||||||
|
|
||||||
List<BaseCommentVO> topComments = topVirtualComment.getChildren();
|
|
||||||
|
|
||||||
List<BaseCommentVO> pageContent;
|
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.
|
* Concretes comment tree.
|
||||||
*
|
*
|
||||||
|
@ -379,9 +396,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
*/
|
*/
|
||||||
protected void concreteTree(@NonNull BaseCommentVO parentComment,
|
protected void concreteTree(@NonNull BaseCommentVO parentComment,
|
||||||
@Nullable Collection<COMMENT> comments,
|
@Nullable Collection<COMMENT> comments,
|
||||||
@NonNull Comparator<BaseCommentVO> commentComparator) {
|
@Nullable Comparator<BaseCommentVO> commentComparator) {
|
||||||
Assert.notNull(parentComment, "Parent comment must not be null");
|
Assert.notNull(parentComment, "Parent comment must not be null");
|
||||||
Assert.notNull(commentComparator, "Comment comparator must not be null");
|
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(comments)) {
|
if (CollectionUtils.isEmpty(comments)) {
|
||||||
return;
|
return;
|
||||||
|
@ -419,7 +435,9 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
// Recursively concrete the children
|
// Recursively concrete the children
|
||||||
parentComment.getChildren().forEach(childComment -> concreteTree(childComment, comments, commentComparator));
|
parentComment.getChildren().forEach(childComment -> concreteTree(childComment, comments, commentComparator));
|
||||||
// Sort the children
|
// Sort the children
|
||||||
parentComment.getChildren().sort(commentComparator);
|
if (commentComparator != null) {
|
||||||
|
parentComment.getChildren().sort(commentComparator);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue