diff --git a/src/main/java/com/mallang/post/domain/PostContent.java b/src/main/java/com/mallang/post/domain/PostContent.java index fd44455e..b36db7f8 100644 --- a/src/main/java/com/mallang/post/domain/PostContent.java +++ b/src/main/java/com/mallang/post/domain/PostContent.java @@ -58,14 +58,22 @@ public PostContent( Member writer ) { this.title = title; - this.postIntro = new PostIntro(postIntro); this.bodyText = bodyText; this.postThumbnailImageName = postThumbnailImageName; this.writer = writer; + setPostIntro(postIntro, bodyText); setPostCategory(category); setTags(tags); } + private void setPostIntro(String postIntro, String bodyText) { + if (postIntro == null || postIntro.isBlank()) { + this.postIntro = new PostIntro(bodyText.substring(0, Math.min(150, bodyText.length()))); + return; + } + this.postIntro = new PostIntro(postIntro); + } + private void setPostCategory(@Nullable PostCategory category) { if (category == null) { this.category = null; diff --git a/src/main/java/com/mallang/post/domain/PostIntro.java b/src/main/java/com/mallang/post/domain/PostIntro.java index d3d2c9cb..21a1cec3 100644 --- a/src/main/java/com/mallang/post/domain/PostIntro.java +++ b/src/main/java/com/mallang/post/domain/PostIntro.java @@ -13,7 +13,7 @@ @Embeddable public class PostIntro { - @Column(nullable = false, length = 250) + @Column(nullable = false, length = 150) private String intro; public PostIntro(String intro) { @@ -26,7 +26,7 @@ private void validateLength(String value) { throw new InvalidPostIntroLengthException(); } int length = value.strip().length(); - if (length < 1 || 250 < length) { + if (length < 1 || 150 < length) { throw new InvalidPostIntroLengthException(); } } diff --git a/src/main/java/com/mallang/post/presentation/request/CreateDraftRequest.java b/src/main/java/com/mallang/post/presentation/request/CreateDraftRequest.java index ef9073ca..61cd7927 100644 --- a/src/main/java/com/mallang/post/presentation/request/CreateDraftRequest.java +++ b/src/main/java/com/mallang/post/presentation/request/CreateDraftRequest.java @@ -7,7 +7,7 @@ public record CreateDraftRequest( String blogName, String title, - String intro, + @Nullable String intro, String bodyText, @Nullable String postThumbnailImageName, @Nullable Long categoryId, diff --git a/src/main/java/com/mallang/post/presentation/request/CreatePostRequest.java b/src/main/java/com/mallang/post/presentation/request/CreatePostRequest.java index 9f600652..38b19cc8 100644 --- a/src/main/java/com/mallang/post/presentation/request/CreatePostRequest.java +++ b/src/main/java/com/mallang/post/presentation/request/CreatePostRequest.java @@ -8,7 +8,7 @@ public record CreatePostRequest( String blogName, String title, - String intro, + @Nullable String intro, String bodyText, @Nullable String postThumbnailImageName, Visibility visibility, diff --git a/src/main/java/com/mallang/post/presentation/request/UpdateDraftRequest.java b/src/main/java/com/mallang/post/presentation/request/UpdateDraftRequest.java index cab45171..3b90a81f 100644 --- a/src/main/java/com/mallang/post/presentation/request/UpdateDraftRequest.java +++ b/src/main/java/com/mallang/post/presentation/request/UpdateDraftRequest.java @@ -6,7 +6,7 @@ public record UpdateDraftRequest( String title, - String intro, + @Nullable String intro, String bodyText, @Nullable String postThumbnailImageName, @Nullable Long categoryId, diff --git a/src/main/java/com/mallang/post/presentation/request/UpdatePostRequest.java b/src/main/java/com/mallang/post/presentation/request/UpdatePostRequest.java index 4e93a92a..14f20b45 100644 --- a/src/main/java/com/mallang/post/presentation/request/UpdatePostRequest.java +++ b/src/main/java/com/mallang/post/presentation/request/UpdatePostRequest.java @@ -8,7 +8,7 @@ public record UpdatePostRequest( String blogName, String title, - String intro, + @Nullable String intro, String bodyText, @Nullable String postThumbnailImageName, Visibility visibility, diff --git a/src/test/java/com/mallang/common/infra/s3/PresignedUrlServiceTest.java b/src/test/java/com/mallang/common/infra/s3/PresignedUrlServiceTest.java index 65fe6e52..9a9f5dd6 100644 --- a/src/test/java/com/mallang/common/infra/s3/PresignedUrlServiceTest.java +++ b/src/test/java/com/mallang/common/infra/s3/PresignedUrlServiceTest.java @@ -51,7 +51,7 @@ class PresignedUrlServiceTest { "https://mallang-bucket.s3.amazonaws.com/", "images/" + response.imageName(), ".img", - "X-Amz-Expires=" + 60 * 10 + "X-Amz-Expires=" ); } } diff --git a/src/test/java/com/mallang/post/domain/PostContentTest.java b/src/test/java/com/mallang/post/domain/PostContentTest.java index e3464f5b..c5463250 100644 --- a/src/test/java/com/mallang/post/domain/PostContentTest.java +++ b/src/test/java/com/mallang/post/domain/PostContentTest.java @@ -31,6 +31,34 @@ class PostContentTest { private final PostCategory jpaPostCategory = 하위_카테고리("JPA", mallang, blog, springPostCategory); private final PostCategory otherPostCategory = 루트_카테고리("Spring", otherMember, otherBlog); + @Test + void 인트로가_없으면_본문에서_가져와_설정된다() { + // when + PostContent postContent = PostContent.builder() + .title("제목") + .bodyText("내용") + .writer(mallang) + .build(); + + // then + String postIntro = postContent.getPostIntro(); + assertThat(postIntro).isEqualTo("내용"); + } + + @Test + void 인트로가_없을때_본문이_150자_이상이면_150자만_인트로로_설정된다() { + // when + PostContent postContent = PostContent.builder() + .title("제목") + .bodyText("1".repeat(151)) + .writer(mallang) + .build(); + + // then + String postIntro = postContent.getPostIntro(); + assertThat(postIntro.length()).isEqualTo(150); + } + @Test void 카테고리를_없앨_수_있다() { // given diff --git a/src/test/java/com/mallang/post/domain/PostIntroTest.java b/src/test/java/com/mallang/post/domain/PostIntroTest.java index 8ff7ec98..5fca550e 100644 --- a/src/test/java/com/mallang/post/domain/PostIntroTest.java +++ b/src/test/java/com/mallang/post/domain/PostIntroTest.java @@ -18,13 +18,13 @@ class PostIntroTest { @Test - void 길이는_1글자_이상_250_글자_이하여야_한다() { + void 길이는_1글자_이상_150_글자_이하여야_한다() { // when & then assertDoesNotThrow(() -> { new PostIntro("1"); }); assertDoesNotThrow(() -> { - new PostIntro("1".repeat(250)); + new PostIntro("1".repeat(150)); }); } @@ -38,9 +38,9 @@ class PostIntroTest { } @Test - void 길익가_250글자_초과라면_예외() { + void 길익가_150글자_초과라면_예외() { // given - String repeat = "1".repeat(251); + String repeat = "1".repeat(151); // when & then assertThatThrownBy(() -> { diff --git a/src/test/java/com/study/StringTest.java b/src/test/java/com/study/StringTest.java new file mode 100644 index 00000000..2ee0f5ba --- /dev/null +++ b/src/test/java/com/study/StringTest.java @@ -0,0 +1,20 @@ +package com.study; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class StringTest { + + @Test + void substring() { + // given + String str = "1".repeat(200); + + // when + String result = str.substring(0, 150); + + // then + assertThat(result.length()).isEqualTo(150); + } +}