From f1f9d8650e02f72e83d2e40b2f7f0726651d3e09 Mon Sep 17 00:00:00 2001 From: gracefulBrown Date: Thu, 6 Jun 2024 02:28:41 +0900 Subject: [PATCH] fix(article): change sort criteria to take into account publishedAt --- .../wooteco/prolog/article/application/ArticleService.java | 6 ++++++ .../prolog/article/domain/repository/ArticleRepository.java | 3 +++ .../java/wooteco/prolog/article/ui/ArticleController.java | 4 ++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/wooteco/prolog/article/application/ArticleService.java b/backend/src/main/java/wooteco/prolog/article/application/ArticleService.java index de9ca0ff1..e8260f080 100644 --- a/backend/src/main/java/wooteco/prolog/article/application/ArticleService.java +++ b/backend/src/main/java/wooteco/prolog/article/application/ArticleService.java @@ -107,6 +107,12 @@ public List getFilteredArticles(final LoginMember member, .collect(toList()); } + public List getArticlesByPublishedAt(final LoginMember member) { + return articleRepository.findAllOrderByPublishedAtOrCreatedAt().stream() + .map(article -> ArticleResponse.of(article, member.getId())) + .collect(toList()); + } + @Transactional public void updateViewCount(final Long id) { final Article article = articleRepository.findById(id) diff --git a/backend/src/main/java/wooteco/prolog/article/domain/repository/ArticleRepository.java b/backend/src/main/java/wooteco/prolog/article/domain/repository/ArticleRepository.java index 9853141b8..cf858bb11 100644 --- a/backend/src/main/java/wooteco/prolog/article/domain/repository/ArticleRepository.java +++ b/backend/src/main/java/wooteco/prolog/article/domain/repository/ArticleRepository.java @@ -12,6 +12,9 @@ public interface ArticleRepository extends JpaRepository { List
findAllByOrderByCreatedAtDesc(); + @Query("SELECT a FROM Article a ORDER BY COALESCE(a.publishedAt, a.createdAt) desc") + List
findAllOrderByPublishedAtOrCreatedAt(); + @Query("select a from Article a join fetch a.articleBookmarks where a.id = :id") Optional
findFetchBookmarkById(@Param("id") final Long id); diff --git a/backend/src/main/java/wooteco/prolog/article/ui/ArticleController.java b/backend/src/main/java/wooteco/prolog/article/ui/ArticleController.java index 2118fa194..8caabb18b 100644 --- a/backend/src/main/java/wooteco/prolog/article/ui/ArticleController.java +++ b/backend/src/main/java/wooteco/prolog/article/ui/ArticleController.java @@ -69,10 +69,10 @@ public ResponseEntity likeArticle(@PathVariable final Long id, } @GetMapping - public ResponseEntity> getFilteredArticles(@AuthMemberPrincipal final LoginMember member, + public ResponseEntity> getArticlesByPublishedAt(@AuthMemberPrincipal final LoginMember member, @RequestParam("course") final ArticleFilterType course, @RequestParam("onlyBookmarked") boolean onlyBookmarked) { - final List articleResponses = articleService.getFilteredArticles(member, course, onlyBookmarked); + final List articleResponses = articleService.getArticlesByPublishedAt(member); return ResponseEntity.ok(articleResponses); }