Add endpoints to list posts by tags and categories

This commit is contained in:
Tim
2025-07-02 14:39:54 +08:00
parent a6fd1b52ed
commit 8dbc940305
5 changed files with 83 additions and 0 deletions

View File

@@ -106,6 +106,29 @@ public class PostService {
return postRepository.findByAuthorAndStatusOrderByCreatedAtDesc(user, PostStatus.PUBLISHED, pageable);
}
public List<Post> listPostsByTags(java.util.List<Long> tagIds,
Integer page,
Integer pageSize) {
if (tagIds == null || tagIds.isEmpty()) {
return java.util.List.of();
}
Pageable pageable = null;
if (page != null && pageSize != null) {
pageable = PageRequest.of(page, pageSize);
}
java.util.List<com.openisle.model.Tag> tags = tagRepository.findAllById(tagIds);
if (tags.isEmpty()) {
return java.util.List.of();
}
if (pageable != null) {
return postRepository.findDistinctByTagsInAndStatus(tags, PostStatus.PUBLISHED, pageable);
}
return postRepository.findDistinctByTagsInAndStatus(tags, PostStatus.PUBLISHED);
}
public List<Post> listPendingPosts() {
return postRepository.findByStatus(PostStatus.PENDING);
}