mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-06 12:00:44 +08:00
Add latest reply tab and backend support
This commit is contained in:
@@ -123,6 +123,12 @@ public class CommentService {
|
||||
return commentRepository.findAllById(ids);
|
||||
}
|
||||
|
||||
public java.time.LocalDateTime getLastCommentTime(Long postId) {
|
||||
Post post = postRepository.findById(postId)
|
||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
||||
return commentRepository.findLastCommentTime(post);
|
||||
}
|
||||
|
||||
@org.springframework.transaction.annotation.Transactional
|
||||
public void deleteComment(String username, Long id) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
|
||||
@@ -206,6 +206,47 @@ public class PostService {
|
||||
return paginate(sortByPinnedAndViews(posts), page, pageSize);
|
||||
}
|
||||
|
||||
public List<Post> listPostsByLatestReply(Integer page, Integer pageSize) {
|
||||
return listPostsByLatestReply(null, null, page, pageSize);
|
||||
}
|
||||
|
||||
public List<Post> listPostsByLatestReply(java.util.List<Long> categoryIds,
|
||||
java.util.List<Long> tagIds,
|
||||
Integer page,
|
||||
Integer pageSize) {
|
||||
boolean hasCategories = categoryIds != null && !categoryIds.isEmpty();
|
||||
boolean hasTags = tagIds != null && !tagIds.isEmpty();
|
||||
|
||||
java.util.List<Post> posts;
|
||||
|
||||
if (!hasCategories && !hasTags) {
|
||||
posts = postRepository.findByStatusOrderByCreatedAtDesc(PostStatus.PUBLISHED);
|
||||
} else if (hasCategories) {
|
||||
java.util.List<Category> categories = categoryRepository.findAllById(categoryIds);
|
||||
if (categories.isEmpty()) {
|
||||
return java.util.List.of();
|
||||
}
|
||||
if (hasTags) {
|
||||
java.util.List<com.openisle.model.Tag> tags = tagRepository.findAllById(tagIds);
|
||||
if (tags.isEmpty()) {
|
||||
return java.util.List.of();
|
||||
}
|
||||
posts = postRepository.findByCategoriesAndAllTagsOrderByCreatedAtDesc(
|
||||
categories, tags, PostStatus.PUBLISHED, tags.size());
|
||||
} else {
|
||||
posts = postRepository.findByCategoryInAndStatusOrderByCreatedAtDesc(categories, PostStatus.PUBLISHED);
|
||||
}
|
||||
} else {
|
||||
java.util.List<com.openisle.model.Tag> tags = tagRepository.findAllById(tagIds);
|
||||
if (tags.isEmpty()) {
|
||||
return java.util.List.of();
|
||||
}
|
||||
posts = postRepository.findByAllTagsOrderByCreatedAtDesc(tags, PostStatus.PUBLISHED, tags.size());
|
||||
}
|
||||
|
||||
return paginate(sortByPinnedAndLastReply(posts), page, pageSize);
|
||||
}
|
||||
|
||||
public List<Post> listPostsByCategories(java.util.List<Long> categoryIds,
|
||||
Integer page,
|
||||
Integer pageSize) {
|
||||
@@ -403,6 +444,17 @@ public class PostService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
private java.util.List<Post> sortByPinnedAndLastReply(java.util.List<Post> posts) {
|
||||
return posts.stream()
|
||||
.sorted(java.util.Comparator
|
||||
.comparing(Post::getPinnedAt, java.util.Comparator.nullsLast(java.util.Comparator.reverseOrder()))
|
||||
.thenComparing(p -> {
|
||||
java.time.LocalDateTime t = commentRepository.findLastCommentTime(p);
|
||||
return t != null ? t : p.getCreatedAt();
|
||||
}, java.util.Comparator.nullsLast(java.util.Comparator.reverseOrder())))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private java.util.List<Post> paginate(java.util.List<Post> posts, Integer page, Integer pageSize) {
|
||||
if (page == null || pageSize == null) {
|
||||
return posts;
|
||||
|
||||
Reference in New Issue
Block a user