-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
052072f
fe48a91
838f7f2
92e0f68
6f79d97
ee3acee
55a1c04
11a473d
a43310f
3d67632
433c3ba
60f3388
d24e5d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -42,7 +42,6 @@ class BrowseWorkbooksUseCase( | |
private val workbookDao: WorkbookDao, | ||
private val workbookMemberService: WorkbookMemberService, | ||
private val workbookSubscribeService: WorkbookSubscribeService, | ||
private val workbookOrderDelegatorExecutor: WorkbookOrderDelegatorExecutor, | ||
) { | ||
|
||
@Transactional | ||
|
@@ -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) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. order 모델에서 사용할 OrderTargetWorkBooks로 변환합니다. |
||
orderDelegator | ||
?.let { delegator -> | ||
UnOrderedWorkBooks( | ||
target, | ||
delegator | ||
).order() | ||
} | ||
Comment on lines
+70
to
76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. orderDelegator 가 존재한다면 정렬 되어있지 않다고 판단하고 UnOrderedWorkBooks를 생성한 후 정렬하여 OrderedWorkBooks를 반환합니다. |
||
} | ||
WorkBookOrderStrategy.MAIN_VIEW_UNAUTH -> { | ||
BasicWorkbookOrderDelegator(workbookDetails) | ||
} | ||
else -> BasicWorkbookOrderDelegator(workbookDetails) | ||
}.let { delegator -> | ||
workbookOrderDelegatorExecutor.execute(delegator) | ||
} | ||
?: run { OrderedWorkBooks(target) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. orderDelegator 가 존재하지 않는다면 정렬된 상태라 판단하고 바로 OrderedWorkBooks를 반환합니다. |
||
}.orderedWorkbooks | ||
|
||
orderedWorkbooks.map { workBook -> | ||
val orderedWorkbookData = orderedWorkbook.workbookData | ||
orderedWorkbookData.map { workBook -> | ||
BrowseWorkBookDetail( | ||
id = workBook.id, | ||
mainImageUrl = workBook.mainImageUrl, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SelectWorkBookRecordWithSubscriptionCount -> 디비에서 조회된 결과를 record로 끝나기로 약속되어 있는데 네이밍이 약간 어색해보임. writerRecords -> 실제 받는 타입이 레코드가 아닌데 변수명은 왜 record인지? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 부분은 인텔리 자동완성에 너무 의지한 것 같네요..! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
다시보니 파라미터로 받는 둘다 디비에서 조회된 결과를 받는 것이라 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 |
---|---|---|
@@ -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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useCaseIn의 값을 가지고 단순 판단만 들어가 getOrderStrategy라 네이밍 하였습니다.