Skip to content

Commit

Permalink
[#124] refactor: PostContent 업데이트 메서드 제거 후, 새로운 객체를 통해 업데이트하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-mallang committed Dec 4, 2023
1 parent 7e2da6e commit 87226a9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 80 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public void update(
List<String> tags
) {
this.visibilityPolish = new PostVisibilityPolicy(visibility, password);
this.content.update(title, intro, bodyText, postThumbnailImageName, category, tags);
this.content = new PostContent(title, intro, bodyText, postThumbnailImageName, category, tags, getWriter());
}

public void delete() {
registerEvent(new PostDeleteEvent(getId()));
}

public void removeCategory() {
this.content.removeCategory();
this.content = content.removeCategory();
}

public void clickLike() {
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/com/mallang/post/domain/PostContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,10 @@ public PostContent(
Member writer
) {
this.title = title;
this.bodyText = bodyText;
this.postThumbnailImageName = postThumbnailImageName;
this.postIntro = new PostIntro(postIntro);
this.writer = writer;
setCategory(category);
setTags(tags);
}

public void update(
String title,
String postIntro,
String bodyText,
@Nullable String postThumbnailImageName,
@Nullable Category category,
List<String> tags
) {
this.title = title;
this.bodyText = bodyText;
this.postThumbnailImageName = postThumbnailImageName;
this.postIntro = new PostIntro(postIntro);
this.writer = writer;
setCategory(category);
setTags(tags);
}
Expand Down Expand Up @@ -114,8 +98,16 @@ public boolean isWriter(Member member) {
return writer.equals(member);
}

public void removeCategory() {
this.category = null;
public PostContent removeCategory() {
return new PostContent(
getTitle(),
getPostIntro(),
getBodyText(),
getPostThumbnailImageName(),
null,
getTags(),
getWriter()
);
}

public String getPostIntro() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/post/domain/draft/Draft.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public void update(
@Nullable Category category,
List<String> tags
) {
this.content.update(title, intro, bodyText, postThumbnailImageName, category, tags);
this.content = new PostContent(title, intro, bodyText, postThumbnailImageName, category, tags, getWriter());
}

public void removeCategory() {
this.content.removeCategory();
this.content = content.removeCategory();
}

public void validateWriter(Member member) {
Expand Down
63 changes: 6 additions & 57 deletions src/test/java/com/mallang/post/domain/PostContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.mallang.category.domain.Category;
import com.mallang.category.exception.NoAuthorityCategoryException;
import com.mallang.post.exception.DuplicatedTagsInPostException;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand Down Expand Up @@ -45,10 +44,14 @@ class PostContentTest {
.build();

// when
postContent.removeCategory();
PostContent removeCategory = postContent.removeCategory();

// then
assertThat(postContent.getCategory()).isNull();
assertThat(removeCategory.getCategory()).isNull();
assertThat(removeCategory)
.usingRecursiveComparison()
.ignoringFields("category")
.isEqualTo(postContent);
}

@Nested
Expand Down Expand Up @@ -155,60 +158,6 @@ class 생성_시 {
}
}

@Nested
class 수정_시 {

@Test
void 수정에_성공한다() {
// given
PostContent postContent = PostContent.builder()
.title("제목")
.postIntro("intro")
.bodyText("내용")
.writer(mallang)
.tags(List.of("태그1"))
.build();

// when
postContent.update(
"수정제목",
"수정인트로", "수정내용",
"postThumbnailImageName",
null,
List.of("태그2")
);

// then
assertThat(postContent.getTitle()).isEqualTo("수정제목");
assertThat(postContent.getBodyText()).isEqualTo("수정내용");
assertThat(postContent.getTags())
.containsExactly("태그2");
}

@Test
void 다른_사람의_카테고리로_수정_시_예외() {
// given
PostContent postContent = PostContent.builder()
.title("제목")
.postIntro("intro")
.bodyText("내용")
.writer(mallang)
.tags(List.of("태그1"))
.build();

// when & then
assertThatThrownBy(() -> {
postContent.update(
"수정제목",
"수정인트로", "수정내용",
"postThumbnailImageName",
otherCategory,
Collections.emptyList()
);
}).isInstanceOf(NoAuthorityCategoryException.class);
}
}

@Test
void 작성자_확인() {
// given
Expand Down

0 comments on commit 87226a9

Please sign in to comment.