core/core/api/src/commonMain/kotlin/dev/inmo/postssystem/core/content/api/business/BusinessContentRepoHelper.kt

60 lines
2.5 KiB
Kotlin

package dev.inmo.postssystem.core.content.api.business
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.repos.*
import dev.inmo.postssystem.core.content.ContentId
typealias AdapterType = String
interface BusinessContentRepoReadHelper : Repo {
suspend fun getKeysByPagination(pagination: Pagination): PaginationResult<ContentId>
suspend fun contains(contentId: ContentId): Boolean
suspend fun getType(contentId: ContentId): AdapterType?
suspend fun count(): Long
}
interface BusinessContentRepoWriteHelper : Repo {
suspend fun deleteContentId(contentId: ContentId)
suspend fun saveType(contentId: ContentId, type: AdapterType): Boolean
}
interface BusinessContentRepoHelper : BusinessContentRepoReadHelper, BusinessContentRepoWriteHelper
class KeyValueBusinessContentRepoReadHelper(
private val keyValueRepo: ReadStandardKeyValueRepo<ContentId, AdapterType>
) : BusinessContentRepoReadHelper {
override suspend fun getKeysByPagination(pagination: Pagination): PaginationResult<ContentId> = keyValueRepo.keys(pagination)
override suspend fun contains(contentId: ContentId): Boolean = keyValueRepo.contains(contentId)
override suspend fun getType(contentId: ContentId): AdapterType? = keyValueRepo.get(contentId)
override suspend fun count(): Long = keyValueRepo.count()
}
class KeyValueBusinessContentRepoWriteHelper(
private val keyValueRepo: WriteStandardKeyValueRepo<ContentId, AdapterType>
) : BusinessContentRepoWriteHelper {
override suspend fun deleteContentId(contentId: ContentId) { keyValueRepo.unset(contentId) }
override suspend fun saveType(contentId: ContentId, type: AdapterType): Boolean {
keyValueRepo.set(contentId, type)
return true
}
}
class KeyValueBusinessContentRepoHelper(
private val keyValueRepo: StandardKeyValueRepo<ContentId, AdapterType>
) : BusinessContentRepoHelper, BusinessContentRepoReadHelper by KeyValueBusinessContentRepoReadHelper(
keyValueRepo
), BusinessContentRepoWriteHelper by KeyValueBusinessContentRepoWriteHelper(
keyValueRepo
)
fun StandardKeyValueRepo<ContentId, AdapterType>.asBusinessContentRepo(
adapters: List<BusinessContentRepoContentAdapter>
) = BusinessContentRepo(
adapters,
KeyValueBusinessContentRepoHelper(this)
)
fun StandardKeyValueRepo<ContentId, AdapterType>.asBusinessContentRepo(
vararg adapters: BusinessContentRepoContentAdapter
) = asBusinessContentRepo(adapters.toList())