feat(content-all): 전체 탭 요청 보정 정책을 추가한다
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package kr.co.vividnext.sodalive.v2.content.all.domain
|
||||
|
||||
import kr.co.vividnext.sodalive.creator.admin.content.series.SeriesPublishedDaysOfWeek
|
||||
import kr.co.vividnext.sodalive.v2.common.domain.ContentSort
|
||||
|
||||
class MainContentAllQueryPolicy {
|
||||
fun resolveType(type: String?): MainContentAllType {
|
||||
return type.toEnumOrNull<MainContentAllType>() ?: MainContentAllType.AUDIO
|
||||
}
|
||||
|
||||
fun resolveSort(sort: String?): ContentSort {
|
||||
val resolved = sort.toEnumOrNull<ContentSort>() ?: ContentSort.LATEST
|
||||
return if (resolved == ContentSort.OWNED) ContentSort.LATEST else resolved
|
||||
}
|
||||
|
||||
fun resolveDayOfWeek(type: MainContentAllType, dayOfWeek: String?): SeriesPublishedDaysOfWeek? {
|
||||
if (type != MainContentAllType.SERIES) return null
|
||||
return dayOfWeek.toEnumOrNull<SeriesPublishedDaysOfWeek>()
|
||||
}
|
||||
|
||||
fun createPage(page: Int?, size: Int?): MainContentPage {
|
||||
return MainContentPage(
|
||||
page = page?.coerceAtLeast(0) ?: DEFAULT_PAGE,
|
||||
size = size?.coerceIn(MIN_SIZE, MAX_SIZE) ?: DEFAULT_SIZE
|
||||
)
|
||||
}
|
||||
|
||||
fun <T> limitItems(items: List<T>, page: MainContentPage): List<T> {
|
||||
return items.take(page.size)
|
||||
}
|
||||
|
||||
fun hasNext(items: List<*>, page: MainContentPage): Boolean {
|
||||
return items.size > page.size
|
||||
}
|
||||
|
||||
private inline fun <reified T : Enum<T>> String?.toEnumOrNull(): T? {
|
||||
if (this == null) return null
|
||||
return enumValues<T>().firstOrNull { it.name == this }
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val DEFAULT_PAGE = 0
|
||||
private const val DEFAULT_SIZE = 20
|
||||
private const val MIN_SIZE = 20
|
||||
private const val MAX_SIZE = 50
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user