Skip to content

Commit

Permalink
feat: 챕터 생성/조회 API 필드에 description 추가 (엔티티 클래스 필드도 추가)
Browse files Browse the repository at this point in the history
  • Loading branch information
sichoi42 committed Aug 22, 2024
1 parent 8a0e4a4 commit 4e0cfd5
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class AutobiographyController {
AutobiographyExceptionStatus.SUBCHAPTER_NUMBER_INVALID,
AutobiographyExceptionStatus.CHAPTER_NUMBER_FORMAT_INVALID,
AutobiographyExceptionStatus.CHAPTER_NAME_LENGTH_EXCEEDED,
AutobiographyExceptionStatus.CHAPTER_DESCRIPTION_LENGTH_EXCEEDED,
AutobiographyExceptionStatus.CHAPTER_ALREADY_EXISTS,
AutobiographyExceptionStatus.CHAPTER_SIZE_EXCEEDED,
AutobiographyExceptionStatus.CHAPTER_NUMBER_DUPLICATED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ public class ChapterRequestDto {
@Schema(description = "챕터 이름", example = "나의 첫번째 챕터")
private final String name;

@Schema(description = "챕터 설명", example = "나의 첫번째 챕터에 대한 설명")
private final String description;

@ArraySchema(schema = @Schema(implementation = SubchapterRequestDto.class))
private final List<SubchapterRequestDto> subchapters;

@JsonCreator
public ChapterRequestDto(@JsonProperty("number") String number,
@JsonProperty("name") String name,
@JsonProperty("description") String description,
@JsonProperty("subchapters") List<SubchapterRequestDto> subchapters) {
this.number = number;
this.name = name;
this.description = description;
this.subchapters = subchapters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ public class SubchapterRequestDto {
@Schema(description = "서브챕터 이름", example = "나의 첫번째 서브챕터")
private final String name;

@Schema(description = "챕터 설명", example = "나의 첫번째 서브챕터에 대한 설명")
private final String description;

@JsonCreator
public SubchapterRequestDto(@JsonProperty("number") String number,
@JsonProperty("name") String name) {
public SubchapterRequestDto(
@JsonProperty("number") String number,
@JsonProperty("name") String name,
@JsonProperty("description") String description) {
this.number = number;
this.name = name;
this.description = description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class ChapterDto {
@Schema(description = "챕터 이름", example = "Chapter 1: Early Life")
private final String chapterName;

@Schema(description = "챕터 설명", example = "This chapter is about my early life.")
private final String chapterDescription;

@Schema(description = "챕터 생성 날짜", example = "2023-01-01T00:00:00")
private final LocalDateTime chapterCreatedAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class SubchapterDto {
@Schema(description = "서브챕터 이름", example = "Subchapter 1: Childhood")
private final String chapterName;

@Schema(description = "서브챕터 설명", example = "This subchapter is about my childhood.")
private final String chapterDescription;

@Schema(description = "서브챕터 생성 날짜", example = "2023-01-01T01:00:00")
private final LocalDateTime chapterCreatedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void createChapters(Member member, ChapterCreateRequestDto chapterCreateR

LocalDateTime now = LocalDateTime.now();
List<Chapter> rootChapters = chapterCreateRequestDto.getChapters().stream()
.map(chapterDto -> Chapter.of(chapterDto.getNumber(), chapterDto.getName(), now,
.map(chapterDto -> Chapter.of(chapterDto.getNumber(), chapterDto.getName(),
chapterDto.getDescription(), now,
null, member))
.collect(Collectors.toList());

Expand All @@ -69,6 +70,7 @@ public void createChapters(Member member, ChapterCreateRequestDto chapterCreateR
AutobiographyExceptionStatus.CHAPTER_NOT_FOUND::toServiceException)
.getId();
return Chapter.of(subchapterDto.getNumber(), subchapterDto.getName(),
subchapterDto.getDescription(),
now,
parentChapterId, member);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public ChapterListResponseDto getChapters(Long memberId, Pageable pageable) {
.chapterId(chapter.getId())
.chapterNumber(chapter.getNumber())
.chapterName(chapter.getName())
.chapterDescription(chapter.getDescription())
.chapterCreatedAt(chapter.getCreatedAt())
.subChapters(subchapterDtos)
.build();
Expand All @@ -92,6 +93,7 @@ private List<SubchapterDto> getSubchapterDtos(Long parentChapterId) {
.chapterId(subchapter.getId())
.chapterNumber(subchapter.getNumber())
.chapterName(subchapter.getName())
.chapterDescription(subchapter.getDescription())
.chapterCreatedAt(subchapter.getCreatedAt())
.build()
).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public boolean isValid(ChapterCreateRequestDto value, ConstraintValidatorContext
return false;
}

// 챕터 설명 길이 검사
if (chapter.getDescription() != null && chapter.getDescription().length() > 64) {
context.buildConstraintViolationWithTemplate("BIO016")
.addConstraintViolation();
return false;
}

Set<String> subChapterNumbers = new HashSet<>();
if (chapter.getSubchapters() != null) {
for (SubchapterRequestDto subChapter : chapter.getSubchapters()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class Chapter {
@Setter
private String name;

@Column(nullable = false)
@Setter
private String description;

@Column(nullable = false)
@Setter
private LocalDateTime createdAt;
Expand All @@ -53,18 +57,20 @@ public class Chapter {
/* } 연관 정보 */

/* 생성자 { */
protected Chapter(String number, String name, LocalDateTime createdAt, Long parentChapterId,
protected Chapter(String number, String name, String description, LocalDateTime createdAt,
Long parentChapterId,
Member member) {
this.number = number;
this.name = name;
this.description = description;
this.createdAt = createdAt;
this.parentChapterId = parentChapterId;
this.member = member;
}

public static Chapter of(String number, String name, LocalDateTime createdAt,
public static Chapter of(String number, String name, String description, LocalDateTime createdAt,
Long parentChapterId, Member member) {
return new Chapter(number, name, createdAt, parentChapterId, member);
return new Chapter(number, name, description, createdAt, parentChapterId, member);
}

public void setChapterAutobiography(Autobiography chapterAutobiography) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public enum AutobiographyExceptionStatus implements ExceptionStatus {
CHAPTER_NUMBER_FORMAT_INVALID(400, "BIO012", "챕터 번호는 1, 1.1, 1.1.1과 같은 형식이어야 합니다."),
CHAPTER_SIZE_EXCEEDED(400, "BIO013", "챕터는 최소 1개, 최대 16개까지 생성할 수 있습니다."),
CHAPTER_NUMBER_DUPLICATED(400, "BIO014", "챕터 번호는 중복될 수 없습니다."),
NEXT_CHAPTER_NOT_FOUND(404, "BIO015", "다음 챕터가 존재하지 않습니다.");
NEXT_CHAPTER_NOT_FOUND(404, "BIO015", "다음 챕터가 존재하지 않습니다."),
CHAPTER_DESCRIPTION_LENGTH_EXCEEDED(400, "BIO016", "챕터 설명은 최대 64자까지 입력할 수 있습니다.");


private final int statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ void setUp() {
.andDo(print());
}

@Test
@DisplayName("실패 - 챕터 설명이 64자를 초과하는 챕터는 생성할 수 없음")
void 실패_챕터_설명이_64자를_초과하는_챕터는_생성할_수_없음() throws Exception {
// given
ChapterCreateRequestDto chapterCreateRequestDto = ChapterCreateRequestDto.builder()
.chapters(TestChapterCreateRequestDto.createTooLongDescriptionChapters())
.build();

// when
MockHttpServletRequestBuilder requestBuilder = post(
url)
.header(AUTHORIZE_VALUE, BEARER + token)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(chapterCreateRequestDto));
ResultActions resultActions = mockMvc.perform(requestBuilder);

// then
JsonMatcher response = JsonMatcher.create();
resultActions
.andExpect(status().isBadRequest())
.andExpect(response.get("code").isEquals("BIO016"))
.andDo(print());
}

@Test
@DisplayName("실패 - 챕터 번호가 중복되는 서브챕터는 생성할 수 없음")
void 실패_챕터_번호가_중복되는_서브챕터는_생성할_수_없음() throws Exception {
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/utils/testdouble/chapter/TestChapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class TestChapter implements TestEntity<Chapter, Long> {
@Builder.Default
private String name = null;
@Builder.Default
private String description = null;
@Builder.Default
private LocalDateTime createdAt = DEFAULT_TIME;
@Builder.Default
private Member member = null;
Expand All @@ -35,19 +37,25 @@ public static Chapter asDefaultEntity() {

public static List<Chapter> asDefaultEntities(Member member) {
return Arrays.asList(
TestChapter.builder().number("1").name("1장").member(member).build().asEntity(),
TestChapter.builder().number("2").name("2장").member(member).build().asEntity(),
TestChapter.builder().number("3").name("3장").member(member).build().asEntity()
TestChapter.builder().number("1").name("1장").description("1장 설명").member(member).build()
.asEntity(),
TestChapter.builder().number("2").name("2장").description("2장 설명").member(member).build()
.asEntity(),
TestChapter.builder().number("3").name("3장").description("3장 설명").member(member).build()
.asEntity()
);
}

public static List<Chapter> asDefaultSubchapterEntities(Chapter chapter, Member loginMember) {
return Arrays.asList(
TestChapter.builder().number(chapter.getNumber() + ".1").name(chapter.getNumber() + ".1절")
.description(chapter.getNumber() + ".1절 설명")
.member(loginMember).parentChapterId(chapter.getId()).build().asEntity(),
TestChapter.builder().number(chapter.getNumber() + ".2").name(chapter.getNumber() + ".2절")
.description(chapter.getNumber() + ".2절 설명")
.member(loginMember).parentChapterId(chapter.getId()).build().asEntity(),
TestChapter.builder().number(chapter.getNumber() + ".3").name(chapter.getNumber() + ".3절")
.description(chapter.getNumber() + ".3절 설명")
.member(loginMember).parentChapterId(chapter.getId()).build().asEntity()
);
}
Expand All @@ -57,6 +65,7 @@ public Chapter asEntity() {
return Chapter.of(
this.number,
this.name,
this.description,
this.createdAt,
this.parentChapterId,
this.member
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static List<ChapterRequestDto> createTooManyChapters() {
.mapToObj(i -> ChapterRequestDto.builder()
.number(String.valueOf(i + 1))
.name("Chapter " + (i + 1))
.description("This is chapter " + (i + 1) + ".")
.build())
.collect(Collectors.toList());
}
Expand All @@ -30,6 +31,7 @@ public static List<ChapterRequestDto> createInvalidNumberChapters() {
ChapterRequestDto.builder()
.number("blabla")
.name("Chapter 1")
.description("This is chapter 1.")
.build()
);
}
Expand All @@ -40,10 +42,12 @@ public static List<ChapterRequestDto> createDuplicatedNumberChapters() {
ChapterRequestDto.builder()
.number("1")
.name("Chapter 1")
.description("This is chapter 1.")
.build(),
ChapterRequestDto.builder()
.number("1")
.name("Chapter 2")
.description("This is chapter 2.")
.build()
);
}
Expand All @@ -54,6 +58,18 @@ public static List<ChapterRequestDto> createTooLongNameChapters() {
ChapterRequestDto.builder()
.number("1")
.name("a".repeat(65))
.description("This is chapter 1.")
.build()
);
}

// 챕터 설명이 64자를 초과하는 챕터를 생성하는 메소드
public static List<ChapterRequestDto> createTooLongDescriptionChapters() {
return List.of(
ChapterRequestDto.builder()
.number("1")
.name("Chapter 1")
.description("a".repeat(65))
.build()
);
}
Expand All @@ -64,14 +80,17 @@ public static List<ChapterRequestDto> createDuplicatedSubchapterNumberChapters()
ChapterRequestDto.builder()
.number("1")
.name("Chapter 1")
.description("This is chapter 1.")
.subchapters(Arrays.asList(
SubchapterRequestDto.builder()
.number("1.1")
.name("Subchapter 1")
.description("This is subchapter 1.")
.build(),
SubchapterRequestDto.builder()
.number("1.1")
.name("Subchapter 2")
.description("This is subchapter 2.")
.build()
))
.build()
Expand All @@ -84,10 +103,12 @@ public static List<ChapterRequestDto> createTooLongSubchapterNameChapters() {
ChapterRequestDto.builder()
.number("1")
.name("Chapter 1")
.description("This is chapter 1.")
.subchapters(List.of(
SubchapterRequestDto.builder()
.number("1.1")
.name("a".repeat(65))
.description("This is subchapter 1.")
.build()
))
.build()
Expand All @@ -100,10 +121,12 @@ public static List<ChapterRequestDto> createInvalidSubchapterNumberChapters() {
ChapterRequestDto.builder()
.number("1")
.name("Chapter 1")
.description("This is chapter 1.")
.subchapters(List.of(
SubchapterRequestDto.builder()
.number("2.1")
.name("Subchapter 1")
.description("This is subchapter 1.")
.build()
))
.build()
Expand All @@ -116,28 +139,34 @@ public static List<ChapterRequestDto> createValidChapters() {
ChapterRequestDto.builder()
.number("1")
.name("나의 첫번째 챕터")
.description("나의 첫번째 챕터 설명")
.subchapters(Arrays.asList(
SubchapterRequestDto.builder()
.number("1.1")
.name("나의 첫번째 서브챕터")
.description("나의 첫번째 서브챕터 설명")
.build(),
SubchapterRequestDto.builder()
.number("1.2")
.name("나의 두번째 서브챕터")
.description("나의 두번째 서브챕터 설명")
.build()
))
.build(),
ChapterRequestDto.builder()
.number("2")
.name("나의 두번째 챕터")
.description("나의 두번째 챕터 설명")
.subchapters(Arrays.asList(
SubchapterRequestDto.builder()
.number("2.1")
.name("나의 첫번째 서브챕터")
.description("나의 첫번째 서브챕터 설명")
.build(),
SubchapterRequestDto.builder()
.number("2.2")
.name("나의 두번째 서브챕터")
.description("나의 두번째 서브챕터 설명")
.build()
))
.build()
Expand Down

0 comments on commit 4e0cfd5

Please sign in to comment.