Skip to content

Commit

Permalink
fix: 카테고리 조회 시 순서대로 조회되도록 변경 (#154)
Browse files Browse the repository at this point in the history
* [#150] fix: 카테고리 조회 시 순서대로 조회되도록 변경

* [#150] test: 테스트 추가
  • Loading branch information
shin-mallang authored Dec 12, 2023
1 parent 913a8eb commit 948c945
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/main/java/com/mallang/post/query/PostCategoryQueryService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,9 +17,23 @@ public class PostCategoryQueryService {
private final PostCategoryQueryRepository postCategoryQueryRepository;

public List<PostCategoryResponse> findAllByBlogName(String blogName) {
return postCategoryQueryRepository.findAllRootByBlogName(blogName)
.stream()
List<PostCategory> categories = postCategoryQueryRepository.findAllByBlogName(blogName);
if (categories.isEmpty()) {
return Collections.emptyList();
}
PostCategory firstRoot = getFirstRoot(categories);
List<PostCategory> roots = firstRoot.getSiblingsExceptSelf();
roots.addFirst(firstRoot);
return roots.stream()
.map(PostCategoryResponse::from)
.toList();
}

private PostCategory getFirstRoot(List<PostCategory> all) {
return all.stream()
.filter(it -> it.getParent() == null)
.filter(it -> it.getPreviousSibling() == null)
.findAny()
.orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostCategory> findAllRootByBlogName(@Param("blogName") String blogName);
@Query("SELECT c FROM PostCategory c WHERE c.blog.name.value = :blogName")
List<PostCategory> findAllByBlogName(@Param("blogName") String blogName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
@DisplayNameGeneration(ReplaceUnderscores.class)
class PostCategoryQueryServiceTest extends ServiceTest {

@Test
void 카테고리_목록이_없는_경우_빈_리스트_반환() {
// given
var memberId = 회원을_저장한다("동훈");
var 동훈_블로그_이름 = 블로그_개설(memberId, "donghun");

// when
List<PostCategoryResponse> result = postCategoryQueryService.findAllByBlogName(동훈_블로그_이름);

// then
assertThat(result).isEmpty();
}

@Test
void 특정_블로그의_카테고리를_순서대로_전체_조회한다() {
// given
Expand Down Expand Up @@ -85,8 +98,8 @@ class PostCategoryQueryServiceTest extends ServiceTest {
말랑_블로그_이름,
"Algorithm",
null,
springId,
null
null,
springId
));
Long dfsId = postCategoryService.create(new CreatePostCategoryCommand(
말랑_ID,
Expand All @@ -97,6 +110,9 @@ class PostCategoryQueryServiceTest extends ServiceTest {
null
));
List<PostCategoryResponse> 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())
Expand All @@ -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())
))
);

Expand Down

0 comments on commit 948c945

Please sign in to comment.