Skip to content

Commit

Permalink
[Refactor/#427] BrowseWorkbooksUseCase 리펙토링 (#428)
Browse files Browse the repository at this point in the history
* feat: Workbooks 모델 생성

* refactor: 기존 WorkbookOrderDelegator 이동 및 order 메서드 시그니쳐 변경

* refactor: WorkbookOrderDelegator 구현체 이동 및 변경 반영

* feat: OrderedWorkBooks 모델 생성

* refactor: BrowseWorkbooksUseCase에 변경 반영

* refactor: 변경 사항 테스트에 반영

* refactor: WorkBook 클래스 범용 데이터 클래스로 변경

* feat: order 모델에서 사용할 OrderTargetWorkBooks 구현

* feat: order 모델 부모 객체 생성

* refactor: order 모델 변경 사항 반영

* refactor: 변경 사항 반영

* refactor: orderedWorkbooks 공통으로 처리할 수 있도록 수정

* fix: 변경 사항 테스트에 적용
  • Loading branch information
belljun3395 authored Oct 10, 2024
1 parent 344520a commit 605cb07
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 199 deletions.
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)
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)

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 ->
orderDelegator
?.let { delegator ->
UnOrderedWorkBooks(
target,
delegator
).order()
}
}
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>>,
): 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

0 comments on commit 605cb07

Please sign in to comment.