Skip to content

Commit

Permalink
feat: category include childrenCategories
Browse files Browse the repository at this point in the history
  • Loading branch information
AirboZH committed Apr 23, 2024
1 parent b74f7c4 commit f16d73f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static class CategorySpec {
private Integer priority;

private List<String> children;

private boolean independent;
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package run.halo.app.theme.finders.impl;

import static run.halo.app.extension.index.query.QueryFactory.all;
import static run.halo.app.extension.index.query.QueryFactory.and;
import static run.halo.app.extension.index.query.QueryFactory.equal;
import static run.halo.app.extension.index.query.QueryFactory.or;

import java.util.Comparator;
import java.util.List;
Expand All @@ -15,12 +17,14 @@
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.content.Category;
import run.halo.app.core.extension.content.Post;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.PageRequestImpl;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.exception.ExtensionNotFoundException;
import run.halo.app.extension.index.query.Query;
import run.halo.app.extension.index.query.QueryFactory;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.extension.router.selector.LabelSelector;
Expand Down Expand Up @@ -141,13 +145,45 @@ private PageRequestImpl getPageRequest(Integer page, Integer size) {
@Override
public Mono<ListResult<ListedPostVo>> listByCategory(Integer page, Integer size,
String categoryName) {
var fieldQuery = QueryFactory.all();
var listOptions = new ListOptions();
var pageRequest = getPageRequest(page, size);

if (StringUtils.isNotBlank(categoryName)) {
fieldQuery = and(fieldQuery, equal("spec.categories", categoryName));
return childrenCategoryQuery(categoryName).flatMap(fieldQuery -> {
listOptions.setFieldSelector(FieldSelector.of(fieldQuery));
return postPublicQueryService.list(listOptions, pageRequest);
});
} else {
return postPublicQueryService.list(listOptions, pageRequest);
}
var listOptions = new ListOptions();
listOptions.setFieldSelector(FieldSelector.of(fieldQuery));
return postPublicQueryService.list(listOptions, getPageRequest(page, size));
}

private Mono<Query> childrenCategoryQuery(String categoryName) {
if (categoryName == null) {
return Mono.empty();
}

return traverseCategories(categoryName)
.map(childrenCategory -> equal("spec.categories", childrenCategory))
.collect(Collectors.toList())
.flatMap(this::mergeQuery);
}

Mono<Query> mergeQuery(List<Query> childrenQueryList) {
if (childrenQueryList.size() < 2) {
return Mono.just(and(all(), childrenQueryList.get(0)));
}
return Mono.just(
or(childrenQueryList.get(0), childrenQueryList.get(1), childrenQueryList)
);
}

private Flux<String> traverseCategories(String categoryName) {
return client.fetch(Category.class, categoryName).expand(
rootCategory -> Flux.fromIterable(rootCategory.getSpec().getChildren())
.flatMap(childCategory -> client.fetch(Category.class, childCategory)
.filter(category -> !category.getSpec().isIndependent()))
).map(category -> category.getMetadata().getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const initialFormState: Category = {
template: "",
priority: 0,
children: [],
isIndependent: false,
},
status: {},
apiVersion: "content.halo.run/v1alpha1",
Expand Down Expand Up @@ -279,6 +280,17 @@ const { handleGenerateSlug } = useSlugify(
type="select"
name="template"
></FormKit>
<FormKit
v-model="formState.spec.independent"
:label="
$t('core.post_category.editing_modal.fields.independent.label')
"
:help="
$t('core.post_category.editing_modal.fields.independent.help')
"
type="checkbox"
name="includeChildren"
></FormKit>
<FormKit
v-model="formState.spec.cover"
:help="$t('core.post_category.editing_modal.fields.cover.help')"
Expand Down
6 changes: 6 additions & 0 deletions ui/packages/api-client/src/models/category-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface CategorySpec {
'children'?: Array<string>;
/**
*
* @type {boolean}
* @memberof CategorySpec
*/
'independent'?: boolean;
/**
*
* @type {string}
* @memberof CategorySpec
*/
Expand Down
3 changes: 3 additions & 0 deletions ui/src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ core:
refresh_message: Regenerate slug based on display name.
template:
label: Custom template
independent:
label: Independent Category
help: Articles and the number of articles will not be counted towards the parent category.
cover:
label: Cover
help: Theme adaptation is required to support
Expand Down
3 changes: 3 additions & 0 deletions ui/src/locales/es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ core:
refresh_message: Regenerar slug basado en el nombre para mostrar.
template:
label: Plantilla personalizada
independent:
label: Categoría independiente
help: El número de artículos y artículos no se incluirá en la categoría padre.
cover:
label: Portada
help: Se requiere adaptación del tema para ser compatible
Expand Down
3 changes: 3 additions & 0 deletions ui/src/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ core:
refresh_message: 根据名称重新生成别名
template:
label: 自定义模板
independent:
label: 独立分类
help: 文章和文章数将不计入父级分类
cover:
label: 封面图
help: 需要主题适配以支持
Expand Down
3 changes: 3 additions & 0 deletions ui/src/locales/zh-TW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ core:
refresh_message: 根據名稱重新生成別名
template:
label: 自定義模板
independent:
label: 獨立分類
help: 文章和文章數將不計入父級分類
cover:
label: 封面圖
help: 需要主題適配以支持
Expand Down

0 comments on commit f16d73f

Please sign in to comment.