Skip to content

Commit

Permalink
fix: 카테고리 제거 시 이전 카테고리와 다음 카테고리 이어주기 (#147)
Browse files Browse the repository at this point in the history
* [#146] fix: 카테고리 제거 시 이전 카테고리와 다음 카테고리 이어주기

* [#146] refactor: 포스트 카테고리 패키지 이동

* [#146] refactor: 포스트와 포스트 카테고리 패키지가 동일하므로 Event를 통해 역전시킨 의존성 되돌리기

* [#146] refactor: postCategoryRepository 필요없는 메서드 제거

* [#146] test: PostCategory 테스트 추가

* [#146] test: PostCategoryAcceptanceTest 테스트명 수정
  • Loading branch information
shin-mallang authored Dec 11, 2023
1 parent 296d80a commit cc234ad
Show file tree
Hide file tree
Showing 34 changed files with 384 additions and 315 deletions.
9 changes: 1 addition & 8 deletions src/main/java/com/mallang/category/TieredCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,7 @@ public void updateName(String name) {

public void delete() {
validateNoChildren();
unlinkFromParent();
}

private void unlinkFromParent() {
if (getParent() != null) {
getParent().getChildren().remove(self());
setParent(null);
}
withdrawCurrentHierarchy();
}

private void validateNoChildren() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/post/application/DraftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.mallang.post.application.command.CreateDraftCommand;
import com.mallang.post.application.command.DeleteDraftCommand;
import com.mallang.post.application.command.UpdateDraftCommand;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.category.PostCategoryRepository;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostCategoryRepository;
import com.mallang.post.domain.draft.Draft;
import com.mallang.post.domain.draft.DraftRepository;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.mallang.post.application.command.DeletePostCategoryCommand;
import com.mallang.post.application.command.UpdatePostCategoryHierarchyCommand;
import com.mallang.post.application.command.UpdatePostCategoryNameCommand;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.category.PostCategoryRepository;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostCategoryRepository;
import com.mallang.post.domain.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,6 +22,7 @@
public class PostCategoryService {

private final BlogRepository blogRepository;
private final PostRepository postRepository;
private final MemberRepository memberRepository;
private final PostCategoryRepository postCategoryRepository;

Expand Down Expand Up @@ -57,6 +60,8 @@ public void delete(DeletePostCategoryCommand command) {
PostCategory postCategory = postCategoryRepository.getById(command.categoryId());
postCategory.validateOwner(member);
postCategory.delete();
postRepository.findAllByCategory(postCategory)
.forEach(Post::removeCategory);
postCategoryRepository.delete(postCategory);
}
}
26 changes: 0 additions & 26 deletions src/main/java/com/mallang/post/application/PostEventHandler.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/com/mallang/post/application/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import com.mallang.post.application.command.DeletePostCommand;
import com.mallang.post.application.command.UpdatePostCommand;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostCategoryRepository;
import com.mallang.post.domain.PostId;
import com.mallang.post.domain.PostIdGenerator;
import com.mallang.post.domain.PostRepository;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.category.PostCategoryRepository;
import com.mallang.post.domain.draft.Draft;
import com.mallang.post.domain.draft.DraftRepository;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mallang.auth.domain.Member;
import com.mallang.blog.domain.Blog;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.draft.Draft;
import jakarta.annotation.Nullable;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mallang.auth.domain.Member;
import com.mallang.blog.domain.Blog;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostId;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import jakarta.annotation.Nullable;
import java.util.List;
import lombok.Builder;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/mallang/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.mallang.blog.domain.Blog;
import com.mallang.common.domain.CommonRootEntity;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.exception.NoAuthorityPostException;
import com.mallang.post.exception.PostLikeCountNegativeException;
import jakarta.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mallang.post.domain.category;
package com.mallang.post.domain;

import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;
Expand Down Expand Up @@ -47,12 +47,6 @@ public void validateOwner(Member member) {
}
}

@Override
public void delete() {
super.delete();
registerEvent(new PostCategoryDeletedEvent(getId()));
}

@Override
protected PostCategory self() {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.mallang.post.domain.category;
package com.mallang.post.domain;

import com.mallang.blog.domain.Blog;
import com.mallang.post.exception.NotFoundPostCategoryException;
import jakarta.annotation.Nullable;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PostCategoryRepository extends JpaRepository<PostCategory, Long> {

Expand All @@ -15,11 +11,6 @@ default PostCategory getById(Long id) {
return findById(id).orElseThrow(NotFoundPostCategoryException::new);
}

boolean existsByBlog(Blog blog);

@Query("SELECT c FROM PostCategory c WHERE c.blog = :blog AND c.parent IS NULL")
List<PostCategory> findAllRootByBlog(@Param("blog") Blog blog);

default PostCategory getByIdIfIdNotNull(@Nullable Long categoryId) {
if (categoryId == null) {
return null;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/mallang/post/domain/PostContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static lombok.AccessLevel.PROTECTED;

import com.mallang.auth.domain.Member;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.exception.DuplicatedTagsInPostException;
import jakarta.annotation.Nullable;
import jakarta.persistence.Column;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/post/domain/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Optional<Post> findById(
@Param("blogName") String blogName
);

@Query("SELECT p FROM Post p WHERE p.content.category.id = :categoryId")
List<Post> findAllByCategoryId(@Param("categoryId") Long categoryId);
@Query("SELECT p FROM Post p WHERE p.content.category = :category")
List<Post> findAllByCategory(@Param("category") PostCategory category);

@Query("SELECT p FROM Post p WHERE p.id.postId in :ids AND p.blog.name.value = :blogName")
List<Post> findAllByIdIn(
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/com/mallang/post/domain/draft/Draft.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.mallang.auth.domain.Member;
import com.mallang.blog.domain.Blog;
import com.mallang.common.domain.CommonRootEntity;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostContent;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.exception.NoAuthorityDraftException;
import jakarta.annotation.Nullable;
import jakarta.persistence.AssociationOverride;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mallang.post.query.repository;

import com.mallang.common.domain.CommonRootEntity;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.exception.NotFoundPostCategoryException;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -10,7 +10,7 @@

public interface PostCategoryQueryRepository extends JpaRepository<PostCategory, Long> {

default List<Long> getCategoryAndDescendants(Long id) {
default List<Long> getIdsWithDescendants(Long id) {
PostCategory postCategory = getById(id);
List<PostCategory> descendants = postCategory.getDescendants();
descendants.add(postCategory);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mallang.post.query.repository;

import static com.mallang.post.domain.QPost.post;
import static com.mallang.post.domain.category.QPostCategory.postCategory;
import static com.mallang.post.domain.QPostCategory.postCategory;
import static com.mallang.post.query.repository.PostManageSearchDao.PostManageSearchCond.NO_CATEGORY_CONDITION;

import com.mallang.blog.domain.Blog;
Expand Down Expand Up @@ -80,7 +80,7 @@ private BooleanExpression hasCategory(@Nullable Long categoryId) {
if (categoryId == NO_CATEGORY_CONDITION) {
return post.content.category.isNull();
}
List<Long> categoryIds = postCategoryQueryRepository.getCategoryAndDescendants(categoryId);
List<Long> categoryIds = postCategoryQueryRepository.getIdsWithDescendants(categoryId);
return post.content.category.id.in(categoryIds);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static com.mallang.blog.domain.QBlog.blog;
import static com.mallang.post.domain.PostVisibilityPolicy.Visibility.PRIVATE;
import static com.mallang.post.domain.QPost.post;
import static com.mallang.post.domain.QPostCategory.postCategory;
import static com.mallang.post.domain.QTag.tag;
import static com.mallang.post.domain.category.QPostCategory.postCategory;
import static org.springframework.data.support.PageableExecutionUtils.getPage;

import com.mallang.post.domain.Post;
Expand Down Expand Up @@ -98,7 +98,7 @@ private BooleanExpression hasCategory(@Nullable Long categoryId) {
if (categoryId == null) {
return null;
}
List<Long> categoryIds = postCategoryQueryRepository.getCategoryAndDescendants(categoryId);
List<Long> categoryIds = postCategoryQueryRepository.getIdsWithDescendants(categoryId);
return post.content.category.id.in(categoryIds);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mallang.post.query.response;

import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostContent;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.draft.Draft;
import jakarta.annotation.Nullable;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mallang.post.query.response;

import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.PostCategory;
import java.util.List;
import lombok.Builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mallang.auth.domain.Member;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mallang.post.query.response;

import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostContent;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mallang.post.query.response;

import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import java.time.LocalDateTime;
import lombok.Builder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.mallang.auth.domain.Member;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.mallang.auth.domain.Member;
import com.mallang.post.domain.Post;
import com.mallang.post.domain.PostCategory;
import com.mallang.post.domain.PostVisibilityPolicy.Visibility;
import com.mallang.post.domain.category.PostCategory;
import com.mallang.post.domain.star.PostStar;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import static com.mallang.acceptance.AcceptanceSteps.정상_처리;
import static com.mallang.acceptance.auth.AuthAcceptanceSteps.회원가입과_로그인_후_세션_ID_반환;
import static com.mallang.acceptance.blog.BlogAcceptanceSteps.블로그_개설;
import static com.mallang.acceptance.post.PostAcceptanceSteps.포스트_단일_조회_요청;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.블로그의_카테고리_조회_요청;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.카테고리_계층구조_수정_요청;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.카테고리_생성;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.카테고리_생성_요청;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.카테고리_이름_수정_요청;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.카테고리_제거_요청;
import static com.mallang.acceptance.post.PostCategoryAcceptanceSteps.카테고리_조회_응답을_검증한다;
import static com.mallang.acceptance.post.PostAcceptanceSteps.포스트_단일_조회_요청;
import static com.mallang.acceptance.post.PostManageAcceptanceSteps.포스트_생성;
import static com.mallang.post.domain.PostVisibilityPolicy.Visibility.PUBLIC;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -117,7 +117,7 @@ class 카테고리_생성_API {
}

@Test
void 타인의_카테고리_하위_카테고리로_지정하는_경우_예외() {
void 타인의_카테고리_계층에_참여하려는_경우_예외() {
// given
var 상위_카테고리_생성_응답 = 카테고리_생성_요청(말랑_세션_ID, Spring_카테고리_생성_요청);
var 상위_카테고리_ID = ID를_추출한다(상위_카테고리_생성_응답);
Expand Down Expand Up @@ -223,7 +223,7 @@ class 카테고리_계층구조_수정_API {
class 카테고리_이름_수정_API {

@Test
void 카테고리를_업데이트한다() {
void 카테고리_이름을_업데이트한다() {
// given
var Spring_카테고리_ID = ID를_추출한다(카테고리_생성_요청(말랑_세션_ID, Spring_카테고리_생성_요청));

Expand Down
Loading

0 comments on commit cc234ad

Please sign in to comment.