Skip to content

Commit

Permalink
feat: 포스트 인트로 없는 경우 내용에서 추출 (#212)
Browse files Browse the repository at this point in the history
* [#210] feat: 포스트 인트로 최대 글자 수 150자로 제한

* [#210] feat: 포스트 인트로 없는 경우 본문에서 추출

* [#208] test: 타이밍에 따라 깨지는 테스트 수정
  • Loading branch information
shin-mallang authored Jan 13, 2024
1 parent c951802 commit 44571a5
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/main/java/com/mallang/post/domain/PostContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/post/domain/PostIntro.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public record CreateDraftRequest(
String blogName,
String title,
String intro,
@Nullable String intro,
String bodyText,
@Nullable String postThumbnailImageName,
@Nullable Long categoryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public record CreatePostRequest(
String blogName,
String title,
String intro,
@Nullable String intro,
String bodyText,
@Nullable String postThumbnailImageName,
Visibility visibility,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public record UpdateDraftRequest(
String title,
String intro,
@Nullable String intro,
String bodyText,
@Nullable String postThumbnailImageName,
@Nullable Long categoryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public record UpdatePostRequest(
String blogName,
String title,
String intro,
@Nullable String intro,
String bodyText,
@Nullable String postThumbnailImageName,
Visibility visibility,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PresignedUrlServiceTest {
"https://mallang-bucket.s3.amazonaws.com/",
"images/" + response.imageName(),
".img",
"X-Amz-Expires=" + 60 * 10
"X-Amz-Expires="
);
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/mallang/post/domain/PostContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/mallang/post/domain/PostIntroTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}

Expand All @@ -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(() -> {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/study/StringTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 44571a5

Please sign in to comment.