Skip to content

Commit

Permalink
feat: support set archives page size. (halo-dev#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Mar 2, 2020
1 parent b708069 commit a9fedef
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String listPost(Model model, String slug, Integer page) {
final Category category = categoryService.getBySlugOfNonNull(slug);
CategoryDTO categoryDTO = categoryService.convertTo(category);

final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), Sort.by(DESC, "createTime"));
final Pageable pageable = PageRequest.of(page - 1, optionService.getArchivesPageSize(), Sort.by(DESC, "createTime"));
Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public String list(Integer page, Model model) {
}

public String archives(Integer page, Model model) {
int pageSize = optionService.getPostPageSize();
int pageSize = optionService.getArchivesPageSize();
Pageable pageable = PageRequest
.of(page >= 1 ? page - 1 : page, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String listPost(Model model, String slug, Integer page) {
final Tag tag = tagService.getBySlugOfNonNull(slug);
TagDTO tagDTO = tagService.convertTo(tag);

final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), Sort.by(DESC, "createTime"));
final Pageable pageable = PageRequest.of(page - 1, optionService.getArchivesPageSize(), Sort.by(DESC, "createTime"));
Page<Post> postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* Post properties.
*
* @author johnniang
* @date 4/1/19
* @author ryanwang
* @date 2019-04-01
*/
public enum PostProperties implements PropertyEnum {

Expand All @@ -28,6 +29,11 @@ public enum PostProperties implements PropertyEnum {
*/
INDEX_PAGE_SIZE("post_index_page_size", Integer.class, "10"),

/**
* Archives page size.
*/
ARCHIVES_PAGE_SIZE("post_archives_page_size", Integer.class, "10"),

/**
* Post index sort.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/run/halo/app/service/OptionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface OptionService extends CrudService<Option, Integer> {

int DEFAULT_POST_PAGE_SIZE = 10;

int DEFAULT_ARCHIVES_PAGE_SIZE = 10;

int DEFAULT_COMMENT_PAGE_SIZE = 10;

int DEFAULT_RSS_PAGE_SIZE = 20;
Expand Down Expand Up @@ -299,6 +301,13 @@ public interface OptionService extends CrudService<Option, Integer> {
*/
int getPostPageSize();

/**
* Gets archives page size.
*
* @return page size
*/
int getArchivesPageSize();

/**
* Gets comment page size.
*
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/run/halo/app/service/impl/OptionServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* OptionService implementation class
*
* @author ryanwang
* @author johnniang
* @date 2019-03-14
*/
@Slf4j
Expand Down Expand Up @@ -365,13 +366,23 @@ public <V, E extends ValueEnum<V>> E getValueEnumByPropertyOrDefault(PropertyEnu
@Override
public int getPostPageSize() {
try {
return getByPropertyOrDefault(PostProperties.INDEX_PAGE_SIZE, Integer.class, DEFAULT_COMMENT_PAGE_SIZE);
return getByPropertyOrDefault(PostProperties.INDEX_PAGE_SIZE, Integer.class, DEFAULT_POST_PAGE_SIZE);
} catch (NumberFormatException e) {
log.error(PostProperties.INDEX_PAGE_SIZE.getValue() + " option is not a number format", e);
return DEFAULT_POST_PAGE_SIZE;
}
}

@Override
public int getArchivesPageSize() {
try {
return getByPropertyOrDefault(PostProperties.ARCHIVES_PAGE_SIZE, Integer.class, DEFAULT_ARCHIVES_PAGE_SIZE);
} catch (NumberFormatException e) {
log.error(PostProperties.ARCHIVES_PAGE_SIZE.getValue() + " option is not a number format", e);
return DEFAULT_POST_PAGE_SIZE;
}
}

@Override
public int getCommentPageSize() {
try {
Expand All @@ -385,7 +396,7 @@ public int getCommentPageSize() {
@Override
public int getRssPageSize() {
try {
return getByPropertyOrDefault(PostProperties.RSS_PAGE_SIZE, Integer.class, DEFAULT_COMMENT_PAGE_SIZE);
return getByPropertyOrDefault(PostProperties.RSS_PAGE_SIZE, Integer.class, DEFAULT_RSS_PAGE_SIZE);
} catch (NumberFormatException e) {
log.error(PostProperties.RSS_PAGE_SIZE.getValue() + " setting is not a number format", e);
return DEFAULT_RSS_PAGE_SIZE;
Expand Down

0 comments on commit a9fedef

Please sign in to comment.