Skip to content

Commit

Permalink
refactor: 메서드명 오타 수정과 형식 통일 (#140)
Browse files Browse the repository at this point in the history
* [#139] refactor: rename validateDependentReference to validateDescendantReference in CategoryValidator

* [#139] refactor: category 주석 수정

* [#139] refactor: Repository 메서드명 형식 통일

* [#139] refactor: polishing post
  • Loading branch information
shin-mallang authored Dec 8, 2023
1 parent 8b6ddb5 commit 9bed700
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 39 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/mallang/auth/domain/MemberRepository.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.mallang.auth.domain;

import com.mallang.auth.exception.NotFoundMemberException;
import jakarta.annotation.Nullable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Long> {

default Member getByIdIfIdNotNull(@Nullable Long id) {
if (id == null) {
return null;
}
return getById(id);
}

default Member getById(Long id) {
return findById(id).orElseThrow(NotFoundMemberException::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void updateHierarchy(UpdateCategoryHierarchyCommand command) {
}

private void updateHierarchy(Category target, Long parentId, Long prevId, Long nextId) {
Category parent = categoryRepository.getByNullableId(parentId);
Category prev = categoryRepository.getByNullableId(prevId);
Category next = categoryRepository.getByNullableId(nextId);
Category parent = categoryRepository.getByIdIfIdNotNull(parentId);
Category prev = categoryRepository.getByIdIfIdNotNull(prevId);
Category next = categoryRepository.getByIdIfIdNotNull(nextId);
target.updateHierarchy(parent, prev, next, categoryValidator);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/mallang/category/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ public List<Category> getSortedChildren() {
return categories;
}

// For lazy loading issue
// parent, prev, next 가 지연로딩되어 프록시로 조회되므로, 그냥 사용 시 update 가 동작하지 않음
// 이를 해결하기 위해 메서드를 통해 접근해야 하는데, private 혹은 package-private 경우 여전히 동작하지 않음
// 따라서 protected 로 설정한
// lazy loading issue 해결을 위한 메서드
// 카테고리 조회 시 parent, prev, next 가 지연로딩되어 프록시로 조회되므로, prev.next = this 등으로 사용 시 update 가 동작하지 않음
// 이를 해결하기 위해 메서드를 통해 접근해야 하는데 private 혹은 package-private 메서드의 경우 여전히 동작하지 않음
// 따라서 protected 로 설정함
protected void setPreviousSibling(Category previousSibling) {
this.previousSibling = previousSibling;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ default Category getById(Long id) {
@Query("SELECT c FROM Category c WHERE c.blog = :blog AND c.parent IS NULL")
List<Category> findAllRootByBlog(@Param("blog") Blog blog);

@Nullable
default Category getByNullableId(@Nullable Long categoryId) {
default Category getByIdIfIdNotNull(@Nullable Long categoryId) {
if (categoryId == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void validateUpdateHierarchy(
) {
validateOwners(target, parent, prevSibling, nextSibling);
validateSelfReference(target, parent, prevSibling, nextSibling);
validateDependentReference(target, parent, prevSibling, nextSibling);
validateDescendantReference(target, parent, prevSibling, nextSibling);
validateContinuous(prevSibling, nextSibling);
validateParentAndChildRelation(target, parent, prevSibling, nextSibling);
validateDuplicatedNameWhenParticipated(target, prevSibling, nextSibling);
Expand All @@ -68,7 +68,7 @@ private void validateSelfReference(Category target, Category parent, Category pr
}
}

private void validateDependentReference(
private void validateDescendantReference(
Category target,
Category parent,
Category prevSibling,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public void update(UpdateUnAuthCommentCommand command) {

public void delete(DeleteUnAuthCommentCommand command) {
UnAuthComment comment = commentRepository.getUnAuthCommentById(command.commentId());
Member member = (command.memberId() == null) ? null
: memberRepository.getById(command.memberId());
Member member = memberRepository.getByIdIfIdNotNull(command.memberId());
comment.validateDelete(member, command.password(), command.postPassword());
comment.delete(commentDeleteService);
}
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/com/mallang/post/application/DraftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.mallang.post.application.command.UpdateDraftCommand;
import com.mallang.post.domain.draft.Draft;
import com.mallang.post.domain.draft.DraftRepository;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -29,15 +28,15 @@ public class DraftService {
public Long create(CreateDraftCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByName(command.blogName());
Category category = getCategoryByIdIfPresent(command.categoryId());
Category category = categoryRepository.getByIdIfIdNotNull(command.categoryId());
Draft draft = command.toDraft(member, blog, category);
return draftRepository.save(draft).getId();
}

public void update(UpdateDraftCommand command) {
Member member = memberRepository.getById(command.memberId());
Draft draft = draftRepository.getById(command.draftId());
Category category = getCategoryByIdIfPresent(command.categoryId());
Category category = categoryRepository.getByIdIfIdNotNull(command.categoryId());
draft.validateWriter(member);
draft.update(
command.title(),
Expand All @@ -54,11 +53,4 @@ public void delete(DeleteDraftCommand command) {
draft.validateWriter(member);
draftRepository.delete(draft);
}

private Category getCategoryByIdIfPresent(@Nullable Long id) {
if (id == null) {
return null;
}
return categoryRepository.getById(id);
}
}
12 changes: 2 additions & 10 deletions src/main/java/com/mallang/post/application/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.mallang.post.domain.PostRepository;
import com.mallang.post.domain.draft.Draft;
import com.mallang.post.domain.draft.DraftRepository;
import jakarta.annotation.Nullable;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -45,7 +44,7 @@ public PostId createFromDraft(CreatePostCommand command, Long draftId) {
public PostId create(CreatePostCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByName(command.blogName());
Category category = getCategoryByIdIfPresent(command.categoryId());
Category category = categoryRepository.getByIdIfIdNotNull(command.categoryId());
PostId postId = postIdGenerator.generate(blog.getId());
Post post = command.toPost(member, postId, blog, category);
return postRepository.save(post).getId();
Expand All @@ -54,7 +53,7 @@ public PostId create(CreatePostCommand command) {
public void update(UpdatePostCommand command) {
Member member = memberRepository.getById(command.memberId());
Post post = postRepository.getById(command.postId(), command.blogName());
Category category = getCategoryByIdIfPresent(command.categoryId());
Category category = categoryRepository.getByIdIfIdNotNull(command.categoryId());
post.validateWriter(member);
post.update(
command.visibility(),
Expand All @@ -76,11 +75,4 @@ public void delete(DeletePostCommand command) {
postRepository.delete(post);
}
}

private Category getCategoryByIdIfPresent(@Nullable Long id) {
if (id == null) {
return null;
}
return categoryRepository.getById(id);
}
}
11 changes: 5 additions & 6 deletions src/main/java/com/mallang/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.mallang.post.exception.PostLikeCountNegativeException;
import jakarta.annotation.Nullable;
import jakarta.persistence.AssociationOverride;
import jakarta.persistence.AssociationOverrides;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -43,9 +42,7 @@ public class Post extends CommonRootEntity<PostId> {
private PostVisibilityPolicy visibilityPolish;

@Embedded
@AssociationOverrides(
@AssociationOverride(name = "tags", joinTable = @JoinTable(name = "post_tags"))
)
@AssociationOverride(name = "tags", joinTable = @JoinTable(name = "post_tags"))
private PostContent content;

private int likeCount = 0;
Expand Down Expand Up @@ -114,8 +111,10 @@ public void validateWriter(Member member) {
}
}

public void validateAccess(@Nullable Member member,
@Nullable String postPassword) {
public void validateAccess(
@Nullable Member member,
@Nullable String postPassword
) {
if (isWriter(member)) {
return;
}
Expand Down

0 comments on commit 9bed700

Please sign in to comment.