29 lines
1.1 KiB
Kotlin
29 lines
1.1 KiB
Kotlin
package dev.inmo.postssystem.core.content.api.business.content_adapters
|
|
|
|
import dev.inmo.micro_utils.repos.*
|
|
import dev.inmo.postssystem.core.content.Content
|
|
import dev.inmo.postssystem.core.content.ContentId
|
|
import dev.inmo.postssystem.core.content.api.business.AdapterType
|
|
import dev.inmo.postssystem.core.content.api.business.BusinessContentRepoContentAdapter
|
|
|
|
class KeyValueBusinessContentRepoAdapter<T>(
|
|
override val type: AdapterType,
|
|
private val keyValueRepo: StandardKeyValueRepo<ContentId, T>,
|
|
private val contentToData: suspend (Content) -> T?,
|
|
private val dataToContent: suspend (T) -> Content
|
|
) : BusinessContentRepoContentAdapter {
|
|
override suspend fun storeContent(contentId: ContentId, content: Content): Boolean {
|
|
keyValueRepo.set(contentId, contentToData(content) ?: return false)
|
|
return true
|
|
}
|
|
|
|
override suspend fun getContent(contentId: ContentId): Content? {
|
|
return dataToContent(
|
|
keyValueRepo.get(contentId) ?: return null
|
|
)
|
|
}
|
|
|
|
override suspend fun removeContent(contentId: ContentId) {
|
|
keyValueRepo.unset(contentId)
|
|
}
|
|
} |