feat: support paging of archive pages. (#608)

* feat: support paging of archive pages.

* fix: test case.
pull/610/head
Ryan Wang 2020-03-01 13:52:27 +08:00 committed by GitHub
parent 62eba99d21
commit b23755a568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 15 deletions

View File

@ -242,6 +242,7 @@ public class WebMvcAutoConfiguration extends WebMvcConfigurationSupport {
blackPatterns.add("/fonts/**"); blackPatterns.add("/fonts/**");
blackPatterns.add("/css/**"); blackPatterns.add("/css/**");
blackPatterns.add("/assets/**"); blackPatterns.add("/assets/**");
blackPatterns.add("/color.less");
blackPatterns.add("/swagger-ui.html"); blackPatterns.add("/swagger-ui.html");
blackPatterns.add("/csrf"); blackPatterns.add("/csrf");
blackPatterns.add("/webjars/**"); blackPatterns.add("/webjars/**");

View File

@ -83,7 +83,7 @@ public class ContentContentController {
public String content(@PathVariable("prefix") String prefix, public String content(@PathVariable("prefix") String prefix,
Model model) { Model model) {
if (optionService.getArchivesPrefix().equals(prefix)) { if (optionService.getArchivesPrefix().equals(prefix)) {
return postModel.list(1, model, "is_archives", "archives"); return postModel.archives(1, model);
} else if (optionService.getCategoriesPrefix().equals(prefix)) { } else if (optionService.getCategoriesPrefix().equals(prefix)) {
return categoryModel.list(model); return categoryModel.list(model);
} else if (optionService.getTagsPrefix().equals(prefix)) { } else if (optionService.getTagsPrefix().equals(prefix)) {
@ -105,7 +105,7 @@ public class ContentContentController {
@PathVariable(value = "page") Integer page, @PathVariable(value = "page") Integer page,
Model model) { Model model) {
if (optionService.getArchivesPrefix().equals(prefix)) { if (optionService.getArchivesPrefix().equals(prefix)) {
return postModel.list(page, model, "is_archives", "archives"); return postModel.archives(page, model);
} else if (optionService.getJournalsPrefix().equals(prefix)) { } else if (optionService.getJournalsPrefix().equals(prefix)) {
return journalModel.list(page, model); return journalModel.list(page, model);
} else if (optionService.getPhotosPrefix().equals(prefix)) { } else if (optionService.getPhotosPrefix().equals(prefix)) {

View File

@ -70,6 +70,6 @@ public class ContentIndexController {
@GetMapping(value = "page/{page}") @GetMapping(value = "page/{page}")
public String index(Model model, public String index(Model model,
@PathVariable(value = "page") Integer page) { @PathVariable(value = "page") Integer page) {
return postModel.list(page, model, "is_index", "index"); return postModel.list(page, model);
} }
} }

View File

@ -5,6 +5,7 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import run.halo.app.cache.StringCacheStore; import run.halo.app.cache.StringCacheStore;
@ -17,6 +18,7 @@ import run.halo.app.model.enums.PostEditorType;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.HaloConst;
import run.halo.app.model.vo.AdjacentPostVO; import run.halo.app.model.vo.AdjacentPostVO;
import run.halo.app.model.vo.ArchiveYearVO;
import run.halo.app.model.vo.PostListVO; import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*; import run.halo.app.service.*;
import run.halo.app.utils.MarkdownUtils; import run.halo.app.utils.MarkdownUtils;
@ -119,7 +121,7 @@ public class PostModel {
return themeService.render("post"); return themeService.render("post");
} }
public String list(Integer page, Model model, String decide, String template) { public String list(Integer page, Model model) {
int pageSize = optionService.getPostPageSize(); int pageSize = optionService.getPostPageSize();
Pageable pageable = PageRequest Pageable pageable = PageRequest
.of(page >= 1 ? page - 1 : page, pageSize, postService.getPostDefaultSort()); .of(page >= 1 ? page - 1 : page, pageSize, postService.getPostDefaultSort());
@ -151,12 +153,62 @@ public class PostModel {
.append(optionService.getPathSuffix()); .append(optionService.getPathSuffix());
} }
model.addAttribute(decide, true); model.addAttribute("is_index", true);
model.addAttribute("posts", posts); model.addAttribute("posts", posts);
model.addAttribute("rainbow", rainbow); model.addAttribute("rainbow", rainbow);
model.addAttribute("pageRainbow", rainbow); model.addAttribute("pageRainbow", rainbow);
model.addAttribute("nextPageFullPath", nextPageFullPath.toString()); model.addAttribute("nextPageFullPath", nextPageFullPath.toString());
model.addAttribute("prePageFullPath", prePageFullPath.toString()); model.addAttribute("prePageFullPath", prePageFullPath.toString());
return themeService.render(template); return themeService.render("index");
}
public String archives(Integer page, Model model) {
int pageSize = optionService.getPostPageSize();
Pageable pageable = PageRequest
.of(page >= 1 ? page - 1 : page, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
Page<Post> postPage = postService.pageBy(PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
List<ArchiveYearVO> archives = postService.convertToYearArchives(postPage.getContent());
// TODO remove this variable
int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
// Next page and previous page url.
StringBuilder nextPageFullPath = new StringBuilder();
StringBuilder prePageFullPath = new StringBuilder();
if (optionService.isEnabledAbsolutePath()) {
nextPageFullPath.append(optionService.getBlogBaseUrl());
prePageFullPath.append(optionService.getBlogBaseUrl());
}
nextPageFullPath.append("/")
.append(optionService.getArchivesPrefix());
prePageFullPath.append("/")
.append(optionService.getArchivesPrefix());
nextPageFullPath.append("/page/")
.append(posts.getNumber() + 2)
.append(optionService.getPathSuffix());
if (posts.getNumber() == 1) {
prePageFullPath.append("/");
} else {
prePageFullPath.append("/page/")
.append(posts.getNumber())
.append(optionService.getPathSuffix());
}
model.addAttribute("is_archives", true);
model.addAttribute("posts", posts);
model.addAttribute("archives", archives);
model.addAttribute("rainbow", rainbow);
model.addAttribute("pageRainbow", rainbow);
model.addAttribute("nextPageFullPath", nextPageFullPath.toString());
model.addAttribute("prePageFullPath", prePageFullPath.toString());
return themeService.render("archives");
} }
} }

View File

@ -3,7 +3,6 @@ package run.halo.app.model.vo;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
import run.halo.app.model.dto.post.BasePostMinimalDTO;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -21,7 +20,7 @@ public class ArchiveYearVO {
private Integer year; private Integer year;
private List<BasePostMinimalDTO> posts; private List<PostListVO> posts;
public static class ArchiveComparator implements Comparator<ArchiveYearVO> { public static class ArchiveComparator implements Comparator<ArchiveYearVO> {

View File

@ -167,6 +167,22 @@ public interface PostService extends BasePostService<Post> {
@NonNull @NonNull
List<ArchiveMonthVO> listMonthArchives(); List<ArchiveMonthVO> listMonthArchives();
/**
* Convert to year archives
*
* @param posts posts must not be null
* @return list of ArchiveYearVO
*/
List<ArchiveYearVO> convertToYearArchives(@NonNull List<Post> posts);
/**
* Convert to month archives
*
* @param posts posts must not be null
* @return list of ArchiveMonthVO
*/
List<ArchiveMonthVO> convertToMonthArchives(@NonNull List<Post> posts);
/** /**
* Import post from markdown document. * Import post from markdown document.
* *

View File

@ -242,6 +242,20 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
List<Post> posts = postRepository List<Post> posts = postRepository
.findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime")); .findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime"));
return convertToYearArchives(posts);
}
@Override
public List<ArchiveMonthVO> listMonthArchives() {
// Get all posts
List<Post> posts = postRepository
.findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime"));
return convertToMonthArchives(posts);
}
@Override
public List<ArchiveYearVO> convertToYearArchives(List<Post> posts) {
Map<Integer, List<Post>> yearPostMap = new HashMap<>(8); Map<Integer, List<Post>> yearPostMap = new HashMap<>(8);
posts.forEach(post -> { posts.forEach(post -> {
@ -256,7 +270,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
// Build archive // Build archive
ArchiveYearVO archive = new ArchiveYearVO(); ArchiveYearVO archive = new ArchiveYearVO();
archive.setYear(year); archive.setYear(year);
archive.setPosts(convertToMinimal(postList)); archive.setPosts(convertToListVo(postList));
// Add archive // Add archive
archives.add(archive); archives.add(archive);
@ -269,10 +283,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
public List<ArchiveMonthVO> listMonthArchives() { public List<ArchiveMonthVO> convertToMonthArchives(List<Post> posts) {
// Get all posts
List<Post> posts = postRepository
.findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime"));
Map<Integer, Map<Integer, List<Post>>> yearMonthPostMap = new HashMap<>(8); Map<Integer, Map<Integer, List<Post>>> yearMonthPostMap = new HashMap<>(8);
@ -292,7 +303,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
ArchiveMonthVO archive = new ArchiveMonthVO(); ArchiveMonthVO archive = new ArchiveMonthVO();
archive.setYear(year); archive.setYear(year);
archive.setMonth(month); archive.setMonth(month);
archive.setPosts(convertToMinimal(postList)); archive.setPosts(convertToListVo(postList));
archives.add(archive); archives.add(archive);
})); }));

@ -1 +1 @@
Subproject commit 9d604ccaac74c4a3a93c71d6c294cf799910ce22 Subproject commit b50e52376237f47b9da289d8feafe891332e0943