From 948c94537e40b9794d159a39e55340ef368d9be7 Mon Sep 17 00:00:00 2001 From: Donghun Shin Date: Tue, 12 Dec 2023 15:33:06 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20=EC=88=9C=EC=84=9C=EB=8C=80?= =?UTF-8?q?=EB=A1=9C=20=EC=A1=B0=ED=9A=8C=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20(#154)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [#150] fix: 카테고리 조회 시 순서대로 조회되도록 변경 * [#150] test: 테스트 추가 --- .../post/query/PostCategoryQueryService.java | 20 ++++++++++++++-- .../PostCategoryQueryRepository.java | 4 ++-- .../query/PostCategoryQueryServiceTest.java | 23 +++++++++++++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/mallang/post/query/PostCategoryQueryService.java b/src/main/java/com/mallang/post/query/PostCategoryQueryService.java index 97cdf6b3..0c4efeaf 100644 --- a/src/main/java/com/mallang/post/query/PostCategoryQueryService.java +++ b/src/main/java/com/mallang/post/query/PostCategoryQueryService.java @@ -1,7 +1,9 @@ package com.mallang.post.query; +import com.mallang.post.domain.PostCategory; import com.mallang.post.query.repository.PostCategoryQueryRepository; import com.mallang.post.query.response.PostCategoryResponse; +import java.util.Collections; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -15,9 +17,23 @@ public class PostCategoryQueryService { private final PostCategoryQueryRepository postCategoryQueryRepository; public List findAllByBlogName(String blogName) { - return postCategoryQueryRepository.findAllRootByBlogName(blogName) - .stream() + List categories = postCategoryQueryRepository.findAllByBlogName(blogName); + if (categories.isEmpty()) { + return Collections.emptyList(); + } + PostCategory firstRoot = getFirstRoot(categories); + List roots = firstRoot.getSiblingsExceptSelf(); + roots.addFirst(firstRoot); + return roots.stream() .map(PostCategoryResponse::from) .toList(); } + + private PostCategory getFirstRoot(List all) { + return all.stream() + .filter(it -> it.getParent() == null) + .filter(it -> it.getPreviousSibling() == null) + .findAny() + .orElseThrow(); + } } diff --git a/src/main/java/com/mallang/post/query/repository/PostCategoryQueryRepository.java b/src/main/java/com/mallang/post/query/repository/PostCategoryQueryRepository.java index bfce2216..4e2de9fb 100644 --- a/src/main/java/com/mallang/post/query/repository/PostCategoryQueryRepository.java +++ b/src/main/java/com/mallang/post/query/repository/PostCategoryQueryRepository.java @@ -24,6 +24,6 @@ default PostCategory getById(Long id) { return findById(id).orElseThrow(NotFoundPostCategoryException::new); } - @Query("SELECT c FROM PostCategory c WHERE c.blog.name.value = :blogName AND c.parent IS NULL") - List findAllRootByBlogName(@Param("blogName") String blogName); + @Query("SELECT c FROM PostCategory c WHERE c.blog.name.value = :blogName") + List findAllByBlogName(@Param("blogName") String blogName); } diff --git a/src/test/java/com/mallang/post/query/PostCategoryQueryServiceTest.java b/src/test/java/com/mallang/post/query/PostCategoryQueryServiceTest.java index d7aaccce..8182c9af 100644 --- a/src/test/java/com/mallang/post/query/PostCategoryQueryServiceTest.java +++ b/src/test/java/com/mallang/post/query/PostCategoryQueryServiceTest.java @@ -16,6 +16,19 @@ @DisplayNameGeneration(ReplaceUnderscores.class) class PostCategoryQueryServiceTest extends ServiceTest { + @Test + void 카테고리_목록이_없는_경우_빈_리스트_반환() { + // given + var memberId = 회원을_저장한다("동훈"); + var 동훈_블로그_이름 = 블로그_개설(memberId, "donghun"); + + // when + List result = postCategoryQueryService.findAllByBlogName(동훈_블로그_이름); + + // then + assertThat(result).isEmpty(); + } + @Test void 특정_블로그의_카테고리를_순서대로_전체_조회한다() { // given @@ -85,8 +98,8 @@ class PostCategoryQueryServiceTest extends ServiceTest { 말랑_블로그_이름, "Algorithm", null, - springId, - null + null, + springId )); Long dfsId = postCategoryService.create(new CreatePostCategoryCommand( 말랑_ID, @@ -97,6 +110,9 @@ class PostCategoryQueryServiceTest extends ServiceTest { null )); List expected = List.of( + new PostCategoryResponse(algorithmId, "Algorithm", List.of( + new PostCategoryResponse(dfsId, "DFS", List.of()) + )), new PostCategoryResponse(springId, "Spring", List.of( new PostCategoryResponse(jpaId, "JPA", List.of( new PostCategoryResponse(n1Id, "N + 1", List.of()) @@ -105,9 +121,6 @@ class PostCategoryQueryServiceTest extends ServiceTest { new PostCategoryResponse(csrfId, "CSRF", List.of()), new PostCategoryResponse(oAuthId, "OAuth", List.of()) )) - )), - new PostCategoryResponse(algorithmId, "Algorithm", List.of( - new PostCategoryResponse(dfsId, "DFS", List.of()) )) );