From c00f54d007a7c70e34d3fb4f2b57dae1498f090e Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Tue, 2 Jul 2024 00:38:55 +0900 Subject: [PATCH] =?UTF-8?q?[Feat/#91]=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20TYPE=5FCD=20=EC=A0=95=EC=9D=98=20=EB=B0=8F=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20(#141)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: CategoryType 추가 * refactor: CategoryType 반영 --- .../few/api/repo/dao/workbook/WorkbookDao.kt | 3 +- .../workbook/record/SelectWorkBookRecord.kt | 2 +- .../api/repo/dao/article/ArticleDaoTest.kt | 11 +++---- .../api/repo/dao/workbook/WorkbookDaoTest.kt | 3 +- .../document/usecase/AddArticleUseCase.kt | 3 +- .../article/usecase/ReadArticleUseCase.kt | 3 +- .../usecase/ReadWorkBookArticleUseCase.kt | 3 +- .../workbook/usecase/ReadWorkbookUseCase.kt | 3 +- .../controller/admin/AdminControllerTest.kt | 7 +++-- .../article/ArticleControllerTest.kt | 3 +- .../workbook/WorkBookControllerTest.kt | 3 +- .../article/WorkBookArticleControllerTest.kt | 3 +- .../com/few/data/common/code/CategoryType.kt | 29 +++++++++++++++++++ 13 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 data/src/main/kotlin/com/few/data/common/code/CategoryType.kt diff --git a/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/WorkbookDao.kt b/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/WorkbookDao.kt index 6f9aa244a..9ffb842e8 100644 --- a/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/WorkbookDao.kt +++ b/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/WorkbookDao.kt @@ -4,6 +4,7 @@ import com.few.api.repo.dao.workbook.command.InsertWorkBookCommand import com.few.api.repo.dao.workbook.command.MapWorkBookToArticleCommand import com.few.api.repo.dao.workbook.query.SelectWorkBookRecordQuery import com.few.api.repo.dao.workbook.record.SelectWorkBookRecord +import com.few.data.common.code.CategoryType import jooq.jooq_dsl.tables.MappingWorkbookArticle import jooq.jooq_dsl.tables.Workbook import org.jooq.DSLContext @@ -32,7 +33,7 @@ class WorkbookDao( return dslContext.insertInto(Workbook.WORKBOOK) .set(Workbook.WORKBOOK.TITLE, command.title) .set(Workbook.WORKBOOK.MAIN_IMAGE_URL, command.mainImageUrl.toString()) - .set(Workbook.WORKBOOK.CATEGORY_CD, 0) // todo fix + .set(Workbook.WORKBOOK.CATEGORY_CD, CategoryType.convertToCode(command.category)) .set(Workbook.WORKBOOK.DESCRIPTION, command.description) .returning(Workbook.WORKBOOK.ID) .fetchOne() diff --git a/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/record/SelectWorkBookRecord.kt b/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/record/SelectWorkBookRecord.kt index 56962bb9b..a7970a160 100644 --- a/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/record/SelectWorkBookRecord.kt +++ b/api-repo/src/main/kotlin/com/few/api/repo/dao/workbook/record/SelectWorkBookRecord.kt @@ -7,7 +7,7 @@ data class SelectWorkBookRecord( val id: Long, val title: String, val mainImageUrl: URL, - val category: Long, + val category: Byte, val description: String, val createdAt: LocalDateTime ) \ No newline at end of file diff --git a/api-repo/src/test/kotlin/com/few/api/repo/dao/article/ArticleDaoTest.kt b/api-repo/src/test/kotlin/com/few/api/repo/dao/article/ArticleDaoTest.kt index 72b78e483..98b9a9361 100644 --- a/api-repo/src/test/kotlin/com/few/api/repo/dao/article/ArticleDaoTest.kt +++ b/api-repo/src/test/kotlin/com/few/api/repo/dao/article/ArticleDaoTest.kt @@ -4,6 +4,7 @@ import com.few.api.repo.dao.article.query.SelectArticleRecordQuery import com.few.api.repo.dao.article.query.SelectWorkBookArticleRecordQuery import com.few.api.repo.dao.article.query.SelectWorkbookMappedArticleRecordsQuery import com.few.api.repo.jooq.JooqTestSpec +import com.few.data.common.code.CategoryType import jooq.jooq_dsl.tables.ArticleIfo import jooq.jooq_dsl.tables.ArticleMst import jooq.jooq_dsl.tables.MappingWorkbookArticle @@ -36,7 +37,7 @@ class ArticleDaoTest : JooqTestSpec() { .set(ArticleMst.ARTICLE_MST.MEMBER_ID, 1L) .set(ArticleMst.ARTICLE_MST.MAIN_IMAGE_URL, "http://localhost:8080/image1.jpg") .set(ArticleMst.ARTICLE_MST.TITLE, "this is title1") - .set(ArticleMst.ARTICLE_MST.CATEGORY_CD, 0) + .set(ArticleMst.ARTICLE_MST.CATEGORY_CD, CategoryType.fromCode(0)!!.code) .execute() dslContext.insertInto(ArticleIfo.ARTICLE_IFO) .set(ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID, 1L) @@ -63,7 +64,7 @@ class ArticleDaoTest : JooqTestSpec() { assertEquals(URL("http://localhost:8080/image1.jpg"), result.mainImageURL) assertEquals("this is title1", result.title) assertEquals("this is content1", result.content) - assertEquals(0, result.category) + assertEquals(CategoryType.fromCode(0)!!.code, result.category) } @Test @@ -85,7 +86,7 @@ class ArticleDaoTest : JooqTestSpec() { assertEquals(URL("http://localhost:8080/image1.jpg"), result.mainImageURL) assertEquals("this is title1", result.title) assertEquals("this is content1", result.content) - assertEquals(0, result.category) + assertEquals(CategoryType.fromCode(0)!!.code, result.category) assertEquals(1L, result.day) } @@ -111,7 +112,7 @@ class ArticleDaoTest : JooqTestSpec() { assertEquals(URL("http://localhost:8080/image${i + 1}.jpg"), result[i].mainImageURL) assertEquals("this is title${i + 1}", result[i].title) assertEquals("this is content${i + 1}", result[i].content) - assertEquals(0, result[i].category) // todo fix + assertEquals(CategoryType.fromCode(0)!!.code, result[i].category) } } @@ -142,7 +143,7 @@ class ArticleDaoTest : JooqTestSpec() { .set(ArticleMst.ARTICLE_MST.MEMBER_ID, 1L) .set(ArticleMst.ARTICLE_MST.MAIN_IMAGE_URL, "http://localhost:8080/image$id.jpg") .set(ArticleMst.ARTICLE_MST.TITLE, "this is title$id") - .set(ArticleMst.ARTICLE_MST.CATEGORY_CD, 0) + .set(ArticleMst.ARTICLE_MST.CATEGORY_CD, CategoryType.fromCode(0)!!.code) .execute() } diff --git a/api-repo/src/test/kotlin/com/few/api/repo/dao/workbook/WorkbookDaoTest.kt b/api-repo/src/test/kotlin/com/few/api/repo/dao/workbook/WorkbookDaoTest.kt index a1ac6c8fa..749a948be 100644 --- a/api-repo/src/test/kotlin/com/few/api/repo/dao/workbook/WorkbookDaoTest.kt +++ b/api-repo/src/test/kotlin/com/few/api/repo/dao/workbook/WorkbookDaoTest.kt @@ -2,6 +2,7 @@ package com.few.api.repo.dao.workbook import com.few.api.repo.dao.workbook.query.SelectWorkBookRecordQuery import com.few.api.repo.jooq.JooqTestSpec +import com.few.data.common.code.CategoryType import jooq.jooq_dsl.tables.Workbook import org.jooq.DSLContext import org.junit.jupiter.api.Assertions.* @@ -25,7 +26,7 @@ class WorkbookDaoTest : JooqTestSpec() { .set(Workbook.WORKBOOK.ID, 1) .set(Workbook.WORKBOOK.TITLE, "title1") .set(Workbook.WORKBOOK.MAIN_IMAGE_URL, "http://localhost:8080/image1.jpg") - .set(Workbook.WORKBOOK.CATEGORY_CD, 0) // todo fix + .set(Workbook.WORKBOOK.CATEGORY_CD, CategoryType.fromCode(0)!!.code) .set(Workbook.WORKBOOK.DESCRIPTION, "description1") .execute() } diff --git a/api/src/main/kotlin/com/few/api/domain/admin/document/usecase/AddArticleUseCase.kt b/api/src/main/kotlin/com/few/api/domain/admin/document/usecase/AddArticleUseCase.kt index 9e631dfd3..2517e0a61 100644 --- a/api/src/main/kotlin/com/few/api/domain/admin/document/usecase/AddArticleUseCase.kt +++ b/api/src/main/kotlin/com/few/api/domain/admin/document/usecase/AddArticleUseCase.kt @@ -10,6 +10,7 @@ import com.few.api.repo.dao.problem.ProblemDao import com.few.api.repo.dao.problem.command.InsertProblemsCommand import com.few.api.repo.dao.problem.support.Content import com.few.api.repo.dao.problem.support.Contents +import com.few.data.common.code.CategoryType import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional @@ -31,7 +32,7 @@ class AddArticleUseCase( writerId = writerId.memberId, mainImageURL = useCaseIn.articleImageUrl, title = useCaseIn.title, - category = 0, // todo fix + category = CategoryType.convertToCode(useCaseIn.category), content = useCaseIn.contentSource ).let { articleDao.insertFullArticleRecord(it) } diff --git a/api/src/main/kotlin/com/few/api/domain/article/usecase/ReadArticleUseCase.kt b/api/src/main/kotlin/com/few/api/domain/article/usecase/ReadArticleUseCase.kt index 18dcab29f..c880f33b1 100644 --- a/api/src/main/kotlin/com/few/api/domain/article/usecase/ReadArticleUseCase.kt +++ b/api/src/main/kotlin/com/few/api/domain/article/usecase/ReadArticleUseCase.kt @@ -9,6 +9,7 @@ import com.few.api.domain.article.service.dto.BrowseArticleProblemIdsQuery import com.few.api.domain.article.service.dto.ReadWriterRecordQuery import com.few.api.repo.dao.article.ArticleDao import com.few.api.repo.dao.article.query.SelectArticleRecordQuery +import com.few.data.common.code.CategoryType import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional @@ -43,7 +44,7 @@ class ReadArticleUseCase( title = articleRecord.title, content = articleRecord.content, problemIds = problemIds.problemIds, - category = articleRecord.category.toString(), // todo fix to enum + category = CategoryType.convertToDisplayName(articleRecord.category), createdAt = articleRecord.createdAt ) } diff --git a/api/src/main/kotlin/com/few/api/domain/workbook/article/usecase/ReadWorkBookArticleUseCase.kt b/api/src/main/kotlin/com/few/api/domain/workbook/article/usecase/ReadWorkBookArticleUseCase.kt index 079c91357..2853befb5 100644 --- a/api/src/main/kotlin/com/few/api/domain/workbook/article/usecase/ReadWorkBookArticleUseCase.kt +++ b/api/src/main/kotlin/com/few/api/domain/workbook/article/usecase/ReadWorkBookArticleUseCase.kt @@ -9,6 +9,7 @@ import com.few.api.domain.workbook.article.dto.ReadWorkBookArticleOut import com.few.api.domain.workbook.article.dto.WriterDetail import com.few.api.repo.dao.article.ArticleDao import com.few.api.repo.dao.article.query.SelectWorkBookArticleRecordQuery +import com.few.data.common.code.CategoryType import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -47,7 +48,7 @@ class ReadWorkBookArticleUseCase( title = articleRecord.title, content = articleRecord.content, problemIds = problemIds.problemIds, - category = articleRecord.category.toString(), // todo fix to enum + category = CategoryType.convertToDisplayName(articleRecord.category), createdAt = articleRecord.createdAt, day = articleRecord.day ) diff --git a/api/src/main/kotlin/com/few/api/domain/workbook/usecase/ReadWorkbookUseCase.kt b/api/src/main/kotlin/com/few/api/domain/workbook/usecase/ReadWorkbookUseCase.kt index 83c8f8613..124004892 100644 --- a/api/src/main/kotlin/com/few/api/domain/workbook/usecase/ReadWorkbookUseCase.kt +++ b/api/src/main/kotlin/com/few/api/domain/workbook/usecase/ReadWorkbookUseCase.kt @@ -7,6 +7,7 @@ import com.few.api.domain.workbook.service.dto.BrowseWorkbookArticlesQuery import com.few.api.domain.workbook.service.dto.BrowseWriterRecordsQuery import com.few.api.repo.dao.workbook.WorkbookDao import com.few.api.repo.dao.workbook.query.SelectWorkBookRecordQuery +import com.few.data.common.code.CategoryType import org.springframework.stereotype.Component @Component @@ -36,7 +37,7 @@ class ReadWorkbookUseCase( mainImageUrl = workbookRecord.mainImageUrl, title = workbookRecord.title, description = workbookRecord.description, - category = workbookRecord.category.toString(), // todo fix enum to string + category = CategoryType.convertToDisplayName(workbookRecord.category), createdAt = workbookRecord.createdAt, writers = writerRecords.toWriterDetails(), articles = workbookMappedArticles.toArticleDetails() diff --git a/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt index 370aea9d7..c4fbf3868 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt @@ -12,6 +12,7 @@ import com.few.api.web.controller.admin.request.* import com.few.api.web.controller.admin.response.ImageSourceResponse import com.few.api.web.controller.description.Description import com.few.api.web.controller.helper.* +import com.few.data.common.code.CategoryType import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test @@ -88,7 +89,7 @@ class AdminControllerTest : ControllerTestSpec() { val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/workbooks").build().toUriString() val title = "title" val mainImageUrl = URL("http://localhost:8080") - val category = "category" + val category = CategoryType.fromCode(0)!!.name val description = "description" val request = AddWorkbookRequest(title, mainImageUrl, category, description) val body = objectMapper.writeValueAsString(request) @@ -135,7 +136,7 @@ class AdminControllerTest : ControllerTestSpec() { "writer@gmail.com", URL("http://localhost:8080"), "title", - "category", + CategoryType.fromCode(0)!!.name, "contentSource", ProblemDto( "title", @@ -157,7 +158,7 @@ class AdminControllerTest : ControllerTestSpec() { "writer@gmail.com", URL("http://localhost:8080"), "title", - "category", + CategoryType.fromCode(0)!!.name, "contentSource", ProblemDetail( "title", diff --git a/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt index d134e430e..fd7f99699 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt @@ -12,6 +12,7 @@ import com.few.api.domain.article.usecase.ReadArticleUseCase import com.few.api.web.controller.ControllerTestSpec import com.few.api.web.controller.description.Description import com.few.api.web.controller.helper.* +import com.few.data.common.code.CategoryType import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test @@ -77,7 +78,7 @@ class ArticleControllerTest : ControllerTestSpec() { url = URL("http://localhost:8080/api/v1/writers/1") ), title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", - content = "content", + content = CategoryType.fromCode(0)!!.name, problemIds = listOf(1L, 2L, 3L), category = "경제", createdAt = LocalDateTime.now() diff --git a/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt index 54e5e5008..0942f75af 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt @@ -13,6 +13,7 @@ import com.few.api.domain.workbook.usecase.ReadWorkbookUseCase import com.few.api.web.controller.ControllerTestSpec import com.few.api.web.controller.description.Description import com.few.api.web.controller.helper.* +import com.few.data.common.code.CategoryType import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName @@ -79,7 +80,7 @@ class WorkBookControllerTest : ControllerTestSpec() { mainImageUrl = URL("http://localhost:8080/api/v1/workbooks/1/image"), title = "재태크, 투자 필수 용어 모음집", description = "사회 초년생부터, 직장인, 은퇴자까지 모두가 알아야 할 기본적인 재태크, 투자 필수 용어 모음집 입니다.", - category = "경제", + category = CategoryType.fromCode(0)!!.name, createdAt = LocalDateTime.now(), writers = listOf( WriterDetail(1L, "안나포", URL("http://localhost:8080/api/v1/users/1")), diff --git a/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt index f728e593c..4b6ef37e8 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt @@ -12,6 +12,7 @@ import com.few.api.domain.workbook.article.usecase.ReadWorkBookArticleUseCase import com.few.api.web.controller.ControllerTestSpec import com.few.api.web.controller.description.Description import com.few.api.web.controller.helper.* +import com.few.data.common.code.CategoryType import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test @@ -83,7 +84,7 @@ class WorkBookArticleControllerTest : ControllerTestSpec() { title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", content = "content", problemIds = listOf(1L, 2L, 3L), - category = "경제", + category = CategoryType.fromCode(0)!!.name, createdAt = LocalDateTime.now(), day = 1L ) diff --git a/data/src/main/kotlin/com/few/data/common/code/CategoryType.kt b/data/src/main/kotlin/com/few/data/common/code/CategoryType.kt new file mode 100644 index 000000000..e9b029b46 --- /dev/null +++ b/data/src/main/kotlin/com/few/data/common/code/CategoryType.kt @@ -0,0 +1,29 @@ +package com.few.data.common.code + +enum class CategoryType(val code: Byte, val displayName: String) { + POLITICS(0, "정치"), + ECONOMY(10, "경제"), + SOCIETY(20, "사회"), + CULTURE(30, "문화"), + LIFE(40, "생활"), + IT(50, "IT"), + SCIENCE(60, "과학"), + ENTERTAINMENTS(70, "엔터테인먼트"), + SPORTS(80, "스포츠"), + GLOBAL(90, "국제"), + ETC(100, "기타"); + + companion object { + fun fromCode(code: Byte): CategoryType? { + return entries.find { it.code == code } + } + + fun convertToCode(displayName: String): Byte { + return entries.find { it.name == displayName }?.code ?: throw IllegalArgumentException("Invalid category name") + } + + fun convertToDisplayName(code: Byte): String { + return entries.find { it.code == code }?.displayName ?: throw IllegalArgumentException("Invalid category code") + } + } +} \ No newline at end of file