Skip to content

Commit

Permalink
Support querying sub-categories of any level (halo-dev#2995)
Browse files Browse the repository at this point in the history
What type of PR is this?

/kind feature
/kind api-change

What this PR does / why we need it:

添加一个方法可以根据分类树的任意一层级的名称查询子树

Which issue(s) this PR fixes:

Fixes halo-dev#2960

Special notes for your reviewer:

None

Does this PR introduce a user-facing change?

```release-note
CategoryFinder 添加根据分类查询子分类树结构的方法
```
  • Loading branch information
chengfeiyue authored Dec 22, 2022
1 parent 77da761 commit a7da2c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/run/halo/app/theme/finders/CategoryFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface CategoryFinder {
Flux<CategoryVo> listAll();

Flux<CategoryTreeVo> listAsTree();

Flux<CategoryTreeVo> listAsTree(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.Category;
Expand Down Expand Up @@ -72,6 +73,15 @@ public Flux<CategoryVo> listAll() {

@Override
public Flux<CategoryTreeVo> listAsTree() {
return this.toCategoryTreeVoFlux(null);
}

@Override
public Flux<CategoryTreeVo> listAsTree(String name) {
return this.toCategoryTreeVoFlux(name);
}

Flux<CategoryTreeVo> toCategoryTreeVoFlux(String name) {
return listAll()
.collectList()
.flatMapIterable(categoryVos -> {
Expand All @@ -80,23 +90,23 @@ public Flux<CategoryTreeVo> listAsTree() {
.collect(Collectors.toMap(categoryVo -> categoryVo.getMetadata().getName(),
Function.identity()));

nameIdentityMap.forEach((name, value) -> {
nameIdentityMap.forEach((nameKey, value) -> {
List<String> children = value.getSpec().getChildren();
if (children == null) {
return;
}
for (String child : children) {
CategoryTreeVo childNode = nameIdentityMap.get(child);
if (childNode != null) {
childNode.setParentName(name);
childNode.setParentName(nameKey);
}
}
});
return listToTree(nameIdentityMap.values());
return listToTree(nameIdentityMap.values(), name);
});
}

static List<CategoryTreeVo> listToTree(Collection<CategoryTreeVo> list) {
static List<CategoryTreeVo> listToTree(Collection<CategoryTreeVo> list, String name) {
Map<String, List<CategoryTreeVo>> parentNameIdentityMap = list.stream()
.filter(categoryTreeVo -> categoryTreeVo.getParentName() != null)
.collect(Collectors.groupingBy(CategoryTreeVo::getParentName));
Expand All @@ -111,7 +121,8 @@ static List<CategoryTreeVo> listToTree(Collection<CategoryTreeVo> list) {
node.setChildren(children);
});
return list.stream()
.filter(v -> v.getParentName() == null)
.filter(v -> StringUtils.isEmpty(name) ? v.getParentName() == null
: StringUtils.equals(v.getMetadata().getName(), name))
.sorted(defaultTreeNodeComparator())
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ void listAsTree() {
assertThat(treeVos).hasSize(1);
}

@Test
void listSubTreeByName() {
when(client.list(eq(Category.class), eq(null), any()))
.thenReturn(Flux.fromIterable(categoriesForTree()));
List<CategoryTreeVo> treeVos = categoryFinder.listAsTree("E").collectList().block();
assertThat(treeVos.get(0).getMetadata().getName()).isEqualTo("E");
assertThat(treeVos.get(0).getChildren()).hasSize(2);
assertThat(treeVos.get(0).getChildren().get(0).getMetadata().getName()).isEqualTo("A");
assertThat(treeVos.get(0).getChildren().get(1).getMetadata().getName()).isEqualTo("C");
}

/**
* Test for {@link CategoryFinderImpl#listAsTree()}.
*
Expand Down

0 comments on commit a7da2c0

Please sign in to comment.