mirror of https://github.com/halo-dev/halo
refactor: add page size configuration for post (#2660)
parent
36613c0442
commit
8d6838a956
|
@ -54,6 +54,11 @@ public class SystemConfigurableEnvironmentFetcher {
|
|||
.switchIfEmpty(Mono.just(new SystemSetting.Comment()));
|
||||
}
|
||||
|
||||
public Mono<SystemSetting.Post> fetchPost() {
|
||||
return fetch(SystemSetting.Post.GROUP, SystemSetting.Post.class)
|
||||
.switchIfEmpty(Mono.just(new SystemSetting.Post()));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Mono<Map<String, String>> getValuesInternal() {
|
||||
return getConfigMap()
|
||||
|
|
|
@ -59,8 +59,10 @@ public class SystemSetting {
|
|||
@Data
|
||||
public static class Post {
|
||||
public static final String GROUP = "post";
|
||||
String sortOrder;
|
||||
Integer pageSize;
|
||||
Integer postPageSize;
|
||||
Integer archivePageSize;
|
||||
Integer categoryPageSize;
|
||||
Integer tagPageSize;
|
||||
Boolean review;
|
||||
}
|
||||
|
||||
|
|
|
@ -67,8 +67,10 @@ public class SiteSettingVo {
|
|||
.favicon(basicSetting.getFavicon())
|
||||
.allowRegistration(userSetting.getAllowRegistration())
|
||||
.post(PostSetting.builder()
|
||||
.sortOrder(postSetting.getSortOrder())
|
||||
.pageSize(postSetting.getPageSize())
|
||||
.postPageSize(postSetting.getPostPageSize())
|
||||
.archivePageSize(postSetting.getArchivePageSize())
|
||||
.categoryPageSize(postSetting.getCategoryPageSize())
|
||||
.tagPageSize(postSetting.getTagPageSize())
|
||||
.build())
|
||||
.seo(SeoSetting.builder()
|
||||
.blockSpiders(seoSetting.getBlockSpiders())
|
||||
|
@ -94,9 +96,13 @@ public class SiteSettingVo {
|
|||
@Value
|
||||
@Builder
|
||||
public static class PostSetting {
|
||||
String sortOrder;
|
||||
Integer postPageSize;
|
||||
|
||||
Integer pageSize;
|
||||
Integer archivePageSize;
|
||||
|
||||
Integer categoryPageSize;
|
||||
|
||||
Integer tagPageSize;
|
||||
}
|
||||
|
||||
@Value
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package run.halo.app.theme.router.strategy;
|
||||
|
||||
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.pageNum;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.totalPage;
|
||||
import static run.halo.app.theme.router.strategy.ModelConst.DEFAULT_PAGE_SIZE;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -12,6 +14,8 @@ import org.springframework.web.reactive.function.server.ServerRequest;
|
|||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import run.halo.app.extension.ListResult;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.infra.utils.PathUtils;
|
||||
import run.halo.app.theme.DefaultTemplateEnum;
|
||||
import run.halo.app.theme.finders.PostFinder;
|
||||
|
@ -29,17 +33,21 @@ import run.halo.app.theme.router.UrlContextListResult;
|
|||
@Component
|
||||
public class ArchivesRouteStrategy implements ListPageRouteHandlerStrategy {
|
||||
private final PostFinder postFinder;
|
||||
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
|
||||
public ArchivesRouteStrategy(PostFinder postFinder) {
|
||||
public ArchivesRouteStrategy(PostFinder postFinder,
|
||||
SystemConfigurableEnvironmentFetcher environmentFetcher) {
|
||||
this.postFinder = postFinder;
|
||||
this.environmentFetcher = environmentFetcher;
|
||||
}
|
||||
|
||||
private Mono<UrlContextListResult<PostArchiveVo>> postList(ServerRequest request) {
|
||||
String year = pathVariable(request, "year");
|
||||
String month = pathVariable(request, "month");
|
||||
String path = request.path();
|
||||
return Mono.defer(() -> Mono.just(postFinder.archives(pageNum(request), 10, year, month)))
|
||||
.publishOn(Schedulers.boundedElastic())
|
||||
return environmentFetcher.fetchPost()
|
||||
.map(postSetting -> defaultIfNull(postSetting.getArchivePageSize(), DEFAULT_PAGE_SIZE))
|
||||
.flatMap(pageSize -> listPost(pageNum(request), pageSize, year, month))
|
||||
.map(list -> new UrlContextListResult.Builder<PostArchiveVo>()
|
||||
.listResult(list)
|
||||
.nextUrl(PageUrlUtils.nextPageUrl(path, totalPage(list)))
|
||||
|
@ -47,6 +55,11 @@ public class ArchivesRouteStrategy implements ListPageRouteHandlerStrategy {
|
|||
.build());
|
||||
}
|
||||
|
||||
Mono<ListResult<PostArchiveVo>> listPost(int pageNum, int pageSize, String year, String month) {
|
||||
return Mono.fromCallable(() -> postFinder.archives(pageNum, pageSize, year, month))
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
private String pathVariable(ServerRequest request, String name) {
|
||||
Map<String, String> pathVariables = request.pathVariables();
|
||||
if (pathVariables.containsKey(name)) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package run.halo.app.theme.router.strategy;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
|
@ -20,13 +21,10 @@ import run.halo.app.theme.finders.vo.CategoryTreeVo;
|
|||
* @since 2.0.0
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class CategoriesRouteStrategy implements ListPageRouteHandlerStrategy {
|
||||
private final CategoryFinder categoryFinder;
|
||||
|
||||
public CategoriesRouteStrategy(CategoryFinder categoryFinder) {
|
||||
this.categoryFinder = categoryFinder;
|
||||
}
|
||||
|
||||
private Mono<List<CategoryTreeVo>> categories() {
|
||||
return Mono.defer(() -> Mono.just(categoryFinder.listAsTree()))
|
||||
.publishOn(Schedulers.boundedElastic());
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package run.halo.app.theme.router.strategy;
|
||||
|
||||
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.pageNum;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.totalPage;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
|
@ -13,6 +15,8 @@ import reactor.core.publisher.Mono;
|
|||
import reactor.core.scheduler.Schedulers;
|
||||
import run.halo.app.core.extension.Category;
|
||||
import run.halo.app.extension.GroupVersionKind;
|
||||
import run.halo.app.extension.ListResult;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.infra.SystemSetting;
|
||||
import run.halo.app.theme.DefaultTemplateEnum;
|
||||
import run.halo.app.theme.finders.CategoryFinder;
|
||||
|
@ -31,6 +35,7 @@ import run.halo.app.theme.router.ViewNameResolver;
|
|||
* @since 2.0.0
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class CategoryRouteStrategy implements DetailsPageRouteHandlerStrategy {
|
||||
private final GroupVersionKind gvk = GroupVersionKind.fromExtension(Category.class);
|
||||
private final PostFinder postFinder;
|
||||
|
@ -39,18 +44,14 @@ public class CategoryRouteStrategy implements DetailsPageRouteHandlerStrategy {
|
|||
|
||||
private final ViewNameResolver viewNameResolver;
|
||||
|
||||
public CategoryRouteStrategy(PostFinder postFinder,
|
||||
CategoryFinder categoryFinder, ViewNameResolver viewNameResolver) {
|
||||
this.postFinder = postFinder;
|
||||
this.categoryFinder = categoryFinder;
|
||||
this.viewNameResolver = viewNameResolver;
|
||||
}
|
||||
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
|
||||
private Mono<UrlContextListResult<PostVo>> postListByCategoryName(String name,
|
||||
ServerRequest request) {
|
||||
String path = request.path();
|
||||
return Mono.defer(() -> Mono.just(postFinder.listByCategory(pageNum(request), 10, name)))
|
||||
.publishOn(Schedulers.boundedElastic())
|
||||
return environmentFetcher.fetchPost()
|
||||
.map(post -> defaultIfNull(post.getCategoryPageSize(), ModelConst.DEFAULT_PAGE_SIZE))
|
||||
.flatMap(pageSize -> listPostsByCategory(pageNum(request), pageSize, name))
|
||||
.map(list -> new UrlContextListResult.Builder<PostVo>()
|
||||
.listResult(list)
|
||||
.nextUrl(PageUrlUtils.nextPageUrl(path, totalPage(list)))
|
||||
|
@ -63,6 +64,11 @@ public class CategoryRouteStrategy implements DetailsPageRouteHandlerStrategy {
|
|||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
public Mono<ListResult<PostVo>> listPostsByCategory(int page, int size, String categoryName) {
|
||||
return Mono.fromCallable(() -> postFinder.listByCategory(page, size, categoryName))
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerFunction<ServerResponse> getHandler(SystemSetting.ThemeRouteRules routeRules,
|
||||
String name) {
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
package run.halo.app.theme.router.strategy;
|
||||
|
||||
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.pageNum;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.totalPage;
|
||||
import static run.halo.app.theme.router.strategy.ModelConst.DEFAULT_PAGE_SIZE;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import run.halo.app.extension.ListResult;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.theme.DefaultTemplateEnum;
|
||||
import run.halo.app.theme.finders.PostFinder;
|
||||
import run.halo.app.theme.finders.vo.PostVo;
|
||||
|
@ -25,18 +30,17 @@ import run.halo.app.theme.router.UrlContextListResult;
|
|||
* @since 2.0.0
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class IndexRouteStrategy implements ListPageRouteHandlerStrategy {
|
||||
|
||||
private final PostFinder postFinder;
|
||||
|
||||
public IndexRouteStrategy(PostFinder postFinder) {
|
||||
this.postFinder = postFinder;
|
||||
}
|
||||
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
|
||||
private Mono<UrlContextListResult<PostVo>> postList(ServerRequest request) {
|
||||
String path = request.path();
|
||||
return Mono.defer(() -> Mono.just(postFinder.list(pageNum(request), 10)))
|
||||
.publishOn(Schedulers.boundedElastic())
|
||||
return environmentFetcher.fetchPost()
|
||||
.map(p -> defaultIfNull(p.getPostPageSize(), DEFAULT_PAGE_SIZE))
|
||||
.flatMap(pageSize -> listPost(pageNum(request), pageSize))
|
||||
.map(list -> new UrlContextListResult.Builder<PostVo>()
|
||||
.listResult(list)
|
||||
.nextUrl(PageUrlUtils.nextPageUrl(path, totalPage(list)))
|
||||
|
@ -44,6 +48,11 @@ public class IndexRouteStrategy implements ListPageRouteHandlerStrategy {
|
|||
.build());
|
||||
}
|
||||
|
||||
private Mono<ListResult<PostVo>> listPost(int page, int size) {
|
||||
return Mono.fromCallable(() -> postFinder.list(page, size))
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerFunction<ServerResponse> getHandler() {
|
||||
return request -> ServerResponse.ok()
|
||||
|
|
|
@ -9,4 +9,5 @@ package run.halo.app.theme.router.strategy;
|
|||
public enum ModelConst {
|
||||
;
|
||||
public static final String TEMPLATE_ID = "_templateId";
|
||||
public static final Integer DEFAULT_PAGE_SIZE = 10;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package run.halo.app.theme.router.strategy;
|
||||
|
||||
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.pageNum;
|
||||
import static run.halo.app.theme.router.PageUrlUtils.totalPage;
|
||||
|
||||
import java.util.Map;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
|
@ -13,6 +15,8 @@ import reactor.core.publisher.Mono;
|
|||
import reactor.core.scheduler.Schedulers;
|
||||
import run.halo.app.core.extension.Tag;
|
||||
import run.halo.app.extension.GroupVersionKind;
|
||||
import run.halo.app.extension.ListResult;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.infra.SystemSetting;
|
||||
import run.halo.app.theme.DefaultTemplateEnum;
|
||||
import run.halo.app.theme.finders.PostFinder;
|
||||
|
@ -30,22 +34,20 @@ import run.halo.app.theme.router.UrlContextListResult;
|
|||
* @since 2.0.0
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class TagRouteStrategy implements DetailsPageRouteHandlerStrategy {
|
||||
private final GroupVersionKind gvk = GroupVersionKind.fromExtension(Tag.class);
|
||||
private final PostFinder postFinder;
|
||||
|
||||
private final TagFinder tagFinder;
|
||||
|
||||
public TagRouteStrategy(PostFinder postFinder,
|
||||
TagFinder tagFinder) {
|
||||
this.postFinder = postFinder;
|
||||
this.tagFinder = tagFinder;
|
||||
}
|
||||
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
|
||||
private Mono<UrlContextListResult<PostVo>> postList(ServerRequest request, String name) {
|
||||
String path = request.path();
|
||||
return Mono.defer(() -> Mono.just(postFinder.listByTag(pageNum(request), 10, name)))
|
||||
.publishOn(Schedulers.boundedElastic())
|
||||
return environmentFetcher.fetchPost()
|
||||
.map(p -> defaultIfNull(p.getTagPageSize(), ModelConst.DEFAULT_PAGE_SIZE))
|
||||
.flatMap(pageSize -> listPostByTag(pageNum(request), pageSize, name))
|
||||
.map(list -> new UrlContextListResult.Builder<PostVo>()
|
||||
.listResult(list)
|
||||
.nextUrl(PageUrlUtils.nextPageUrl(path, totalPage(list)))
|
||||
|
@ -53,6 +55,11 @@ public class TagRouteStrategy implements DetailsPageRouteHandlerStrategy {
|
|||
.build());
|
||||
}
|
||||
|
||||
private Mono<ListResult<PostVo>> listPostByTag(int page, int size, String name) {
|
||||
return Mono.fromCallable(() -> postFinder.listByTag(page, size, name))
|
||||
.subscribeOn(Schedulers.boundedElastic());
|
||||
}
|
||||
|
||||
private Mono<TagVo> tagByName(String name) {
|
||||
return Mono.defer(() -> Mono.just(tagFinder.getByName(name)))
|
||||
.publishOn(Schedulers.boundedElastic());
|
||||
|
|
|
@ -23,22 +23,34 @@ spec:
|
|||
- group: post
|
||||
label: 文章设置
|
||||
formSchema:
|
||||
- $formkit: select
|
||||
label: "列表排序方式"
|
||||
name: sortOrder
|
||||
value: "publishTime"
|
||||
options:
|
||||
visitCount: "浏览量"
|
||||
publishTime: "发布时间"
|
||||
updateTime: "更新时间"
|
||||
validation: required
|
||||
- $formkit: number
|
||||
label: "列表显示条数"
|
||||
name: pageSize
|
||||
label: "文章列表显示条数"
|
||||
name: postPageSize
|
||||
value: 10
|
||||
min: 1
|
||||
max: 100
|
||||
validation: required
|
||||
validation: required | max:100
|
||||
- $formkit: number
|
||||
label: "归档页文章显示条数"
|
||||
name: archivePageSize
|
||||
value: 10
|
||||
min: 1
|
||||
max: 100
|
||||
validation: required | max:100
|
||||
- $formkit: number
|
||||
label: "分类页文章显示条数"
|
||||
name: categoryPageSize
|
||||
value: 10
|
||||
min: 1
|
||||
max: 100
|
||||
validation: required | max:100
|
||||
- $formkit: number
|
||||
label: "标签页文章显示条数"
|
||||
name: tagPageSize
|
||||
value: 10
|
||||
min: 1
|
||||
max: 100
|
||||
validation: required | max:100
|
||||
- $formkit: checkbox
|
||||
label: "新文章审核"
|
||||
value: false
|
||||
|
|
|
@ -47,6 +47,8 @@ abstract class RouterStrategyTestSuite {
|
|||
|
||||
@BeforeEach
|
||||
final void setUpParent() throws URISyntaxException {
|
||||
lenient().when(environmentFetcher.fetchPost())
|
||||
.thenReturn(Mono.just(new SystemSetting.Post()));
|
||||
lenient().when(environmentFetcher.fetch(eq(SystemSetting.ThemeRouteRules.GROUP),
|
||||
eq(SystemSetting.ThemeRouteRules.class))).thenReturn(Mono.just(getThemeRouteRules()));
|
||||
lenient().when(haloProperties.getExternalUrl()).thenReturn(new URI("http://example.com"));
|
||||
|
|
Loading…
Reference in New Issue