Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#55 feat: GET /api/v1/workbooks/{workbookId} 구현 #101

Merged
merged 16 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.few.api.repo.dao.article

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.dao.article.record.SelectArticleRecord
import com.few.api.repo.dao.article.record.SelectWorkBookArticleRecord
import com.few.api.repo.dao.article.record.SelectWorkBookMappedArticleRecord
import jooq.jooq_dsl.tables.ArticleIfo
import jooq.jooq_dsl.tables.ArticleMst
import jooq.jooq_dsl.tables.MappingWorkbookArticle
Expand Down Expand Up @@ -62,4 +64,27 @@ class ArticleDao(
.fetchOneInto(SelectWorkBookArticleRecord::class.java)
?: throw IllegalArgumentException("cannot find $workbookId article record by articleId: $articleId")
}

fun selectWorkbookMappedArticleRecords(query: SelectWorkbookMappedArticleRecordsQuery): List<SelectWorkBookMappedArticleRecord> {
val workbookId = query.workbookId

return dslContext.select(
ArticleMst.ARTICLE_MST.ID.`as`(SelectWorkBookArticleRecord::articleId.name),
ArticleMst.ARTICLE_MST.MEMBER_ID.`as`(SelectWorkBookArticleRecord::writerId.name),
ArticleMst.ARTICLE_MST.MAIN_IMAGE_URL.`as`(SelectWorkBookArticleRecord::mainImageURL.name),
ArticleMst.ARTICLE_MST.TITLE.`as`(SelectWorkBookArticleRecord::title.name),
ArticleMst.ARTICLE_MST.CATEGORY_CD.`as`(SelectWorkBookArticleRecord::category.name),
ArticleIfo.ARTICLE_IFO.CONTENT.`as`(SelectWorkBookArticleRecord::content.name),
ArticleMst.ARTICLE_MST.CREATED_AT.`as`(SelectWorkBookArticleRecord::createdAt.name),
MappingWorkbookArticle.MAPPING_WORKBOOK_ARTICLE.DAY_COL.`as`(SelectWorkBookArticleRecord::day.name)
)
.from(MappingWorkbookArticle.MAPPING_WORKBOOK_ARTICLE)
.leftJoin(ArticleMst.ARTICLE_MST)
.on(MappingWorkbookArticle.MAPPING_WORKBOOK_ARTICLE.ARTICLE_ID.eq(ArticleMst.ARTICLE_MST.ID))
.join(ArticleIfo.ARTICLE_IFO)
.on(ArticleMst.ARTICLE_MST.ID.eq(ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID))
.where(MappingWorkbookArticle.MAPPING_WORKBOOK_ARTICLE.WORKBOOK_ID.eq(workbookId))
.and(ArticleMst.ARTICLE_MST.DELETED_AT.isNull)
.fetchInto(SelectWorkBookMappedArticleRecord::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.repo.dao.article.query

data class SelectWorkbookMappedArticleRecordsQuery(
val workbookId: Long
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.few.api.repo.dao.article.record

import java.net.URL
import java.time.LocalDateTime

data class SelectArticleRecord(
val articleId: Long,
val writerId: Long,
val mainImageURL: String,
val mainImageURL: URL,
val title: String,
val category: Byte,
val content: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.few.api.repo.dao.article.record

import java.net.URL
import java.time.LocalDateTime

data class SelectWorkBookArticleRecord(
val articleId: Long,
val writerId: Long,
val mainImageURL: String,
val mainImageURL: URL,
val title: String,
val category: Byte,
val content: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.few.api.repo.dao.article.record

import java.net.URL
import java.time.LocalDateTime

data class SelectWorkBookMappedArticleRecord(
val articleId: Long,
val writerId: Long,
val mainImageURL: URL,
val title: String,
val category: Byte,
val content: String,
val createdAt: LocalDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.few.api.repo.dao.member
import com.few.api.repo.dao.member.command.InsertMemberCommand
import com.few.api.repo.dao.member.query.SelectMemberByEmailQuery
import com.few.api.repo.dao.member.query.SelectWriterQuery
import com.few.api.repo.dao.member.query.SelectWritersQuery
import com.few.api.repo.dao.member.record.MemberRecord
import com.few.api.repo.dao.member.record.WriterRecord
import com.few.data.common.code.MemberType
import jooq.jooq_dsl.tables.Member
import org.jooq.DSLContext
import org.jooq.impl.DSL
Expand All @@ -25,12 +27,26 @@ class MemberDao(
)
.from(Member.MEMBER)
.where(Member.MEMBER.ID.eq(writerId))
.and(Member.MEMBER.TYPE_CD.eq(1)) // todo fix after considering the type_cd
.and(Member.MEMBER.TYPE_CD.eq(MemberType.WRITER.code))
.and(Member.MEMBER.DELETED_AT.isNull)
.fetchOneInto(WriterRecord::class.java)
?: throw IllegalArgumentException("cannot find writer record by writerId: $writerId")
}

fun selectWriters(query: SelectWritersQuery): List<WriterRecord> {
return dslContext.select(
Member.MEMBER.ID.`as`(WriterRecord::writerId.name),
DSL.jsonGetAttributeAsText(Member.MEMBER.DESCRIPTION, "name").`as`(WriterRecord::name.name),
DSL.jsonGetAttribute(Member.MEMBER.DESCRIPTION, "url").`as`(WriterRecord::url.name)
)
.from(Member.MEMBER)
.where(Member.MEMBER.ID.`in`(query.writerIds))
.and(Member.MEMBER.TYPE_CD.eq(MemberType.WRITER.code))
.and(Member.MEMBER.DELETED_AT.isNull)
.orderBy(Member.MEMBER.ID.asc())
.fetchInto(WriterRecord::class.java)
}

fun selectMemberByEmail(query: SelectMemberByEmailQuery): MemberRecord {
val email = query.email

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.repo.dao.member.query

data class SelectWritersQuery(
val writerIds: List<Long>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.few.api.repo.dao.workbook

import com.few.api.repo.dao.workbook.query.SelectWorkBookRecordQuery
import com.few.api.repo.dao.workbook.record.SelectWorkBookRecord
import jooq.jooq_dsl.tables.Workbook
import org.jooq.DSLContext
import org.springframework.stereotype.Repository

@Repository
class WorkbookDao(
private val dslContext: DSLContext
) {
fun selectWorkBook(query: SelectWorkBookRecordQuery): SelectWorkBookRecord {
return dslContext.select(
Workbook.WORKBOOK.ID.`as`(SelectWorkBookRecord::id.name),
Workbook.WORKBOOK.TITLE.`as`(SelectWorkBookRecord::title.name),
Workbook.WORKBOOK.MAIN_IMAGE_URL.`as`(SelectWorkBookRecord::mainImageUrl.name),
Workbook.WORKBOOK.CATEGORY_CD.`as`(SelectWorkBookRecord::category.name),
Workbook.WORKBOOK.DESCRIPTION.`as`(SelectWorkBookRecord::description.name),
Workbook.WORKBOOK.CREATED_AT.`as`(SelectWorkBookRecord::createdAt.name)
)
.from(Workbook.WORKBOOK)
.where(Workbook.WORKBOOK.ID.eq(query.id))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete_at 필드 체크 필요할듯

.and(Workbook.WORKBOOK.DELETED_AT.isNull)
.fetchOneInto(SelectWorkBookRecord::class.java)
?: throw RuntimeException("WorkBook with ID ${query.id} not found")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.repo.dao.workbook.query

data class SelectWorkBookRecordQuery(
val id: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.few.api.repo.dao.workbook.record

import java.net.URL
import java.time.LocalDateTime

data class SelectWorkBookRecord(
val id: Long,
val title: String,
val mainImageUrl: URL,
val category: Long,
val description: String,
val createdAt: LocalDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.few.api.repo.dao.article

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 jooq.jooq_dsl.tables.ArticleIfo
import jooq.jooq_dsl.tables.ArticleMst
Expand All @@ -13,6 +14,7 @@ import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional
import java.net.URL

class ArticleDaoTest : JooqTestSpec() {

Expand All @@ -32,13 +34,13 @@ class ArticleDaoTest : JooqTestSpec() {
dslContext.insertInto(ArticleMst.ARTICLE_MST)
.set(ArticleMst.ARTICLE_MST.ID, 1L)
.set(ArticleMst.ARTICLE_MST.MEMBER_ID, 1L)
.set(ArticleMst.ARTICLE_MST.MAIN_IMAGE_URL, "http://localhost:8080/image.jpg")
.set(ArticleMst.ARTICLE_MST.TITLE, "this is title")
.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)
.execute()
dslContext.insertInto(ArticleIfo.ARTICLE_IFO)
.set(ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID, 1L)
.set(ArticleIfo.ARTICLE_IFO.CONTENT, "this is content")
.set(ArticleIfo.ARTICLE_IFO.CONTENT, "this is content1")
.execute()
log.debug("===== finish setUp =====")
}
Expand All @@ -58,17 +60,17 @@ class ArticleDaoTest : JooqTestSpec() {
assertNotNull(result)
assertEquals(1L, result.articleId)
assertEquals(1L, result.writerId)
assertEquals("http://localhost:8080/image.jpg", result.mainImageURL)
assertEquals("this is title", result.title)
assertEquals("this is content", result.content)
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)
}

@Test
@Transactional
fun `학습지 Id와 아티클 Id를 통해 학습지에서의 아티클 Day가 포함된 아티클 정보를 조회합니다`() {
// given
setMappingWorkbookArticleData()
setMappingWorkbookArticleData(1)
val query = SelectWorkBookArticleRecordQuery(1L, 1L)

// when
Expand All @@ -80,19 +82,49 @@ class ArticleDaoTest : JooqTestSpec() {
assertNotNull(result)
assertEquals(1L, result.articleId)
assertEquals(1L, result.writerId)
assertEquals("http://localhost:8080/image.jpg", result.mainImageURL)
assertEquals("this is title", result.title)
assertEquals("this is content", result.content)
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(1L, result.day)
}

private fun setMappingWorkbookArticleData() {
@Test
@Transactional
fun `학습지에 속한 아티클 정보를 조회합니다`() {
// given
val totalCount = 5
setMappingWorkbookArticleData(totalCount)
val query = SelectWorkbookMappedArticleRecordsQuery(1L)

// when
val result = query.let {
articleDao.selectWorkbookMappedArticleRecords(it)
}

// then
assertNotNull(result)
assertEquals(totalCount, result.size)
for (i in result.indices) {
assertEquals(i + 1L, result[i].articleId)
assertEquals(1L, result[i].writerId)
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
}
}

private fun setMappingWorkbookArticleData(count: Int) {
log.debug("===== start setMappingWorkbookArticleData =====")
dslContext.deleteFrom(MappingWorkbookArticle.MAPPING_WORKBOOK_ARTICLE).execute()
setMappingWorkbookArticle(1L, 1L, 1)
setMappingWorkbookArticle(1L, 2L, 2)
setMappingWorkbookArticle(1L, 3L, 3)
for (i in 1..count) {
setMappingWorkbookArticle(1L, i.toLong(), i)
}
for (i in 2..count) {
setArticleMST(i.toLong())
setArticleInfo(i.toLong())
}
log.debug("===== finish setMappingWorkbookArticleData =====")
}

Expand All @@ -103,4 +135,21 @@ class ArticleDaoTest : JooqTestSpec() {
.set(MappingWorkbookArticle.MAPPING_WORKBOOK_ARTICLE.DAY_COL, day)
.execute()
}

private fun setArticleMST(id: Long) {
dslContext.insertInto(ArticleMst.ARTICLE_MST)
.set(ArticleMst.ARTICLE_MST.ID, id)
.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)
.execute()
}

private fun setArticleInfo(id: Long) {
dslContext.insertInto(ArticleIfo.ARTICLE_IFO)
.set(ArticleIfo.ARTICLE_IFO.ARTICLE_MST_ID, id)
.set(ArticleIfo.ARTICLE_IFO.CONTENT, "this is content$id")
.execute()
}
}
Loading
Loading