mirror of https://github.com/halo-dev/halo
Complete comment children list service
parent
86444ff5da
commit
2e77999d20
|
@ -13,6 +13,7 @@ import run.halo.app.model.dto.BaseCommentDTO;
|
|||
import run.halo.app.model.dto.post.BasePostDetailDTO;
|
||||
import run.halo.app.model.dto.post.BasePostSimpleDTO;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.entity.PostComment;
|
||||
import run.halo.app.model.enums.CommentStatus;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
import run.halo.app.model.params.PostCommentParam;
|
||||
|
@ -88,9 +89,12 @@ public class PostController {
|
|||
|
||||
@GetMapping("{postId:\\d+}/comments/{commentParentId:\\d+}/children")
|
||||
public List<BaseCommentDTO> listChildrenBy(@PathVariable("postId") Integer postId,
|
||||
@PathVariable("commentParentId") Integer commentParentId,
|
||||
@PathVariable("commentParentId") Long commentParentId,
|
||||
@SortDefault(sort = "createTime", direction = DESC) Sort sort) {
|
||||
return postCommentService.listChildrenBy(postId, commentParentId, CommentStatus.PUBLISHED, sort);
|
||||
// Find all children comments
|
||||
List<PostComment> postComments = postCommentService.listChildrenBy(postId, commentParentId, CommentStatus.PUBLISHED, sort);
|
||||
// Convert to base comment dto
|
||||
return postCommentService.convertTo(postComments);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -94,6 +94,27 @@ public interface BaseCommentRepository<COMMENT extends BaseComment> extends Base
|
|||
@NonNull
|
||||
Page<COMMENT> findAllByPostIdAndStatus(Integer postId, CommentStatus status, Pageable pageable);
|
||||
|
||||
/**
|
||||
* Finds comments by post id, comment status and parent id.
|
||||
*
|
||||
* @param postId post id must not be null
|
||||
* @param status comment status must not be null
|
||||
* @param parentId comment parent id must not be null
|
||||
* @return a list of comment
|
||||
*/
|
||||
@NonNull
|
||||
List<COMMENT> findAllByPostIdAndStatusAndParentId(Integer postId, CommentStatus status, Long parentId);
|
||||
|
||||
/**
|
||||
* Finds all comments by status and parent id collection.
|
||||
*
|
||||
* @param status comment status must not be null
|
||||
* @param parentIds parent id collection must not be null
|
||||
* @return a list of comment
|
||||
*/
|
||||
@NonNull
|
||||
List<COMMENT> findAllByStatusAndParentIdIn(@NonNull CommentStatus status, @NonNull Iterable<Long> parentIds);
|
||||
|
||||
/**
|
||||
* Finds comments by post id, comment status and parent id.
|
||||
*
|
||||
|
@ -106,6 +127,7 @@ public interface BaseCommentRepository<COMMENT extends BaseComment> extends Base
|
|||
@NonNull
|
||||
Page<COMMENT> findAllByPostIdAndStatusAndParentId(Integer postId, CommentStatus status, Long parentId, Pageable pageable);
|
||||
|
||||
|
||||
@Query("select new run.halo.app.model.projection.CommentChildrenCountProjection(count(comment.id), comment.parentId) " +
|
||||
"from BaseComment comment " +
|
||||
"where comment.parentId in ?1 " +
|
||||
|
|
|
@ -213,5 +213,5 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
|
|||
* @return a list of children comment
|
||||
*/
|
||||
@NonNull
|
||||
List<BaseCommentDTO> listChildrenBy(@NonNull Integer targetId, @NonNull Integer commentParentId, @NonNull CommentStatus status, @NonNull Sort sort);
|
||||
List<COMMENT> listChildrenBy(@NonNull Integer targetId, @NonNull Long commentParentId, @NonNull CommentStatus status, @NonNull Sort sort);
|
||||
}
|
||||
|
|
|
@ -445,12 +445,55 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<BaseCommentDTO> listChildrenBy(Integer targetId, Integer commentParentId, CommentStatus status, Sort sort) {
|
||||
public List<COMMENT> listChildrenBy(Integer targetId, Long commentParentId, CommentStatus status, Sort sort) {
|
||||
Assert.notNull(targetId, "Target id must not be null");
|
||||
Assert.notNull(commentParentId, "Comment parent id must not be null");
|
||||
Assert.notNull(sort, "Sort info must not be null");
|
||||
|
||||
return null;
|
||||
// Get comments recursively
|
||||
|
||||
// Get direct children
|
||||
List<COMMENT> directChildren = baseCommentRepository.findAllByPostIdAndStatusAndParentId(targetId, status, commentParentId);
|
||||
|
||||
// Create result container
|
||||
Set<COMMENT> children = new HashSet<>();
|
||||
|
||||
// Get children comments
|
||||
getChildrenRecursively(directChildren, status, children);
|
||||
|
||||
// Sort children
|
||||
List<COMMENT> childrenList = new ArrayList<>(children);
|
||||
childrenList.sort(Comparator.comparing(BaseComment::getId));
|
||||
|
||||
return childrenList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get children comments recursively.
|
||||
*
|
||||
* @param topComments top comment list
|
||||
* @param status comment status must not be null
|
||||
* @param children children result must not be null
|
||||
*/
|
||||
private void getChildrenRecursively(@Nullable List<COMMENT> topComments, @NonNull CommentStatus status, @NonNull Set<COMMENT> children) {
|
||||
Assert.notNull(status, "Comment status must not be null");
|
||||
Assert.notNull(children, "Children comment set must not be null");
|
||||
|
||||
if (CollectionUtils.isEmpty(topComments)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert comment id set
|
||||
Set<Long> commentIds = ServiceUtils.fetchProperty(topComments, COMMENT::getId);
|
||||
|
||||
// Get direct children
|
||||
List<COMMENT> directChildren = baseCommentRepository.findAllByStatusAndParentIdIn(status, commentIds);
|
||||
|
||||
// Recursively invoke
|
||||
getChildrenRecursively(directChildren, status, children);
|
||||
|
||||
// Add direct children to children result
|
||||
children.addAll(topComments);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,6 +31,7 @@ public class ServiceUtils {
|
|||
* @param <T> data type
|
||||
* @return a set of id
|
||||
*/
|
||||
@NonNull
|
||||
public static <ID, T> Set<ID> fetchProperty(final Collection<T> datas, Function<T, ID> mappingFunction) {
|
||||
return CollectionUtils.isEmpty(datas) ?
|
||||
Collections.emptySet() :
|
||||
|
@ -47,6 +48,7 @@ public class ServiceUtils {
|
|||
* @param <D> data type
|
||||
* @return a map which key is in ids and value containing in list
|
||||
*/
|
||||
@NonNull
|
||||
public static <ID, D> Map<ID, List<D>> convertToListMap(Collection<ID> ids, Collection<D> list, Function<D, ID> mappingFunction) {
|
||||
Assert.notNull(mappingFunction, "mapping function must not be null");
|
||||
|
||||
|
@ -72,6 +74,7 @@ public class ServiceUtils {
|
|||
* @param <D> data type
|
||||
* @return a map which key from list data and value is data
|
||||
*/
|
||||
@NonNull
|
||||
public static <ID, D> Map<ID, D> convertToMap(Collection<D> list, Function<D, ID> mappingFunction) {
|
||||
Assert.notNull(mappingFunction, "mapping function must not be null");
|
||||
|
||||
|
@ -97,6 +100,7 @@ public class ServiceUtils {
|
|||
* @param <V> value type
|
||||
* @return a map which key from list data and value is data
|
||||
*/
|
||||
@NonNull
|
||||
public static <ID, D, V> Map<ID, V> convertToMap(Collection<D> list, Function<D, ID> keyFunction, Function<D, V> valueFunction) {
|
||||
Assert.notNull(keyFunction, "mapping function must not be null");
|
||||
|
||||
|
|
Loading…
Reference in New Issue