Skip to content

Commit

Permalink
[Refactor/#285] 워크북 정렬 문제 해결 (#288)
Browse files Browse the repository at this point in the history
* feat: browseMemberSubscribeWorkbooks 구현

* feat: WorkbookOrderDelegator 선언

* feat: BasicWorkbookOrderDelegator 구현

* feat: MainViewWorkbookOrderDelegator 구현

* feat: BrowseWorkbooksUseCaseIn에 viewCategory, memberId 추가

* refactor: orderStrategy에 따라 정렬된 워크북 반환하도록 수정

* refactor: view 파람 추가

* refactor: mainView에서 로그인/비로그인 분리하도록 수정

* refactor: WorkbookOrderDelegatorExecutor 구현 및 반영

* test: 변경 사항 테스트 반영

* chore: workbooks 인자의 주석 추가
  • Loading branch information
belljun3395 authored Aug 3, 2024
1 parent 476bf95 commit 44332ef
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.few.api.domain.workbook.service

import com.few.api.domain.workbook.service.dto.BrowseMemberSubscribeWorkbooksInDto
import com.few.api.domain.workbook.service.dto.BrowseMemberSubscribeWorkbooksOutDto
import com.few.api.repo.dao.subscription.SubscriptionDao
import com.few.api.repo.dao.subscription.query.SelectAllMemberWorkbookSubscriptionStatusNotConsiderDeletedAtQuery
import org.springframework.stereotype.Service

@Service
class WorkbookSubscribeService(
private val subscriptionDao: SubscriptionDao,
) {

fun browseMemberSubscribeWorkbooks(dto: BrowseMemberSubscribeWorkbooksInDto): List<BrowseMemberSubscribeWorkbooksOutDto> {
return SelectAllMemberWorkbookSubscriptionStatusNotConsiderDeletedAtQuery(dto.memberId).let { it ->
subscriptionDao.selectAllWorkbookSubscriptionStatus(it).map {
BrowseMemberSubscribeWorkbooksOutDto(
workbookId = it.workbookId,
isActiveSub = it.isActiveSub,
currentDay = it.currentDay
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.few.api.domain.workbook.service.dto

data class BrowseMemberSubscribeWorkbooksInDto(
val memberId: Long,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.few.api.domain.workbook.service.dto

data class BrowseMemberSubscribeWorkbooksOutDto(
val workbookId: Long,
val isActiveSub: Boolean,
val currentDay: Int,
)
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
package com.few.api.domain.workbook.usecase

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.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.BasicWorkbookOrderDelegator
import com.few.api.domain.workbook.usecase.model.AuthMainViewWorkbookOrderDelegator
import com.few.api.domain.workbook.usecase.service.WorkbookOrderDelegatorExecutor
import com.few.api.repo.dao.workbook.WorkbookDao
import com.few.api.repo.dao.workbook.query.BrowseWorkBookQueryWithSubscriptionCount
import com.few.api.web.support.ViewCategory
import com.few.data.common.code.CategoryType
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

enum class WorkBookOrderStrategy {
BASIC,

/**
* 로그인 상태에서 메인 화면에 보여질 워크북을 정렬합니다.
* - view의 값이 MAIN_CARD이다.
* - memberId가 null이 아니다.
* */
MAIN_VIEW_AUTH,

/**
* 비로그인 상태에서 메인 화면에 보여질 워크북을 정렬합니다.
* - view의 값이 MAIN_CARD이다.
* - memberId가 null이다.
*/
MAIN_VIEW_UNAUTH,
}

@Component
class BrowseWorkbooksUseCase(
private val workbookDao: WorkbookDao,
private val workbookMemberService: WorkbookMemberService,
private val workbookSubscribeService: WorkbookSubscribeService,
private val workbookOrderDelegatorExecutor: WorkbookOrderDelegatorExecutor,
) {

@Transactional
Expand Down Expand Up @@ -48,8 +74,30 @@ class BrowseWorkbooksUseCase(
)
}

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 orderedWorkbooks = when (orderStrategy) {
WorkBookOrderStrategy.MAIN_VIEW_AUTH -> {
BrowseMemberSubscribeWorkbooksInDto(useCaseIn.memberId!!).let { dto ->
workbookSubscribeService.browseMemberSubscribeWorkbooks(dto)
}.let { memberSubscribeWorkbooks ->
AuthMainViewWorkbookOrderDelegator(workbookDetails, memberSubscribeWorkbooks)
}
}
WorkBookOrderStrategy.MAIN_VIEW_UNAUTH -> {
BasicWorkbookOrderDelegator(workbookDetails)
}
else -> BasicWorkbookOrderDelegator(workbookDetails)
}.let { delegator ->
workbookOrderDelegatorExecutor.execute(delegator)
}

return BrowseWorkbooksUseCaseOut(
workbooks = workbookDetails
workbooks = orderedWorkbooks
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.few.api.domain.workbook.usecase.dto

import com.few.api.web.support.ViewCategory
import com.few.api.web.support.WorkBookCategory

data class BrowseWorkbooksUseCaseIn(
val category: WorkBookCategory,
val viewCategory: ViewCategory?,
val memberId: Long?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.few.api.domain.workbook.usecase.model

import com.few.api.domain.workbook.service.dto.BrowseMemberSubscribeWorkbooksOutDto
import com.few.api.domain.workbook.usecase.dto.BrowseWorkBookDetail

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

/**
* 메인 화면에 보여질 워크북을 정렬합니다.
* 1. 활성화된 구독 워크북을 먼저 보여줍니다.
* 2. 구독 기록이 없는 워크북을 보여줍니다.
* 3. 비활성화된 구독 워크북을 보여줍니다.
*/
override fun order(): List<BrowseWorkBookDetail> {
val allWorkbookIds = workbooks.associate { it.id to false }.toMutableMap()
val activeSubWorkbookIds = memberSubscribeWorkbooks.filter { it.isActiveSub }.sortedByDescending {
it.currentDay
}.map { it.workbookId }
val inActiveSubWorkbookIds = memberSubscribeWorkbooks.filter { !it.isActiveSub }.map { it.workbookId }

val orderedWorkbooks = mutableListOf<BrowseWorkBookDetail>()

/**
* 활성화된 구독 워크북을 먼저 보여줍니다.
*/
activeSubWorkbookIds.forEach { activeSubWorkbookId ->
workbooks.find { it.id == activeSubWorkbookId }?.let {
orderedWorkbooks.add(it)
allWorkbookIds[activeSubWorkbookId] = true
}
}

/**
* 비활성화된 구독 워크북을 모아둡니다.
*/
val lastAddWorkbooks = mutableListOf<BrowseWorkBookDetail>()
inActiveSubWorkbookIds.forEach { inActiveSubWorkbookId ->
workbooks.find { it.id == inActiveSubWorkbookId }?.let {
lastAddWorkbooks.add(it)
allWorkbookIds[inActiveSubWorkbookId] = true
}
}

/**
* 구독 기록이 없는 워크북을 보여줍니다.
*/
allWorkbookIds.filter { !it.value }.forEach { (id, _) ->
workbooks.find { it.id == id }?.let {
orderedWorkbooks.add(it)
}
}

/**
* 비활성화된 구독 워크북을 보여줍니다.
*/
orderedWorkbooks.addAll(lastAddWorkbooks)

return orderedWorkbooks
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.few.api.domain.workbook.usecase.model

import com.few.api.domain.workbook.usecase.dto.BrowseWorkBookDetail

class BasicWorkbookOrderDelegator(
private val workbooks: List<BrowseWorkBookDetail>,
) : WorkbookOrderDelegator {
override fun order(): List<BrowseWorkBookDetail> {
return workbooks
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.few.api.domain.workbook.usecase.model

import com.few.api.domain.workbook.usecase.dto.BrowseWorkBookDetail

interface WorkbookOrderDelegator {

/**
* 워크북을 정렬합니다.
* */
fun order(): List<BrowseWorkBookDetail>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.few.api.domain.workbook.usecase.service

import com.few.api.domain.workbook.usecase.dto.BrowseWorkBookDetail
import com.few.api.domain.workbook.usecase.model.WorkbookOrderDelegator
import org.springframework.stereotype.Service

@Service
class WorkbookOrderDelegatorExecutor {

fun execute(delegator: WorkbookOrderDelegator): List<BrowseWorkBookDetail> {
return delegator.order()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.few.api.web.controller.workbook.response.*
import com.few.api.web.support.WorkBookCategory
import com.few.api.web.support.ApiResponse
import com.few.api.web.support.ApiResponseGenerator
import com.few.api.web.support.ViewCategory
import jakarta.validation.constraints.Min
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
Expand Down Expand Up @@ -45,9 +46,12 @@ class WorkBookController(
fun browseWorkBooks(
@RequestParam(value = "category", required = false)
category: WorkBookCategory?,
@RequestParam(value = "view", required = false)
viewCategory: ViewCategory?,
): ApiResponse<ApiResponse.SuccessBody<BrowseWorkBooksResponse>> {
val useCaseOut =
BrowseWorkbooksUseCaseIn(category ?: WorkBookCategory.All).let { useCaseIn ->
// todo fix memberId
BrowseWorkbooksUseCaseIn(category ?: WorkBookCategory.All, viewCategory, 1L).let { useCaseIn ->
browseWorkBooksUseCase.execute(useCaseIn)
}

Expand Down
Loading

0 comments on commit 44332ef

Please sign in to comment.