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

55 lines
1.5 KiB
Kotlin
Raw Normal View History

2020-11-25 08:08:45 +00:00
package dev.inmo.postssystem.core.exposed.content
2020-04-11 07:06:10 +00:00
2020-11-25 08:08:45 +00:00
import dev.inmo.postssystem.core.content.ContentId
import dev.inmo.postssystem.core.content.SpecialContent
2020-04-11 07:06:10 +00:00
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
private class SpecialContentHolderRepoTable(
private val database: Database
) : ContentHolderRepo<SpecialContent>, Table() {
private val idColumn = text("id")
private val internalIdColumn = text("internalId")
override val primaryKey: PrimaryKey = PrimaryKey(idColumn)
init {
2020-07-30 09:34:32 +00:00
transaction(
db = database
) {
2020-04-11 07:06:10 +00:00
SchemaUtils.createMissingTablesAndColumns(this@SpecialContentHolderRepoTable)
}
}
2020-07-31 05:28:27 +00:00
override suspend fun getContent(id: ContentId): SpecialContent? = transaction(
2020-07-30 09:34:32 +00:00
db = database
) {
2020-04-11 07:06:10 +00:00
select {
idColumn.eq(id)
}.limit(1).firstOrNull() ?.get(internalIdColumn) ?.let {
SpecialContent(it)
}
}
override suspend fun removeContent(id: ContentId) {
2020-07-31 05:28:27 +00:00
transaction(
2020-07-30 09:34:32 +00:00
db = database
) {
2020-04-11 07:06:10 +00:00
deleteWhere { idColumn.eq(id) }
}
}
override suspend fun putContent(id: ContentId, content: SpecialContent) {
2020-07-31 05:28:27 +00:00
transaction(
2020-07-30 09:34:32 +00:00
db = database
) {
2020-04-11 07:06:10 +00:00
insert {
it[idColumn] = id
it[internalIdColumn] = content.internalId
}
}
}
}
class SpecialContentHolderRepo(
database: Database
) : ContentHolderRepo<SpecialContent> by SpecialContentHolderRepoTable(database)