diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequest.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequest.java index 97c018d0..fa34ea5f 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequest.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequest.java @@ -223,6 +223,11 @@ private String initSort(final String sort) { if (sort == null || sort.isBlank()) { return "writtenAt"; } + + if (sort.equals("category")) { + return "category.text"; + } + return validateSort(sort); } @@ -275,12 +280,21 @@ private String validateOrder(final String order) { .orElseThrow(() -> new IllegalArgumentException("정렬 순서는 asc, desc 중 하나여야 합니다.")); } + /** + * 정렬 기준을 Sort.Order로 변환 + * + * @return Sort.Order + */ + public Sort.Order getOrder() { + return Sort.Order.by(sort).with(convertOrderToSortDirection()); + } + /** * 정렬 순서를 Sort.Direction으로 변환 * * @return Sort.Direction */ - public Sort.Direction convertOrderToSortDirection() { + private Sort.Direction convertOrderToSortDirection() { if (order.equals("desc")) return Sort.Direction.DESC; else diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryService.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryService.java index d443db2a..193284d4 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryService.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryService.java @@ -2,6 +2,7 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.boards.common.category.repository.BoardCategoryRepository; import org.hyunggi.mygardenbe.boards.common.category.service.response.BoardCategoryResponse; import org.springframework.stereotype.Service; @@ -47,12 +48,13 @@ private void validateBoardType(final String boardType) { * * @param category 분류 * @param boardType 게시판 타입 + * @return 분류와 게시판 타입에 해당하는 분류 Entity */ - public void validateCategoryWithBoardType(final String category, final String boardType) { + public BoardCategoryEntity getCategoryWithBoardType(final String category, final String boardType) { validateCategory(category); validateBoardType(boardType); - boardCategoryRepository.findByCodeAndBoardType(category, boardType) + return boardCategoryRepository.findByCodeAndBoardType(category, boardType) .orElseThrow(() -> new EntityNotFoundException("해당 분류가 존재하지 않습니다.")); } diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardController.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardController.java index a82f53ef..15ea8979 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardController.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardController.java @@ -67,7 +67,7 @@ private PageRequest buildPageable(final GetRequest.SearchPaging searchPaging) { return PageRequest.of( searchPaging.currentPage(), searchPaging.pageSize(), - Sort.by(searchPaging.convertOrderToSortDirection(), searchPaging.sort(), "id") + Sort.by(searchPaging.getOrder(), Sort.Order.asc("id")) ); } @@ -79,6 +79,7 @@ private PageRequest buildPageable(final GetRequest.SearchPaging searchPaging) { */ @GetMapping("/{boardId}") public ApiResponse getLearnBoard(@PathVariable final Long boardId) { + //TODO: 게시판 조회시에 연관관계에 따라, Board Category를 TIL 게시판 조회 후에 또 다시 조회하는 문제가 있음 -> 당장 급한 문제는 아니니, 추후에 수정 return ApiResponse.ok(learnBoardService.getLearnBoard(boardId)); } diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntity.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntity.java index 7c6b16de..2967fae2 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntity.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntity.java @@ -4,6 +4,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.common.entity.BaseEntity; import java.time.LocalDateTime; @@ -37,8 +38,9 @@ public class LearnBoardEntity extends BaseEntity { /** * 분류 */ - @Column(nullable = false, length = 20) - private String category; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(nullable = false, name = "category_id") + private BoardCategoryEntity category; /** * 조회수 @@ -65,7 +67,7 @@ public class LearnBoardEntity extends BaseEntity { private Long memberId; @Builder(access = lombok.AccessLevel.PRIVATE) - private LearnBoardEntity(final String title, final String content, final String category, final String writer, final LocalDateTime writtenAt, final Long memberId) { + private LearnBoardEntity(final String title, final String content, final BoardCategoryEntity category, final String writer, final LocalDateTime writtenAt, final Long memberId) { this.title = title; this.content = content; this.category = category; @@ -86,7 +88,7 @@ private LearnBoardEntity(final String title, final String content, final String * @param memberId 작성자 ID * @return TIL 게시글 Entity */ - public static LearnBoardEntity of(final String title, final String content, final String category, final String writer, final Long memberId) { + public static LearnBoardEntity of(final String title, final String content, final BoardCategoryEntity category, final String writer, final Long memberId) { return of(title, content, category, writer, LocalDateTime.now(), memberId); } @@ -101,7 +103,7 @@ public static LearnBoardEntity of(final String title, final String content, fina * @param memberId 작성자 ID * @return TIL 게시글 Entity */ - public static LearnBoardEntity of(final String title, final String content, final String category, final String writer, final LocalDateTime writtenAt, final Long memberId) { + public static LearnBoardEntity of(final String title, final String content, final BoardCategoryEntity category, final String writer, final LocalDateTime writtenAt, final Long memberId) { validateConstructor(title, content, category, writer, writtenAt, memberId); return LearnBoardEntity.builder() @@ -124,7 +126,7 @@ public static LearnBoardEntity of(final String title, final String content, fina * @param writtenAt 작성일 * @param memberId 작성자 ID */ - private static void validateConstructor(final String title, final String content, final String category, final String writer, final LocalDateTime writtenAt, final Long memberId) { + private static void validateConstructor(final String title, final String content, final BoardCategoryEntity category, final String writer, final LocalDateTime writtenAt, final Long memberId) { validateTitle(title); validateContent(content); validateCategory(category); @@ -156,13 +158,13 @@ private static void validateContent(final String content) { } /** - * 분류 유효성 검사 + * 분류 검증 * * @param category 분류 */ - private static void validateCategory(final String category) { - if (category == null || category.isBlank()) { - throw new IllegalArgumentException("분류는 null이 될 수 없고 빈 문자열이 될 수 없습니다."); + private static void validateCategory(final BoardCategoryEntity category) { + if (category == null) { + throw new IllegalArgumentException("분류는 null이 될 수 없습니다."); } } @@ -213,7 +215,7 @@ public void increaseViewCount() { * @param content 내용 * @param category 분류 */ - public void update(final String title, final String content, final String category) { + public void update(final String title, final String content, final BoardCategoryEntity category) { validateUpdate(title, content, category); this.title = title; @@ -228,7 +230,7 @@ public void update(final String title, final String content, final String catego * @param content 내용 * @param category 분류 */ - private void validateUpdate(final String title, final String content, final String category) { + private void validateUpdate(final String title, final String content, final BoardCategoryEntity category) { validateTitle(title); validateContent(content); validateCategory(category); @@ -243,4 +245,13 @@ private void validateUpdate(final String title, final String content, final Stri public boolean isWriter(final Long memberId) { return this.memberId.equals(memberId); } + + /** + * 분류 코드 반환 + * + * @return 분류 코드 + */ + public String getCategoryCode() { + return category.getCode(); + } } diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryCustomImpl.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryCustomImpl.java index 6b9a1054..b6b7f17b 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryCustomImpl.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryCustomImpl.java @@ -53,6 +53,7 @@ public Page searchLearnBoards(final LocalDateTime startDateTim private Function getContentQuery(final LocalDateTime startDateTime, final LocalDateTime endDateTime, final String category, final String searchText) { return queryFactory -> queryFactory .selectFrom(learnBoardEntity) + .join(learnBoardEntity.category).fetchJoin() .where( writtenAtBetween(startDateTime, endDateTime), categoryEquals(category), @@ -102,7 +103,7 @@ private BooleanExpression categoryEquals(final String category) { return null; } - return learnBoardEntity.category.eq(category); + return learnBoardEntity.category.code.eq(category); } /** diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardService.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardService.java index 4f0d2bca..585242e4 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardService.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardService.java @@ -2,6 +2,7 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.boards.common.category.service.BoardCategoryService; import org.hyunggi.mygardenbe.boards.common.response.CustomPage; import org.hyunggi.mygardenbe.boards.learn.entity.LearnBoardEntity; @@ -134,12 +135,13 @@ private LearnBoardEntity getLearnBoardEntity(final Long boardId) { * @return 작성한 게시글 ID */ public Long postLearnBoard(final String category, final String title, final String content, final MemberEntity member) { - validatePostRequest(category, title, content); + validatePostRequest(title, content); + final BoardCategoryEntity categoryEntity = boardCategoryService.getCategoryWithBoardType(category, "learn"); final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( title, content, - category, + categoryEntity, getMemberEmailId(member), LocalDateTime.now(), member.getId() @@ -151,24 +153,20 @@ public Long postLearnBoard(final String category, final String title, final Stri /** * 게시글 작성의 인자 검증 * - * @param category 분류 - * @param title 제목 - * @param content 내용 + * @param title 제목 + * @param content 내용 */ - private void validatePostRequest(final String category, final String title, final String content) { - validateContent(category, title, content); + private void validatePostRequest(final String title, final String content) { + validateContent(title, content); } /** * 게시글 내용 유효성 검사 * - * @param category 분류 - * @param title 제목 - * @param content 내용 + * @param title 제목 + * @param content 내용 */ - private void validateContent(final String category, final String title, final String content) { - boardCategoryService.validateCategoryWithBoardType(category, "learn"); - + private void validateContent(final String title, final String content) { Assert.isTrue(StringUtils.hasText(title), "제목은 비어있을 수 없습니다."); Assert.isTrue(title.length() <= 100, "제목은 100자를 넘을 수 없습니다."); Assert.isTrue(StringUtils.hasText(content), "게시글 내용은 비어있을 수 없습니다."); @@ -197,7 +195,8 @@ private String getMemberEmailId(final MemberEntity member) { */ @Transactional public Long putLearnBoard(final Long boardId, final String category, final String title, final String content, final MemberEntity member) { - validatePutRequest(boardId, category, title, content); + validatePutRequest(boardId, title, content); + final BoardCategoryEntity categoryEntity = boardCategoryService.getCategoryWithBoardType(category, "learn"); final LearnBoardEntity learnBoardEntity = getLearnBoardEntity(boardId); @@ -206,7 +205,7 @@ public Long putLearnBoard(final Long boardId, final String category, final Strin learnBoardEntity.update( title, content, - category + categoryEntity ); return boardId; @@ -227,15 +226,14 @@ private void validateBoardWriter(final MemberEntity member, final LearnBoardEnti /** * 게시글 수정의 인자 검증 * - * @param boardId 게시글 ID - * @param category 분류 - * @param title 제목 - * @param content 내용 + * @param boardId 게시글 ID + * @param title 제목 + * @param content 내용 */ - private void validatePutRequest(final Long boardId, final String category, final String title, final String content) { + private void validatePutRequest(final Long boardId, final String title, final String content) { validateBoardId(boardId); - validateContent(category, title, content); + validateContent(title, content); } /** diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponse.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponse.java index b0d29f5a..8fdbb900 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponse.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponse.java @@ -68,7 +68,7 @@ public static LearnBoardResponse of(final LearnBoardEntity learnBoardEntity) { .id(learnBoardEntity.getId()) .title(learnBoardEntity.getTitle()) .content(learnBoardEntity.getContent()) - .category(learnBoardEntity.getCategory()) + .category(learnBoardEntity.getCategoryCode()) .views(learnBoardEntity.getViews()) .writer(learnBoardEntity.getWriter()) .writtenAt(learnBoardEntity.getWrittenAt().format(ofPattern("yyyy-MM-dd HH:mm:ss"))) diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardController.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardController.java index ac3b762a..6c30941a 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardController.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardController.java @@ -79,7 +79,7 @@ private PageRequest buildPageable(final GetRequest.SearchPaging searchPaging) { return PageRequest.of( searchPaging.currentPage(), searchPaging.pageSize(), - Sort.by(searchPaging.convertOrderToSortDirection(), searchPaging.sort(), "id") + Sort.by(searchPaging.getOrder(), Sort.Order.asc("id")) ); } @@ -91,6 +91,7 @@ private PageRequest buildPageable(final GetRequest.SearchPaging searchPaging) { */ @GetMapping("/{boardId}") public ApiResponse getNoticeBoard(@PathVariable final Long boardId) { + //TODO: 게시판 조회시에 연관관계에 따라, Board Category를 Notice 게시판 조회 후에 또 다시 조회하는 문제가 있음 -> 당장 급한 문제는 아니니, 추후에 수정 return ApiResponse.ok(noticeBoardService.getNoticeBoard(boardId)); } diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntity.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntity.java index 17807731..1b2da588 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntity.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntity.java @@ -4,6 +4,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.common.entity.BaseEntity; import java.time.LocalDateTime; @@ -37,8 +38,9 @@ public class NoticeBoardEntity extends BaseEntity { /** * 분류 */ - @Column(nullable = false, length = 20) - private String category; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(nullable = false, name = "category_id") + private BoardCategoryEntity category; /** * 중요 여부 @@ -71,7 +73,7 @@ public class NoticeBoardEntity extends BaseEntity { private Long memberId; @Builder(access = lombok.AccessLevel.PRIVATE) - private NoticeBoardEntity(final String title, final String content, final String category, final Boolean isImportant, final String writer, final LocalDateTime writtenAt, final Long memberId) { + private NoticeBoardEntity(final String title, final String content, final BoardCategoryEntity category, final Boolean isImportant, final String writer, final LocalDateTime writtenAt, final Long memberId) { this.title = title; this.content = content; this.category = category; @@ -93,7 +95,7 @@ private NoticeBoardEntity(final String title, final String content, final String * @param memberId 작성자 ID * @return 게시글 Entity */ - public static NoticeBoardEntity of(final String title, final String content, final String category, final String writer, final Long memberId) { + public static NoticeBoardEntity of(final String title, final String content, final BoardCategoryEntity category, final String writer, final Long memberId) { return of(title, content, category, false, writer, LocalDateTime.now(), memberId); } @@ -109,7 +111,7 @@ public static NoticeBoardEntity of(final String title, final String content, fin * @param memberId 작성자 ID * @return 게시글 Entity */ - public static NoticeBoardEntity of(final String title, final String content, final String category, final Boolean isImportant, final String writer, final LocalDateTime writtenAt, final Long memberId) { + public static NoticeBoardEntity of(final String title, final String content, final BoardCategoryEntity category, final Boolean isImportant, final String writer, final LocalDateTime writtenAt, final Long memberId) { validateConstructor(title, content, category, isImportant, writer, writtenAt, memberId); return NoticeBoardEntity.builder() @@ -134,7 +136,7 @@ public static NoticeBoardEntity of(final String title, final String content, fin * @param writtenAt 작성일 * @param memberId 작성자 ID */ - private static void validateConstructor(final String title, final String content, final String category, final Boolean isImportant, final String writer, final LocalDateTime writtenAt, final Long memberId) { + private static void validateConstructor(final String title, final String content, final BoardCategoryEntity category, final Boolean isImportant, final String writer, final LocalDateTime writtenAt, final Long memberId) { validateTitle(title); validateContent(content); validateCategory(category); @@ -171,9 +173,9 @@ private static void validateContent(final String content) { * * @param category 분류 */ - private static void validateCategory(final String category) { - if (category == null || category.isBlank()) { - throw new IllegalArgumentException("분류는 null이 될 수 없고 빈 문자열이 될 수 없습니다."); + private static void validateCategory(final BoardCategoryEntity category) { + if (category == null) { + throw new IllegalArgumentException("분류는 null이 될 수 없습니다."); } } @@ -243,7 +245,7 @@ public void increaseViewCount() { * @param category 분류 * @param important 중요 여부 */ - public void update(final String title, final String content, final String category, final Boolean important) { + public void update(final String title, final String content, final BoardCategoryEntity category, final Boolean important) { validateUpdate(title, content, category, important); this.title = title; @@ -260,7 +262,7 @@ public void update(final String title, final String content, final String catego * @param category 분류 * @param important 중요 여부 */ - private void validateUpdate(final String title, final String content, final String category, final Boolean important) { + private void validateUpdate(final String title, final String content, final BoardCategoryEntity category, final Boolean important) { validateTitle(title); validateContent(content); validateCategory(category); @@ -276,4 +278,13 @@ private void validateUpdate(final String title, final String content, final Stri public boolean isWriter(final Long memberId) { return this.memberId.equals(memberId); } + + /** + * 분류 코드 반환 + * + * @return 분류 코드 + */ + public String getCategoryCode() { + return category.getCode(); + } } diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryCustomImpl.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryCustomImpl.java index 0ffdf4f5..c128405e 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryCustomImpl.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryCustomImpl.java @@ -53,6 +53,7 @@ public Page searchNoticeBoards(final LocalDateTime startDateT private Function getContentQuery(final LocalDateTime startDateTime, final LocalDateTime endDateTime, final String category, final String searchText) { return queryFactory -> queryFactory .selectFrom(noticeBoardEntity) + .join(noticeBoardEntity.category).fetchJoin() .where( writtenAtBetween(startDateTime, endDateTime), categoryEquals(category), @@ -104,7 +105,7 @@ private BooleanExpression categoryEquals(final String category) { return null; } - return noticeBoardEntity.category.eq(category); + return noticeBoardEntity.category.code.eq(category); } /** diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardService.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardService.java index 0088e11e..ab8e0ec3 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardService.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardService.java @@ -2,6 +2,7 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.boards.common.category.service.BoardCategoryService; import org.hyunggi.mygardenbe.boards.common.response.CustomPage; import org.hyunggi.mygardenbe.boards.notice.entity.NoticeBoardEntity; @@ -137,12 +138,13 @@ private NoticeBoardEntity getNoticeBoardEntity(final Long boardId) { * @return 게시글 ID */ public Long postNoticeBoard(final String category, final String title, final String content, final Boolean isImportant, final MemberEntity member) { - validatePostRequest(category, title, content, isImportant); + validatePostRequest(title, content, isImportant); + final BoardCategoryEntity categoryEntity = boardCategoryService.getCategoryWithBoardType(category, "notice"); final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( title, content, - category, + categoryEntity, isImportant, getMemberEmailId(member), LocalDateTime.now(), @@ -155,26 +157,22 @@ public Long postNoticeBoard(final String category, final String title, final Str /** * 게시글 작성의 인자 검증 * - * @param category 분류 * @param title 제목 * @param content 내용 * @param isImportant 중요 여부 */ - private void validatePostRequest(final String category, final String title, final String content, final Boolean isImportant) { - validateContent(category, title, content, isImportant); + private void validatePostRequest(final String title, final String content, final Boolean isImportant) { + validateContent(title, content, isImportant); } /** * 게시글 작성의 내용 검증 * - * @param category 분류 * @param title 제목 * @param content 내용 * @param isImportant 중요 여부 */ - private void validateContent(final String category, final String title, final String content, final Boolean isImportant) { - boardCategoryService.validateCategoryWithBoardType(category, "notice"); - + private void validateContent(final String title, final String content, final Boolean isImportant) { Assert.isTrue(title != null, "제목은 null이 될 수 없습니다."); Assert.isTrue(content != null, "내용은 null이 될 수 없습니다."); Assert.isTrue(title.length() <= 100, "제목은 100자 이하여야 합니다."); @@ -205,7 +203,8 @@ private String getMemberEmailId(final MemberEntity member) { */ @Transactional public Long putNoticeBoard(final Long boardId, final String category, final String title, final String content, final Boolean isImportant, final MemberEntity member) { - validatePutRequest(boardId, category, title, content, isImportant); + validatePutRequest(boardId, title, content, isImportant); + final BoardCategoryEntity categoryEntity = boardCategoryService.getCategoryWithBoardType(category, "notice"); final NoticeBoardEntity noticeBoardEntity = getNoticeBoardEntity(boardId); @@ -214,7 +213,7 @@ public Long putNoticeBoard(final Long boardId, final String category, final Stri noticeBoardEntity.update( title, content, - category, + categoryEntity, isImportant ); @@ -237,15 +236,14 @@ private void validateBoardWriter(final MemberEntity member, final NoticeBoardEnt * 게시글 수정의 인자 검증 * * @param boardId 게시글 ID - * @param category 분류 * @param title 제목 * @param content 내용 * @param isImportant 중요 여부 */ - private void validatePutRequest(final Long boardId, final String category, final String title, final String content, final Boolean isImportant) { + private void validatePutRequest(final Long boardId, final String title, final String content, final Boolean isImportant) { validateBoardId(boardId); - validateContent(category, title, content, isImportant); + validateContent(title, content, isImportant); } /** diff --git a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponse.java b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponse.java index be1ee112..7af064b4 100644 --- a/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponse.java +++ b/my-garden-be/src/main/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponse.java @@ -74,7 +74,7 @@ public static NoticeBoardResponse of(final NoticeBoardEntity noticeBoardEntity) .id(noticeBoardEntity.getId()) .title(noticeBoardEntity.getTitle()) .content(noticeBoardEntity.getContent()) - .category(noticeBoardEntity.getCategory()) + .category(noticeBoardEntity.getCategoryCode()) .isImportant(noticeBoardEntity.getIsImportant()) .views(noticeBoardEntity.getViews()) .writer(noticeBoardEntity.getWriter()) diff --git a/my-garden-be/src/main/resources/application.yaml b/my-garden-be/src/main/resources/application.yaml index 296afe48..0a18524d 100644 --- a/my-garden-be/src/main/resources/application.yaml +++ b/my-garden-be/src/main/resources/application.yaml @@ -13,7 +13,7 @@ spring: jpa: show-sql: true hibernate: - ddl-auto: update + ddl-auto: validate properties: hibernate: format_sql: true diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequestTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequestTest.java index 42f12601..70dff320 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequestTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/request/GetRequestTest.java @@ -2,9 +2,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; import org.springframework.data.domain.Sort; import java.time.LocalDate; @@ -265,25 +263,6 @@ void getRequestConstructor() { assertThat(requestSearchPaging.order()).isEqualTo("desc"); } - @ParameterizedTest - @MethodSource("provideGetRequestOrder") - @DisplayName("convertOrderToSortDirection() 메서드를 통해, order를 Sort.Direction로 변환한다.") - void convertOrderToSortDirection(final String order, final Sort.Direction expected) { - //given - final GetRequest.SearchPaging searchPaging = GetRequest.SearchPaging.builder() - .currentPage(null) - .pageSize(null) - .sort(null) - .order(order) - .build(); - - //when - final Sort.Direction sortDirection = searchPaging.convertOrderToSortDirection(); - - //then - assertThat(sortDirection).isEqualTo(expected); - } - public static Stream provideGetRequestOrder() { return Stream.of( Arguments.of("desc", Sort.Direction.DESC), @@ -311,4 +290,25 @@ void currentPage() { //then assertThat(searchPaging.currentPage()).isEqualTo(1); } + + @Test + @DisplayName("getOrder()를 통해 정렬 순서를 반환한다.") + void getOrder() { + //given + final Integer currentPage = 0; + final Integer pageSize = 10; + final String sort = "writtenAt"; + final String order = "desc"; + + //when + final GetRequest.SearchPaging searchPaging = GetRequest.SearchPaging.builder() + .currentPage(currentPage) + .pageSize(pageSize) + .sort(sort) + .order(order) + .build(); + + //then + assertThat(searchPaging.getOrder()).isEqualTo(Sort.Order.desc("writtenAt")); + } } diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryServiceTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryServiceTest.java index 43c8fbfc..be112e1e 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryServiceTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/common/category/service/BoardCategoryServiceTest.java @@ -62,7 +62,7 @@ void validateBoardType(final String boardType) { boardCategoryRepository.save(boardCategoryEntity); //when, then - assertThatThrownBy(() -> boardCategoryService.validateCategoryWithBoardType("study", boardType)) + assertThatThrownBy(() -> boardCategoryService.getCategoryWithBoardType("study", boardType)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("게시판 타입은 비어있을 수 없습니다."); } @@ -77,7 +77,7 @@ void validateCategory(final String category) { boardCategoryRepository.save(boardCategoryEntity); //when, then - assertThatThrownBy(() -> boardCategoryService.validateCategoryWithBoardType(category, "notice")) + assertThatThrownBy(() -> boardCategoryService.getCategoryWithBoardType(category, "notice")) .isInstanceOf(IllegalArgumentException.class) .hasMessage("분류는 비어있을 수 없습니다."); } @@ -91,7 +91,7 @@ void validateCategoryWithBoardType() { boardCategoryRepository.save(boardCategoryEntity); //when, then - assertThatThrownBy(() -> boardCategoryService.validateCategoryWithBoardType("study", "notice")) + assertThatThrownBy(() -> boardCategoryService.getCategoryWithBoardType("study", "notice")) .isInstanceOf(EntityNotFoundException.class) .hasMessage("해당 분류가 존재하지 않습니다."); } diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardControllerTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardControllerTest.java index 57d66f00..f7871b16 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardControllerTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/controller/LearnBoardControllerTest.java @@ -29,7 +29,14 @@ class LearnBoardControllerTest extends ControllerTestSupport { @DisplayName("TIL 목록을 조회한다.") void getDailyRoutine_withoutPagination() throws Exception { //given - final Pageable pageable = PageRequest.of(0, 10, Sort.by("writtenAt", "id").descending()); + final Pageable pageable = PageRequest.of( + 0, + 10, + Sort.by( + Sort.Order.desc("writtenAt"), + Sort.Order.asc("id") + ) + ); final List learnBoardResponses = List.of( LearnBoardResponse.builder() diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntityTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntityTest.java index a1383e9e..c881e19f 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntityTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/entity/LearnBoardEntityTest.java @@ -1,5 +1,6 @@ package org.hyunggi.mygardenbe.boards.learn.entity; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -18,18 +19,18 @@ void of() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when - final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId); // then assertThat(learnBoardEntity.getTitle()).isEqualTo(title); assertThat(learnBoardEntity.getContent()).isEqualTo(content); - assertThat(learnBoardEntity.getCategory()).isEqualTo(category); + assertThat(learnBoardEntity.getCategory()).isEqualTo(boardCategoryEntity); assertThat(learnBoardEntity.getWriter()).isEqualTo(writer); assertThat(learnBoardEntity.getWrittenAt()).isEqualTo(writtenAt); assertThat(learnBoardEntity.getMemberId()).isEqualTo(memberId); @@ -41,13 +42,13 @@ void of() { void titleIsNull(final String title) { // given final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId)) + assertThatThrownBy(() -> LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("제목은 null이 될 수 없고 빈 문자열이 될 수 없습니다."); } @@ -58,34 +59,17 @@ void titleIsNull(final String title) { void contentIsNull(final String content) { // given final String title = "title"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId)) + assertThatThrownBy(() -> LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("내용은 null이 될 수 없고 빈 문자열이 될 수 없습니다."); } - @ParameterizedTest - @NullAndEmptySource - @DisplayName("category가 null 혹은 빈 문자열이면 IllegalArgumentException이 발생한다.") - void categoryIsNull(final String category) { - // given - final String title = "title"; - final String content = "content"; - final String writer = "writer"; - final LocalDateTime writtenAt = LocalDateTime.now(); - final Long memberId = 1L; - - // when, then - assertThatThrownBy(() -> LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("분류는 null이 될 수 없고 빈 문자열이 될 수 없습니다."); - } - @ParameterizedTest @NullAndEmptySource @DisplayName("writer가 null 혹은 빈 문자열이면 IllegalArgumentException이 발생한다.") @@ -93,12 +77,12 @@ void writerIsNull(final String writer) { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId)) + assertThatThrownBy(() -> LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("작성자는 null이 될 수 없고 빈 문자열이 될 수 없습니다."); } @@ -109,12 +93,12 @@ void writtenAtIsNull() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final Long memberId = 1L; // when, then - assertThatThrownBy(() -> LearnBoardEntity.of(title, content, category, writer, null, memberId)) + assertThatThrownBy(() -> LearnBoardEntity.of(title, content, boardCategoryEntity, writer, null, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("작성일은 null이 될 수 없습니다."); } @@ -126,12 +110,12 @@ void memberIdIsNull(final Long memberId) { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); // when, then - assertThatThrownBy(() -> LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId)) + assertThatThrownBy(() -> LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("멤버 아이디는 null이 될 수 없고 0보다 커야 합니다."); } @@ -142,11 +126,11 @@ void increaseViewCount() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; - final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId); // when learnBoardEntity.increaseViewCount(); @@ -161,23 +145,23 @@ void update() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; - final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of(title, content, category, writer, writtenAt, memberId); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of(title, content, boardCategoryEntity, writer, writtenAt, memberId); final String updatedTitle = "updatedTitle"; final String updatedContent = "updatedContent"; - final String updatedCategory = "updatedCategory"; + final BoardCategoryEntity newBoardCategoryEntity = new BoardCategoryEntity("updatedCategory", "신규 카테고리", "learn"); // when - learnBoardEntity.update(updatedTitle, updatedContent, updatedCategory); + learnBoardEntity.update(updatedTitle, updatedContent, newBoardCategoryEntity); // then assertThat(learnBoardEntity.getTitle()).isEqualTo(updatedTitle); assertThat(learnBoardEntity.getContent()).isEqualTo(updatedContent); - assertThat(learnBoardEntity.getCategory()).isEqualTo(updatedCategory); + assertThat(learnBoardEntity.getCategory()).isEqualTo(newBoardCategoryEntity); } @Test @@ -185,10 +169,24 @@ void update() { void isWriter() { // given final Long memberId = 1L; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); - final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of("title", "content", "category", "writer", LocalDateTime.now(), memberId); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of("title", "content", boardCategoryEntity, "writer", LocalDateTime.now(), memberId); // when, then assertThat(learnBoardEntity.isWriter(memberId)).isTrue(); } + + @Test + @DisplayName("getCategoryCode() 메서드를 통해 카테고리 코드를 가져올 수 있다.") + void getCategoryCode() { + // given + final String categoryCode = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity(categoryCode, "카테고리", "learn"); + + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of("title", "content", boardCategoryEntity, "writer", LocalDateTime.now(), 1L); + + // when, then + assertThat(learnBoardEntity.getCategoryCode()).isEqualTo(categoryCode); + } } diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryTest.java index e4bb53a8..2fa3de8c 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/repository/LearnBoardRepositoryTest.java @@ -1,6 +1,8 @@ package org.hyunggi.mygardenbe.boards.learn.repository; import org.hyunggi.mygardenbe.IntegrationTestSupport; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; +import org.hyunggi.mygardenbe.boards.common.category.repository.BoardCategoryRepository; import org.hyunggi.mygardenbe.boards.learn.entity.LearnBoardEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -18,6 +20,8 @@ class LearnBoardRepositoryTest extends IntegrationTestSupport { @Autowired LearnBoardRepository learnBoardRepository; + @Autowired + BoardCategoryRepository boardCategoryRepository; @Test @DisplayName("해당 날짜에 해당하는 TIL을 조회할 수 있다.") @@ -44,10 +48,13 @@ void searchLearnBoards_with_writtenAt() { } private LearnBoardEntity buildLearnBoardWith(final LocalDateTime writtenAt) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + return LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", writtenAt, 1L @@ -69,15 +76,18 @@ void searchLearnBoards_with_writtenAt_and_category() { //then assertThat(learnBoardEntities).hasSize(1) - .extracting("category") + .extracting("category.code") .containsExactly("category1"); } private LearnBoardEntity buildLearnBoardWith(final String title, final String content, final String category) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity(category, "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + return LearnBoardEntity.of( title, content, - category, + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), 1L diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardServiceTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardServiceTest.java index 04c68df5..7f9afb44 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardServiceTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/LearnBoardServiceTest.java @@ -90,10 +90,13 @@ void getLearnBoardsWithoutCategoryAndSearchText() { } private LearnBoardEntity buildLearnBoardWith(final String title, final String content, final String category) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity(category, "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + return LearnBoardEntity.of( title, content, - category, + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), 1L @@ -288,10 +291,13 @@ void getLearnBoardsWithNullPageable() { @DisplayName("TIL을 조회한다.") void getLearnBoard() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), 1L @@ -305,7 +311,7 @@ void getLearnBoard() { assertThat(learnBoard.getId()).isEqualTo(learnBoardEntity.getId()); assertThat(learnBoard.getTitle()).isEqualTo(learnBoardEntity.getTitle()); assertThat(learnBoard.getContent()).isEqualTo(learnBoardEntity.getContent()); - assertThat(learnBoard.getCategory()).isEqualTo(learnBoardEntity.getCategory()); + assertThat(learnBoard.getCategory()).isEqualTo(learnBoardEntity.getCategoryCode()); assertThat(learnBoard.getWriter()).isEqualTo(learnBoardEntity.getWriter()); assertThat(learnBoard.getWrittenAt()).isEqualTo(learnBoardEntity.getWrittenAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); assertThat(learnBoard.getViews()).isEqualTo(learnBoardEntity.getViews()); @@ -315,10 +321,13 @@ void getLearnBoard() { @DisplayName("TIL을 조회하면, 조회 수가 1 증가한다.") void getLearnBoardWithIncreaseViewCount() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), 1L @@ -373,7 +382,7 @@ void postLearnBoard() { assertThat(learnBoardEntity.getTitle()).isEqualTo("title"); assertThat(learnBoardEntity.getContent()).isEqualTo("content"); - assertThat(learnBoardEntity.getCategory()).isEqualTo("project"); + assertThat(learnBoardEntity.getCategoryCode()).isEqualTo("project"); assertThat(learnBoardEntity.getWriter()).isEqualTo(member.getEmail().split("@")[0]); assertThat(learnBoardEntity.getViews()).isZero(); } @@ -462,10 +471,13 @@ void postLearnBoardWithOver4000Content() { @DisplayName("TIL을 수정한다.") void putLearnBoard() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -486,7 +498,7 @@ void putLearnBoard() { assertThat(updatedLearnBoardEntity.getTitle()).isEqualTo("title2"); assertThat(updatedLearnBoardEntity.getContent()).isEqualTo("content2"); - assertThat(updatedLearnBoardEntity.getCategory()).isEqualTo("project"); + assertThat(updatedLearnBoardEntity.getCategoryCode()).isEqualTo("project"); } @Test @@ -527,10 +539,13 @@ void putLearnBoardWithNegativeBoardId() { @DisplayName("TIL을 수정할 때, PostRequest의 title이 null이면, IllegalArgumentException이 발생한다.") void putLearnBoardWithNullTitle() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -553,10 +568,13 @@ void putLearnBoardWithNullTitle() { @DisplayName("TIL을 수정할 때, PostRequest의 title이 100자를 넘으면, IllegalArgumentException이 발생한다.") void putLearnBoardWithOver100Title() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -579,10 +597,13 @@ void putLearnBoardWithOver100Title() { @DisplayName("TIL을 수정할 때, PostRequest의 content가 null이면, IllegalArgumentException이 발생한다.") void putLearnBoardWithNullContent() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -605,10 +626,13 @@ void putLearnBoardWithNullContent() { @DisplayName("TIL을 수정할 때, PostRequest의 content가 4000자를 넘으면, IllegalArgumentException이 발생한다.") void putLearnBoardWithOver4000Content() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -631,10 +655,13 @@ void putLearnBoardWithOver4000Content() { @DisplayName("TIL을 수정할 때, PostRequest의 category가 존재하지 않으면, EntityNotFoundException이 발생한다.") void putLearnBoardWithNonExistCategory() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -674,10 +701,13 @@ void putLearnBoardWithNonExistLearnBoard() { @DisplayName("TIL을 수정할 때, 작성자가 아니면, IllegalArgumentException이 발생한다.") void putLearnBoardWithNotWriter() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() @@ -700,10 +730,13 @@ void putLearnBoardWithNotWriter() { @DisplayName("TIL을 삭제한다.") void deleteLearnBoard() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + boardCategoryRepository.save(boardCategoryEntity); + final LearnBoardEntity learnBoardEntity = LearnBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), member.getId() diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponseTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponseTest.java index 30ad195f..ce79eb82 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponseTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/learn/service/response/LearnBoardResponseTest.java @@ -1,5 +1,6 @@ package org.hyunggi.mygardenbe.boards.learn.service.response; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.boards.learn.entity.LearnBoardEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,7 +13,9 @@ class LearnBoardResponseTest { @DisplayName("of 메서드를 통해서, LearnBoardResponse 객체를 생성할 수 있다.") void of() { // given - final LearnBoardEntity noticeBoardEntity = LearnBoardEntity.of("title", "content", "category", "writer", 1L); + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "learn"); + + final LearnBoardEntity noticeBoardEntity = LearnBoardEntity.of("title", "content", boardCategoryEntity, "writer", 1L); // when final LearnBoardResponse noticeBoardResponse = LearnBoardResponse.of(noticeBoardEntity); @@ -21,7 +24,7 @@ void of() { assertThat(noticeBoardResponse).isNotNull(); assertThat(noticeBoardResponse.getTitle()).isEqualTo(noticeBoardEntity.getTitle()); assertThat(noticeBoardResponse.getContent()).isEqualTo(noticeBoardEntity.getContent()); - assertThat(noticeBoardResponse.getCategory()).isEqualTo(noticeBoardEntity.getCategory()); + assertThat(noticeBoardResponse.getCategory()).isEqualTo(noticeBoardEntity.getCategoryCode()); assertThat(noticeBoardResponse.getViews()).isEqualTo(noticeBoardEntity.getViews()); assertThat(noticeBoardResponse.getWriter()).isEqualTo(noticeBoardEntity.getWriter()); assertThat(noticeBoardResponse.getWrittenAt()).isEqualTo(noticeBoardEntity.getWrittenAt().format(ofPattern("yyyy-MM-dd HH:mm:ss"))); diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardControllerTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardControllerTest.java index f95cec29..22be2d39 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardControllerTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/controller/NoticeBoardControllerTest.java @@ -67,7 +67,14 @@ void getNoticeImportantBoards() throws Exception { @DisplayName("공지사항 목록을 조회한다.") void getNoticeBoards() throws Exception { //given - final Pageable pageable = PageRequest.of(0, 10, Sort.by("writtenAt", "id").descending()); + final Pageable pageable = PageRequest.of( + 0, + 10, + Sort.by( + Sort.Order.desc("writtenAt"), + Sort.Order.asc("id") + ) + ); final List noticeBoardResponses = List.of( NoticeBoardResponse.builder() diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntityTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntityTest.java index bfbd6a8a..0e0ea58c 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntityTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/entity/NoticeBoardEntityTest.java @@ -1,5 +1,6 @@ package org.hyunggi.mygardenbe.boards.notice.entity; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -19,19 +20,19 @@ void of() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when - final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId); // then assertThat(noticeBoardEntity.getTitle()).isEqualTo(title); assertThat(noticeBoardEntity.getContent()).isEqualTo(content); - assertThat(noticeBoardEntity.getCategory()).isEqualTo(category); + assertThat(noticeBoardEntity.getCategory()).isEqualTo(boardCategoryEntity); assertThat(noticeBoardEntity.getIsImportant()).isEqualTo(isImportant); assertThat(noticeBoardEntity.getWriter()).isEqualTo(writer); assertThat(noticeBoardEntity.getWrittenAt()).isEqualTo(writtenAt); @@ -44,14 +45,14 @@ void of() { void titleIsNull(final String title) { // given final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId)) + assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("제목은 null이 될 수 없고 빈 문자열이 될 수 없습니다."); } @@ -62,49 +63,31 @@ void titleIsNull(final String title) { void contentIsNull(final String content) { // given final String title = "title"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId)) + assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("내용은 null이 될 수 없고 빈 문자열이 될 수 없습니다."); } - @ParameterizedTest - @NullAndEmptySource - @DisplayName("category가 null 혹은 빈 문자열이면 IllegalArgumentException이 발생한다.") - void categoryIsNull(final String category) { - // given - final String title = "title"; - final String content = "content"; - final Boolean isImportant = false; - final String writer = "writer"; - final LocalDateTime writtenAt = LocalDateTime.now(); - final Long memberId = 1L; - - // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("분류는 null이 될 수 없고 빈 문자열이 될 수 없습니다."); - } - @Test @DisplayName("isImportant가 null이면 IllegalArgumentException이 발생한다.") void isImportantIsNull() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, null, writer, writtenAt, memberId)) + assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, boardCategoryEntity, null, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("알림글 여부는 null이 될 수 없습니다."); } @@ -116,13 +99,13 @@ void writerIsNull(final String writer) { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId)) + assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("작성자는 null이 될 수 없고 빈 문자열이 될 수 없습니다."); } @@ -133,13 +116,13 @@ void writtenAtIsNull() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final Long memberId = 1L; // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, isImportant, writer, null, memberId)) + assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, null, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("작성일은 null이 될 수 없습니다."); } @@ -151,13 +134,13 @@ void memberIdIsNull(final Long memberId) { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); // when, then - assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId)) + assertThatThrownBy(() -> NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("멤버 아이디는 null이 될 수 없고 0보다 커야 합니다."); } @@ -168,12 +151,12 @@ void setImportant() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; - final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId); // when noticeBoardEntity.setImportant(); @@ -188,12 +171,12 @@ void increaseViewCount() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; - final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId); // when noticeBoardEntity.increaseViewCount(); @@ -208,25 +191,25 @@ void update() { // given final String title = "title"; final String content = "content"; - final String category = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); final Boolean isImportant = false; final String writer = "writer"; final LocalDateTime writtenAt = LocalDateTime.now(); final Long memberId = 1L; - final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, category, isImportant, writer, writtenAt, memberId); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of(title, content, boardCategoryEntity, isImportant, writer, writtenAt, memberId); final String updatedTitle = "updatedTitle"; final String updatedContent = "updatedContent"; - final String updatedCategory = "updatedCategory"; + final BoardCategoryEntity newBoardCategoryEntity = new BoardCategoryEntity("updatedCategory", "신규 카테고리", "notice"); final Boolean updatedIsImportant = true; // when - noticeBoardEntity.update(updatedTitle, updatedContent, updatedCategory, updatedIsImportant); + noticeBoardEntity.update(updatedTitle, updatedContent, newBoardCategoryEntity, updatedIsImportant); // then assertThat(noticeBoardEntity.getTitle()).isEqualTo(updatedTitle); assertThat(noticeBoardEntity.getContent()).isEqualTo(updatedContent); - assertThat(noticeBoardEntity.getCategory()).isEqualTo(updatedCategory); + assertThat(noticeBoardEntity.getCategory()).isEqualTo(newBoardCategoryEntity); assertThat(noticeBoardEntity.getIsImportant()).isEqualTo(updatedIsImportant); } @@ -235,10 +218,27 @@ void update() { void isWriter() { // given final Long memberId = 1L; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); - final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of("title", "content", "category", false, "writer", LocalDateTime.now(), memberId); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of("title", "content", boardCategoryEntity, false, "writer", LocalDateTime.now(), memberId); // when, then assertThat(noticeBoardEntity.isWriter(memberId)).isTrue(); } + + @Test + @DisplayName("getCategoryCode() 메서드를 통해 카테고리 코드를 가져올 수 있다.") + void getCategoryCode() { + // given + final String categoryCode = "category"; + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity(categoryCode, "카테고리", "notice"); + + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of("title", "content", boardCategoryEntity, false, "writer", LocalDateTime.now(), 1L); + + // when + final String result = noticeBoardEntity.getCategoryCode(); + + // then + assertThat(result).isEqualTo(categoryCode); + } } diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryTest.java index 2a508040..b956729b 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/repository/NoticeBoardRepositoryTest.java @@ -1,6 +1,8 @@ package org.hyunggi.mygardenbe.boards.notice.repository; import org.hyunggi.mygardenbe.IntegrationTestSupport; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; +import org.hyunggi.mygardenbe.boards.common.category.repository.BoardCategoryRepository; import org.hyunggi.mygardenbe.boards.notice.entity.NoticeBoardEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -18,6 +20,8 @@ class NoticeBoardRepositoryTest extends IntegrationTestSupport { @Autowired NoticeBoardRepository noticeBoardRepository; + @Autowired + BoardCategoryRepository boardCategoryRepository; @Test @DisplayName("중요 공지사항을 조회할 수 있다.") @@ -40,10 +44,13 @@ void findAllByIsImportant() { } private NoticeBoardEntity buildNoticeImportantBoardWith(final String title, final String content, final int day, final boolean isImportant) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + return NoticeBoardEntity.of( title, content, - "category", + boardCategoryEntity, isImportant, "writer", LocalDateTime.of(2024, 3, day, 12, 0, 0), @@ -76,10 +83,13 @@ void searchNoticeBoards_with_writtenAt() { } private NoticeBoardEntity buildNoticeBoardWith(final LocalDateTime writtenAt) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + return NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, false, "writer", writtenAt, @@ -102,15 +112,18 @@ void searchNoticeBoards_with_writtenAt_and_category() { //then assertThat(noticeBoardEntities).hasSize(1) - .extracting("category") + .extracting("category.code") .containsExactly("category1"); } private NoticeBoardEntity buildNoticeBoardWith(final String title, final String content, final String category) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity(category, "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + return NoticeBoardEntity.of( title, content, - category, + boardCategoryEntity, false, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardServiceTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardServiceTest.java index 95871ac5..638101e7 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardServiceTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/NoticeBoardServiceTest.java @@ -110,10 +110,13 @@ void getNoticeBoardsWithoutCategoryAndSearchText() { } private NoticeBoardEntity buildNoticeBoardWith(final String title, final String content, final String category, final boolean isImportant) { + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity(category, "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + return NoticeBoardEntity.of( title, content, - category, + boardCategoryEntity, isImportant, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -309,10 +312,13 @@ void getNoticeBoardsWithNullPageable() { @DisplayName("공지사항을 조회한다.") void getNoticeBoard() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, false, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -327,7 +333,7 @@ void getNoticeBoard() { assertThat(noticeBoard.getId()).isEqualTo(noticeBoardEntity.getId()); assertThat(noticeBoard.getTitle()).isEqualTo(noticeBoardEntity.getTitle()); assertThat(noticeBoard.getContent()).isEqualTo(noticeBoardEntity.getContent()); - assertThat(noticeBoard.getCategory()).isEqualTo(noticeBoardEntity.getCategory()); + assertThat(noticeBoard.getCategory()).isEqualTo(noticeBoardEntity.getCategoryCode()); assertThat(noticeBoard.getIsImportant()).isEqualTo(noticeBoardEntity.getIsImportant()); assertThat(noticeBoard.getWriter()).isEqualTo(noticeBoardEntity.getWriter()); assertThat(noticeBoard.getWrittenAt()).isEqualTo(noticeBoardEntity.getWrittenAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); @@ -338,10 +344,13 @@ void getNoticeBoard() { @DisplayName("공지사항을 조회하면, 조회 수가 1 증가한다.") void getNoticeBoardWithIncreaseViewCount() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -398,7 +407,7 @@ void postNoticeBoard() { assertThat(noticeBoardEntity.getTitle()).isEqualTo("title"); assertThat(noticeBoardEntity.getContent()).isEqualTo("content"); - assertThat(noticeBoardEntity.getCategory()).isEqualTo("project"); + assertThat(noticeBoardEntity.getCategoryCode()).isEqualTo("project"); assertThat(noticeBoardEntity.getIsImportant()).isEqualTo((Boolean) true); assertThat(noticeBoardEntity.getWriter()).isEqualTo(member.getEmail().split("@")[0]); assertThat(noticeBoardEntity.getViews()).isZero(); @@ -493,10 +502,13 @@ void postNoticeBoardWithNonExistCategory() { @DisplayName("공지사항을 수정한다.") void putNoticeBoard() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -519,7 +531,7 @@ void putNoticeBoard() { assertThat(updatedNoticeBoardEntity.getTitle()).isEqualTo("title2"); assertThat(updatedNoticeBoardEntity.getContent()).isEqualTo("content2"); - assertThat(updatedNoticeBoardEntity.getCategory()).isEqualTo("project"); + assertThat(updatedNoticeBoardEntity.getCategoryCode()).isEqualTo("project"); assertThat(updatedNoticeBoardEntity.getIsImportant()).isEqualTo((Boolean) false); } @@ -563,10 +575,13 @@ void putNoticeBoardWithNegativeBoardId() { @DisplayName("공지사항을 수정할 때, PostRequest의 title이 null이면, IllegalArgumentException이 발생한다.") void putNoticeBoardWithNullTitle() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -591,10 +606,13 @@ void putNoticeBoardWithNullTitle() { @DisplayName("공지사항을 수정할 때, PostRequest의 title이 100자를 초과하면, IllegalArgumentException이 발생한다.") void putNoticeBoardWithOver100Title() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -619,10 +637,13 @@ void putNoticeBoardWithOver100Title() { @DisplayName("공지사항을 수정할 때, PostRequest의 content가 null이면, IllegalArgumentException이 발생한다.") void putNoticeBoardWithNullContent() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -647,10 +668,13 @@ void putNoticeBoardWithNullContent() { @DisplayName("공지사항을 수정할 때, PostRequest의 content가 4000자를 초과하면, IllegalArgumentException이 발생한다.") void putNoticeBoardWithOver4000Content() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -675,10 +699,13 @@ void putNoticeBoardWithOver4000Content() { @DisplayName("공지사항을 수정할 때, PostRequest의 category가 존재하지 않으면, EntityNotFoundException이 발생한다.") void putNoticeBoardWithNonExistCategory() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -721,10 +748,13 @@ void putNoticeBoardWithNonExistNoticeBoard() { @DisplayName("공지사항을 수정할 때, 작성자가 아니면, IllegalArgumentException이 발생한다.") void putNoticeBoardWithNotWriter() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), @@ -749,10 +779,13 @@ void putNoticeBoardWithNotWriter() { @DisplayName("공지사항을 삭제한다.") void deleteNoticeBoard() { // given + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + boardCategoryRepository.save(boardCategoryEntity); + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of( "title", "content", - "category", + boardCategoryEntity, true, "writer", LocalDateTime.of(2024, 1, 27, 12, 0, 0), diff --git a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponseTest.java b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponseTest.java index 4bc3307a..4d41de42 100644 --- a/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponseTest.java +++ b/my-garden-be/src/test/java/org/hyunggi/mygardenbe/boards/notice/service/response/NoticeBoardResponseTest.java @@ -1,5 +1,6 @@ package org.hyunggi.mygardenbe.boards.notice.service.response; +import org.hyunggi.mygardenbe.boards.common.category.entity.BoardCategoryEntity; import org.hyunggi.mygardenbe.boards.notice.entity.NoticeBoardEntity; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -13,7 +14,9 @@ class NoticeBoardResponseTest { @DisplayName("of 메서드를 통해서, NoticeBoardResponse 객체를 생성할 수 있다.") void of() { // given - final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of("title", "content", "category", "writer", 1L); + final BoardCategoryEntity boardCategoryEntity = new BoardCategoryEntity("category", "카테고리", "notice"); + + final NoticeBoardEntity noticeBoardEntity = NoticeBoardEntity.of("title", "content", boardCategoryEntity, "writer", 1L); // when final NoticeBoardResponse noticeBoardResponse = NoticeBoardResponse.of(noticeBoardEntity); @@ -22,7 +25,7 @@ void of() { assertThat(noticeBoardResponse).isNotNull(); assertThat(noticeBoardResponse.getTitle()).isEqualTo(noticeBoardEntity.getTitle()); assertThat(noticeBoardResponse.getContent()).isEqualTo(noticeBoardEntity.getContent()); - assertThat(noticeBoardResponse.getCategory()).isEqualTo(noticeBoardEntity.getCategory()); + assertThat(noticeBoardResponse.getCategory()).isEqualTo(noticeBoardEntity.getCategoryCode()); assertThat(noticeBoardResponse.getIsImportant()).isEqualTo(noticeBoardEntity.getIsImportant()); assertThat(noticeBoardResponse.getViews()).isEqualTo(noticeBoardEntity.getViews()); assertThat(noticeBoardResponse.getWriter()).isEqualTo(noticeBoardEntity.getWriter());