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

[Refactor/#427] BrowseWorkbooksUseCase 리펙토링 #428

Merged
merged 13 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -4,16 +4,16 @@ import com.few.api.domain.workbook.service.WorkbookMemberService
import com.few.api.domain.workbook.service.WorkbookSubscribeService
import com.few.api.domain.workbook.service.dto.BrowseMemberSubscribeWorkbooksInDto
import com.few.api.domain.workbook.service.dto.BrowseWorkbookWriterRecordsInDto
import com.few.api.domain.workbook.service.dto.WriterMappedWorkbookOutDto
import com.few.api.domain.workbook.usecase.dto.BrowseWorkBookDetail
import com.few.api.domain.workbook.usecase.dto.BrowseWorkbooksUseCaseIn
import com.few.api.domain.workbook.usecase.dto.BrowseWorkbooksUseCaseOut
import com.few.api.domain.workbook.usecase.dto.WriterDetail
import com.few.api.domain.workbook.usecase.model.*
import com.few.api.domain.workbook.usecase.service.order.AuthMainViewWorkbookOrderDelegator
import com.few.api.domain.workbook.usecase.service.order.BasicWorkbookOrderDelegator
import com.few.api.domain.workbook.usecase.service.order.WorkbookOrderDelegatorExecutor
import com.few.api.domain.workbook.usecase.model.order.*
import com.few.api.repo.dao.workbook.WorkbookDao
import com.few.api.repo.dao.workbook.query.BrowseWorkBookQueryWithSubscriptionCountQuery
import com.few.api.repo.dao.workbook.record.SelectWorkBookRecordWithSubscriptionCount
import com.few.api.web.support.ViewCategory
import com.few.data.common.code.CategoryType
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -42,7 +42,6 @@ class BrowseWorkbooksUseCase(
private val workbookDao: WorkbookDao,
private val workbookMemberService: WorkbookMemberService,
private val workbookSubscribeService: WorkbookSubscribeService,
private val workbookOrderDelegatorExecutor: WorkbookOrderDelegatorExecutor,
) {

@Transactional
Expand All @@ -56,54 +55,30 @@ class BrowseWorkbooksUseCase(
BrowseWorkbookWriterRecordsInDto(workbookIds)
)

val workbookDetails = workbookRecords.map { record ->
WorkBook(
id = record.id,
mainImageUrl = record.mainImageUrl,
title = record.title,
description = record.description,
category = CategoryType.convertToDisplayName(record.category),
createdAt = record.createdAt,
writerDetails = writerRecords[record.id]?.map {
WorkBookWriter(
id = it.writerId,
name = it.name,
url = it.url
)
} ?: emptyList(),
subscriptionCount = record.subscriptionCount
)
val orderStrategy = getOrderStrategy(useCaseIn)
Copy link
Collaborator Author

@belljun3395 belljun3395 Sep 28, 2024

Choose a reason for hiding this comment

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

useCaseIn의 값을 가지고 단순 판단만 들어가 getOrderStrategy라 네이밍 하였습니다.

val orderDelegator = when (orderStrategy) {
WorkBookOrderStrategy.MAIN_VIEW_AUTH -> {
genAuthMainViewWorkbookOrderDelegator(useCaseIn)
}
/** BASIC, MAIN_VIEW_UNAUTH -> 해당 경우는 DB 조회 결과를 그대로 반환 */
else -> null
}

val orderStrategy = when {
useCaseIn.viewCategory == ViewCategory.MAIN_CARD && useCaseIn.memberId != null -> WorkBookOrderStrategy.MAIN_VIEW_AUTH
useCaseIn.viewCategory == ViewCategory.MAIN_CARD && useCaseIn.memberId == null -> WorkBookOrderStrategy.MAIN_VIEW_UNAUTH
else -> WorkBookOrderStrategy.BASIC
}
val workbooks = toWorkbooks(workbookRecords, writerRecords)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

우선 workbooks 데이터로 만듭니다.


val orderedWorkbooks = when (orderStrategy) {
WorkBookOrderStrategy.MAIN_VIEW_AUTH -> {
BrowseMemberSubscribeWorkbooksInDto(useCaseIn.memberId!!).let { dto ->
workbookSubscribeService.browseMemberSubscribeWorkbooks(dto)
}.map {
MemberSubscribedWorkbook(
workbookId = it.workbookId,
isActiveSub = it.isActiveSub,
currentDay = it.currentDay
)
}.let { subscribedWorkbooks ->
AuthMainViewWorkbookOrderDelegator(workbookDetails, subscribedWorkbooks)
val orderedWorkbook = OrderTargetWorkBooks(workbooks).let { target ->
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

order 모델에서 사용할 OrderTargetWorkBooks로 변환합니다.

orderDelegator
?.let { delegator ->
UnOrderedWorkBooks(
target,
delegator
).order().orderedWorkbooks
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

orderDelegator가 있으면 변환할 필요가 있다고 판단하고 UnOrderedWorkBooks를 만든 이후 정렬합니다.

}
WorkBookOrderStrategy.MAIN_VIEW_UNAUTH -> {
BasicWorkbookOrderDelegator(workbookDetails)
}
else -> BasicWorkbookOrderDelegator(workbookDetails)
}.let { delegator ->
workbookOrderDelegatorExecutor.execute(delegator)
?: run { OrderedWorkBooks(target).orderedWorkbooks }
}

orderedWorkbooks.map { workBook ->
val orderedWorkbookData = orderedWorkbook.workbookData
orderedWorkbookData.map { workBook ->
BrowseWorkBookDetail(
id = workBook.id,
mainImageUrl = workBook.mainImageUrl,
Expand All @@ -126,4 +101,51 @@ class BrowseWorkbooksUseCase(
)
}
}

private fun getOrderStrategy(useCaseIn: BrowseWorkbooksUseCaseIn) =
when {
useCaseIn.viewCategory == ViewCategory.MAIN_CARD && useCaseIn.memberId != null -> WorkBookOrderStrategy.MAIN_VIEW_AUTH
useCaseIn.viewCategory == ViewCategory.MAIN_CARD && useCaseIn.memberId == null -> WorkBookOrderStrategy.MAIN_VIEW_UNAUTH
else -> WorkBookOrderStrategy.BASIC
}

private fun genAuthMainViewWorkbookOrderDelegator(useCaseIn: BrowseWorkbooksUseCaseIn): WorkbookOrderDelegator {
return BrowseMemberSubscribeWorkbooksInDto(useCaseIn.memberId!!).let { dto ->
workbookSubscribeService.browseMemberSubscribeWorkbooks(dto)
}.map {
MemberSubscribedWorkbook(
workbookId = it.workbookId,
isActiveSub = it.isActiveSub,
currentDay = it.currentDay
)
}.let { subscribedWorkbooks ->
AuthMainViewWorkbookOrderDelegator(subscribedWorkbooks)
}
}

private fun toWorkbooks(
workbookRecords: List<SelectWorkBookRecordWithSubscriptionCount>,
writerRecords: Map<Long, List<WriterMappedWorkbookOutDto>>,
Comment on lines +126 to +128
Copy link
Member

Choose a reason for hiding this comment

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

SelectWorkBookRecordWithSubscriptionCount -> 디비에서 조회된 결과를 record로 끝나기로 약속되어 있는데 네이밍이 약간 어색해보임.

writerRecords -> 실제 받는 타입이 레코드가 아닌데 변수명은 왜 record인지?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

요 부분은 인텔리 자동완성에 너무 의지한 것 같네요..!
적절한 네이밍은 고민해볼께요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

디비에서 조회된 결과를 record로 끝나기로 약속되어 있는데 네이밍이 약간 어색해보임.

다시보니 파라미터로 받는 둘다 디비에서 조회된 결과를 받는 것이라 record로 해도 괜찮을 것 같은데
혹시 다른 적절한 파라미터 네이밍 있을까요?

): WorkBooks {
return workbookRecords.map { record ->
WorkBook(
id = record.id,
mainImageUrl = record.mainImageUrl,
title = record.title,
description = record.description,
category = CategoryType.convertToDisplayName(record.category),
createdAt = record.createdAt,
writerDetails = writerRecords[record.id]?.map {
WorkBookWriter(
id = it.writerId,
name = it.name,
url = it.url
)
} ?: emptyList(),
subscriptionCount = record.subscriptionCount
)
}.let {
WorkBooks(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.domain.workbook.usecase.model

data class WorkBooks(
val workbookData: List<WorkBook>,
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.few.api.domain.workbook.usecase.service.order
package com.few.api.domain.workbook.usecase.model.order

import com.few.api.domain.workbook.usecase.model.MemberSubscribedWorkbook
import com.few.api.domain.workbook.usecase.model.WorkBook
import com.few.api.domain.workbook.usecase.model.WorkBooks

class AuthMainViewWorkbookOrderDelegator(
/**
* @see com.few.api.repo.dao.workbook.WorkbookDao.browseWorkBookWithSubscriptionCount
*/
private val workbooks: List<WorkBook>,
private val memberSubscribedWorkbooks: List<MemberSubscribedWorkbook>,
) : WorkbookOrderDelegator {

Expand All @@ -18,7 +15,8 @@ class AuthMainViewWorkbookOrderDelegator(
* 2. 구독 기록이 없는 워크북을 보여줍니다.
* 3. 비활성화된 구독 워크북을 보여줍니다.
*/
override fun order(): List<WorkBook> {
override fun order(targetWorkBooks: OrderTargetWorkBooks): OrderTargetWorkBooks {
val workbooks = targetWorkBooks.workbooks.workbookData
val allWorkbookIds = workbooks.associate { it.id to false }.toMutableMap()
val activeSubWorkbookIds =
memberSubscribedWorkbooks.filter { it.isActiveSub }.sortedByDescending {
Expand Down Expand Up @@ -64,6 +62,6 @@ class AuthMainViewWorkbookOrderDelegator(
*/
orderedWorkbooks.addAll(lastAddWorkbooks)

return orderedWorkbooks
return OrderTargetWorkBooks(WorkBooks(orderedWorkbooks))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.domain.workbook.usecase.model.order

open class OrderAbleWorkBooks(
protected open val targetWorkBooks: OrderTargetWorkBooks,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.few.api.domain.workbook.usecase.model.order

import com.few.api.domain.workbook.usecase.model.WorkBooks

data class OrderTargetWorkBooks(
val workbooks: WorkBooks,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.few.api.domain.workbook.usecase.model.order

import com.few.api.domain.workbook.usecase.model.WorkBooks

class OrderedWorkBooks(
orderTargetWorkBooks: OrderTargetWorkBooks,
) : OrderAbleWorkBooks(orderTargetWorkBooks) {
val orderedWorkbooks: WorkBooks = orderTargetWorkBooks.workbooks
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.few.api.domain.workbook.usecase.model.order

class UnOrderedWorkBooks(
orderTargetWorkBooks: OrderTargetWorkBooks,
private val delegator: WorkbookOrderDelegator,
) : OrderAbleWorkBooks(orderTargetWorkBooks) {
fun order(): OrderedWorkBooks {
val orderedWorkbooks = delegator.order(targetWorkBooks)
return OrderedWorkBooks(orderedWorkbooks)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.few.api.domain.workbook.usecase.model.order

interface WorkbookOrderDelegator {

/**
* 워크북을 정렬합니다.
* @param targetWorkBooks 정렬할 워크북 목록
* @return 정렬된 워크북 목록
*/
fun order(targetWorkBooks: OrderTargetWorkBooks): OrderTargetWorkBooks
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading