mirror of https://github.com/halo-dev/halo
Complete ArchiveController#listMonthArchives
parent
3c70b83c1d
commit
658667541d
|
@ -4,6 +4,8 @@ import lombok.Data;
|
|||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Archive vo.
|
||||
*
|
||||
|
@ -17,14 +19,17 @@ public class ArchiveMonthVO extends ArchiveYearVO {
|
|||
|
||||
private Integer month;
|
||||
|
||||
@Override
|
||||
public int compare(ArchiveYearVO current, ArchiveYearVO other) {
|
||||
int compare = super.compare(current, other);
|
||||
public static class ArchiveComparator implements Comparator<ArchiveMonthVO> {
|
||||
|
||||
if (compare != 0) {
|
||||
return compare;
|
||||
@Override
|
||||
public int compare(ArchiveMonthVO left, ArchiveMonthVO right) {
|
||||
int compare = right.getYear() - left.getYear();
|
||||
|
||||
if (compare != 0) {
|
||||
return compare;
|
||||
}
|
||||
|
||||
return right.getMonth() - left.getMonth();
|
||||
}
|
||||
|
||||
return ((ArchiveMonthVO) current).month - ((ArchiveMonthVO) other).month;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,14 +17,17 @@ import java.util.List;
|
|||
@Data
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
public class ArchiveYearVO implements Comparator<ArchiveYearVO> {
|
||||
public class ArchiveYearVO {
|
||||
|
||||
private Integer year;
|
||||
|
||||
private List<PostMinimalOutputDTO> posts;
|
||||
|
||||
@Override
|
||||
public int compare(ArchiveYearVO current, ArchiveYearVO other) {
|
||||
return current.year - other.year;
|
||||
public static class ArchiveComparator implements Comparator<ArchiveYearVO> {
|
||||
|
||||
@Override
|
||||
public int compare(ArchiveYearVO left, ArchiveYearVO right) {
|
||||
return right.getYear() - left.getYear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -263,7 +263,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
|||
// Get all posts
|
||||
List<Post> posts = postRepository.findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime"));
|
||||
|
||||
Map<Integer, List<Post>> yearPostMap = new HashMap<>();
|
||||
Map<Integer, List<Post>> yearPostMap = new HashMap<>(8);
|
||||
|
||||
posts.forEach(post -> {
|
||||
Calendar calendar = DateUtils.convertTo(post.getCreateTime());
|
||||
|
@ -271,6 +271,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
|||
.add(post);
|
||||
});
|
||||
|
||||
|
||||
List<ArchiveYearVO> archives = new LinkedList<>();
|
||||
|
||||
yearPostMap.forEach((year, postList) -> {
|
||||
|
@ -283,14 +284,42 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
|
|||
archives.add(archive);
|
||||
});
|
||||
|
||||
// TODO Sort this list and inner list
|
||||
// Sort this list
|
||||
archives.sort(new ArchiveYearVO.ArchiveComparator());
|
||||
|
||||
return archives;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ArchiveMonthVO> listMonthArchives() {
|
||||
return null;
|
||||
// Get all posts
|
||||
List<Post> posts = postRepository.findAllByStatus(PostStatus.PUBLISHED, Sort.by(DESC, "createTime"));
|
||||
|
||||
Map<Integer, Map<Integer, List<Post>>> yearMonthPostMap = new HashMap<>(8);
|
||||
|
||||
posts.forEach(post -> {
|
||||
Calendar calendar = DateUtils.convertTo(post.getCreateTime());
|
||||
|
||||
yearMonthPostMap.computeIfAbsent(calendar.get(Calendar.YEAR), year -> new HashMap<>())
|
||||
.computeIfAbsent((calendar.get(Calendar.MONTH) + 1), month -> new LinkedList<>())
|
||||
.add(post);
|
||||
});
|
||||
|
||||
List<ArchiveMonthVO> archives = new LinkedList<>();
|
||||
|
||||
yearMonthPostMap.forEach((year, monthPostMap) -> monthPostMap.forEach((month, postList) -> {
|
||||
ArchiveMonthVO archive = new ArchiveMonthVO();
|
||||
archive.setYear(year);
|
||||
archive.setMonth(month);
|
||||
archive.setPosts(convertTo(postList));
|
||||
|
||||
archives.add(archive);
|
||||
}));
|
||||
|
||||
// Sort this list
|
||||
archives.sort(new ArchiveMonthVO.ArchiveComparator());
|
||||
|
||||
return archives;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue