mirror of https://github.com/halo-dev/halo
feat: support paging of archive pages. (#608)
* feat: support paging of archive pages. * fix: test case.pull/610/head
parent
62eba99d21
commit
b23755a568
|
@ -242,6 +242,7 @@ public class WebMvcAutoConfiguration extends WebMvcConfigurationSupport {
|
|||
blackPatterns.add("/fonts/**");
|
||||
blackPatterns.add("/css/**");
|
||||
blackPatterns.add("/assets/**");
|
||||
blackPatterns.add("/color.less");
|
||||
blackPatterns.add("/swagger-ui.html");
|
||||
blackPatterns.add("/csrf");
|
||||
blackPatterns.add("/webjars/**");
|
||||
|
|
|
@ -83,7 +83,7 @@ public class ContentContentController {
|
|||
public String content(@PathVariable("prefix") String prefix,
|
||||
Model model) {
|
||||
if (optionService.getArchivesPrefix().equals(prefix)) {
|
||||
return postModel.list(1, model, "is_archives", "archives");
|
||||
return postModel.archives(1, model);
|
||||
} else if (optionService.getCategoriesPrefix().equals(prefix)) {
|
||||
return categoryModel.list(model);
|
||||
} else if (optionService.getTagsPrefix().equals(prefix)) {
|
||||
|
@ -105,7 +105,7 @@ public class ContentContentController {
|
|||
@PathVariable(value = "page") Integer page,
|
||||
Model model) {
|
||||
if (optionService.getArchivesPrefix().equals(prefix)) {
|
||||
return postModel.list(page, model, "is_archives", "archives");
|
||||
return postModel.archives(page, model);
|
||||
} else if (optionService.getJournalsPrefix().equals(prefix)) {
|
||||
return journalModel.list(page, model);
|
||||
} else if (optionService.getPhotosPrefix().equals(prefix)) {
|
||||
|
|
|
@ -70,6 +70,6 @@ public class ContentIndexController {
|
|||
@GetMapping(value = "page/{page}")
|
||||
public String index(Model model,
|
||||
@PathVariable(value = "page") Integer page) {
|
||||
return postModel.list(page, model, "is_index", "index");
|
||||
return postModel.list(page, model);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.ui.Model;
|
||||
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.support.HaloConst;
|
||||
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.service.*;
|
||||
import run.halo.app.utils.MarkdownUtils;
|
||||
|
@ -119,7 +121,7 @@ public class PostModel {
|
|||
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();
|
||||
Pageable pageable = PageRequest
|
||||
.of(page >= 1 ? page - 1 : page, pageSize, postService.getPostDefaultSort());
|
||||
|
@ -151,12 +153,62 @@ public class PostModel {
|
|||
.append(optionService.getPathSuffix());
|
||||
}
|
||||
|
||||
model.addAttribute(decide, true);
|
||||
model.addAttribute("is_index", true);
|
||||
model.addAttribute("posts", posts);
|
||||
model.addAttribute("rainbow", rainbow);
|
||||
model.addAttribute("pageRainbow", rainbow);
|
||||
model.addAttribute("nextPageFullPath", nextPageFullPath.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");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package run.halo.app.model.vo;
|
|||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import run.halo.app.model.dto.post.BasePostMinimalDTO;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
@ -21,7 +20,7 @@ public class ArchiveYearVO {
|
|||
|
||||
private Integer year;
|
||||
|
||||
private List<BasePostMinimalDTO> posts;
|
||||
private List<PostListVO> posts;
|
||||
|
||||
public static class ArchiveComparator implements Comparator<ArchiveYearVO> {
|
||||
|
||||
|
|
|
@ -167,6 +167,22 @@ public interface PostService extends BasePostService<Post> {
|
|||
@NonNull
|
||||
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.
|
||||
*
|
||||
|
|
|
@ -242,6 +242,20 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
List<Post> posts = postRepository
|
||||
.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);
|
||||
|
||||
posts.forEach(post -> {
|
||||
|
@ -256,7 +270,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
// Build archive
|
||||
ArchiveYearVO archive = new ArchiveYearVO();
|
||||
archive.setYear(year);
|
||||
archive.setPosts(convertToMinimal(postList));
|
||||
archive.setPosts(convertToListVo(postList));
|
||||
|
||||
// Add archive
|
||||
archives.add(archive);
|
||||
|
@ -269,10 +283,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<ArchiveMonthVO> listMonthArchives() {
|
||||
// Get all posts
|
||||
List<Post> posts = postRepository
|
||||
.findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime"));
|
||||
public List<ArchiveMonthVO> convertToMonthArchives(List<Post> posts) {
|
||||
|
||||
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();
|
||||
archive.setYear(year);
|
||||
archive.setMonth(month);
|
||||
archive.setPosts(convertToMinimal(postList));
|
||||
archive.setPosts(convertToListVo(postList));
|
||||
|
||||
archives.add(archive);
|
||||
}));
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 9d604ccaac74c4a3a93c71d6c294cf799910ce22
|
||||
Subproject commit b50e52376237f47b9da289d8feafe891332e0943
|
Loading…
Reference in New Issue