core/core/exposed/src/jvmMain/kotlin/dev/inmo/postssystem/core/exposed/content/TextContentHolderRepo.kt

55 lines
1.5 KiB
Kotlin

package dev.inmo.postssystem.core.exposed.content
import dev.inmo.postssystem.core.content.ContentId
import dev.inmo.postssystem.core.content.TextContent
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
private class TextContentHolderRepoTable(
private val database: Database
) : ContentHolderRepo<TextContent>, Table() {
private val idColumn = text("id")
private val textColumn = text("text")
override val primaryKey: PrimaryKey = PrimaryKey(idColumn)
init {
transaction(
db = database
) {
SchemaUtils.createMissingTablesAndColumns(this@TextContentHolderRepoTable)
}
}
override suspend fun getContent(id: ContentId): TextContent? = transaction(
db = database
) {
select {
idColumn.eq(id)
}.limit(1).firstOrNull() ?.get(textColumn) ?.let {
TextContent(it)
}
}
override suspend fun removeContent(id: ContentId) {
transaction(
db = database
) {
deleteWhere { idColumn.eq(id) }
}
}
override suspend fun putContent(id: ContentId, content: TextContent) {
transaction(
db = database
) {
insert {
it[idColumn] = id
it[textColumn] = content.text
}
}
}
}
class TextContentHolderRepo(
database: Database
) : ContentHolderRepo<TextContent> by TextContentHolderRepoTable(database)