Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 메서드명 오타 수정과 형식 통일 #140

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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