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 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 ) : BusinessContentRepoReadHelper { override suspend fun getKeysByPagination(pagination: Pagination): PaginationResult = 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 ) : 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 ) : BusinessContentRepoHelper, BusinessContentRepoReadHelper by KeyValueBusinessContentRepoReadHelper( keyValueRepo ), BusinessContentRepoWriteHelper by KeyValueBusinessContentRepoWriteHelper( keyValueRepo ) fun StandardKeyValueRepo.asBusinessContentRepo( adapters: List ) = BusinessContentRepo( adapters, KeyValueBusinessContentRepoHelper(this) ) fun StandardKeyValueRepo.asBusinessContentRepo( vararg adapters: BusinessContentRepoContentAdapter ) = asBusinessContentRepo(adapters.toList())