From be0e1822214ac0b0fa5507c77e67fb0d226cd6c3 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Tue, 15 Jul 2025 23:13:16 +0800 Subject: [PATCH] fix post order --- .../openisle/repository/PostRepository.java | 20 ++++++++++++++++ .../com/openisle/service/PostService.java | 23 ++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/openisle/repository/PostRepository.java b/src/main/java/com/openisle/repository/PostRepository.java index 42d0d6da8..b7bde987f 100644 --- a/src/main/java/com/openisle/repository/PostRepository.java +++ b/src/main/java/com/openisle/repository/PostRepository.java @@ -16,15 +16,23 @@ import org.springframework.data.repository.query.Param; public interface PostRepository extends JpaRepository { List findByStatus(PostStatus status); List findByStatus(PostStatus status, Pageable pageable); + List findByStatusOrderByCreatedAtDesc(PostStatus status); + List findByStatusOrderByCreatedAtDesc(PostStatus status, Pageable pageable); List findByStatusOrderByViewsDesc(PostStatus status); List findByStatusOrderByViewsDesc(PostStatus status, Pageable pageable); List findByAuthorAndStatusOrderByCreatedAtDesc(User author, PostStatus status, Pageable pageable); List findByCategoryInAndStatus(List categories, PostStatus status); List findByCategoryInAndStatus(List categories, PostStatus status, Pageable pageable); + List findByCategoryInAndStatusOrderByCreatedAtDesc(List categories, PostStatus status); + List findByCategoryInAndStatusOrderByCreatedAtDesc(List categories, PostStatus status, Pageable pageable); List findDistinctByTagsInAndStatus(List tags, PostStatus status); List findDistinctByTagsInAndStatus(List tags, PostStatus status, Pageable pageable); + List findDistinctByTagsInAndStatusOrderByCreatedAtDesc(List tags, PostStatus status); + List findDistinctByTagsInAndStatusOrderByCreatedAtDesc(List tags, PostStatus status, Pageable pageable); List findDistinctByCategoryInAndTagsInAndStatus(List categories, List tags, PostStatus status); List findDistinctByCategoryInAndTagsInAndStatus(List categories, List tags, PostStatus status, Pageable pageable); + List findDistinctByCategoryInAndTagsInAndStatusOrderByCreatedAtDesc(List categories, List tags, PostStatus status); + List findDistinctByCategoryInAndTagsInAndStatusOrderByCreatedAtDesc(List categories, List tags, PostStatus status, Pageable pageable); // Queries requiring all provided tags to be present @Query("SELECT p FROM Post p JOIN p.tags t WHERE t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount") @@ -33,6 +41,12 @@ public interface PostRepository extends JpaRepository { @Query(value = "SELECT p FROM Post p JOIN p.tags t WHERE t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount") List findByAllTags(@Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount, Pageable pageable); + @Query("SELECT p FROM Post p JOIN p.tags t WHERE t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount ORDER BY p.createdAt DESC") + List findByAllTagsOrderByCreatedAtDesc(@Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount); + + @Query(value = "SELECT p FROM Post p JOIN p.tags t WHERE t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount ORDER BY p.createdAt DESC") + List findByAllTagsOrderByCreatedAtDesc(@Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount, Pageable pageable); + @Query("SELECT p FROM Post p JOIN p.tags t WHERE t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount ORDER BY p.views DESC") List findByAllTagsOrderByViewsDesc(@Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount); @@ -51,6 +65,12 @@ public interface PostRepository extends JpaRepository { @Query(value = "SELECT p FROM Post p JOIN p.tags t WHERE p.category IN :categories AND t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount ORDER BY p.views DESC") List findByCategoriesAndAllTagsOrderByViewsDesc(@Param("categories") List categories, @Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount, Pageable pageable); + @Query("SELECT p FROM Post p JOIN p.tags t WHERE p.category IN :categories AND t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount ORDER BY p.createdAt DESC") + List findByCategoriesAndAllTagsOrderByCreatedAtDesc(@Param("categories") List categories, @Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount); + + @Query(value = "SELECT p FROM Post p JOIN p.tags t WHERE p.category IN :categories AND t IN :tags AND p.status = :status GROUP BY p.id HAVING COUNT(DISTINCT t.id) = :tagCount ORDER BY p.createdAt DESC") + List findByCategoriesAndAllTagsOrderByCreatedAtDesc(@Param("categories") List categories, @Param("tags") List tags, @Param("status") PostStatus status, @Param("tagCount") long tagCount, Pageable pageable); + List findByCategoryInAndStatusOrderByViewsDesc(List categories, PostStatus status); List findByCategoryInAndStatusOrderByViewsDesc(List categories, PostStatus status, Pageable pageable); List findDistinctByTagsInAndStatusOrderByViewsDesc(List tags, PostStatus status); diff --git a/src/main/java/com/openisle/service/PostService.java b/src/main/java/com/openisle/service/PostService.java index a4f659d21..5eacdb3e8 100644 --- a/src/main/java/com/openisle/service/PostService.java +++ b/src/main/java/com/openisle/service/PostService.java @@ -24,6 +24,7 @@ import org.springframework.stereotype.Service; import java.util.List; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; @Service public class PostService { @@ -174,7 +175,7 @@ public class PostService { Integer pageSize) { Pageable pageable = null; if (page != null && pageSize != null) { - pageable = PageRequest.of(page, pageSize); + pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC, "createdAt"); } boolean hasCategories = categoryIds != null && !categoryIds.isEmpty(); @@ -225,21 +226,21 @@ public class PostService { Integer pageSize) { Pageable pageable = null; if (page != null && pageSize != null) { - pageable = PageRequest.of(page, pageSize); + pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC, "createdAt"); } if (categoryIds == null || categoryIds.isEmpty()) { if (pageable != null) { - return postRepository.findByStatus(PostStatus.PUBLISHED, pageable); + return postRepository.findByStatusOrderByCreatedAtDesc(PostStatus.PUBLISHED, pageable); } - return postRepository.findByStatus(PostStatus.PUBLISHED); + return postRepository.findByStatusOrderByCreatedAtDesc(PostStatus.PUBLISHED); } java.util.List categories = categoryRepository.findAllById(categoryIds); if (pageable != null) { - return postRepository.findByCategoryInAndStatus(categories, PostStatus.PUBLISHED, pageable); + return postRepository.findByCategoryInAndStatusOrderByCreatedAtDesc(categories, PostStatus.PUBLISHED, pageable); } - return postRepository.findByCategoryInAndStatus(categories, PostStatus.PUBLISHED); + return postRepository.findByCategoryInAndStatusOrderByCreatedAtDesc(categories, PostStatus.PUBLISHED); } public List getRecentPostsByUser(String username, int limit) { @@ -267,7 +268,7 @@ public class PostService { Pageable pageable = null; if (page != null && pageSize != null) { - pageable = PageRequest.of(page, pageSize); + pageable = PageRequest.of(page, pageSize, Sort.Direction.DESC, "createdAt"); } java.util.List tags = tagRepository.findAllById(tagIds); @@ -276,9 +277,9 @@ public class PostService { } if (pageable != null) { - return postRepository.findByAllTags(tags, PostStatus.PUBLISHED, tags.size(), pageable); + return postRepository.findByAllTagsOrderByCreatedAtDesc(tags, PostStatus.PUBLISHED, tags.size(), pageable); } - return postRepository.findByAllTags(tags, PostStatus.PUBLISHED, tags.size()); + return postRepository.findByAllTagsOrderByCreatedAtDesc(tags, PostStatus.PUBLISHED, tags.size()); } public List listPostsByCategoriesAndTags(java.util.List categoryIds, @@ -301,9 +302,9 @@ public class PostService { } if (pageable != null) { - return postRepository.findByCategoriesAndAllTags(categories, tags, PostStatus.PUBLISHED, tags.size(), pageable); + return postRepository.findByCategoriesAndAllTagsOrderByCreatedAtDesc(categories, tags, PostStatus.PUBLISHED, tags.size(), pageable); } - return postRepository.findByCategoriesAndAllTags(categories, tags, PostStatus.PUBLISHED, tags.size()); + return postRepository.findByCategoriesAndAllTagsOrderByCreatedAtDesc(categories, tags, PostStatus.PUBLISHED, tags.size()); } public List listPendingPosts() {