55 lines
1.5 KiB
Kotlin
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.SpecialContent
|
|
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 {
|
|
transaction(
|
|
db = database
|
|
) {
|
|
SchemaUtils.createMissingTablesAndColumns(this@SpecialContentHolderRepoTable)
|
|
}
|
|
}
|
|
|
|
override suspend fun getContent(id: ContentId): SpecialContent? = transaction(
|
|
db = database
|
|
) {
|
|
select {
|
|
idColumn.eq(id)
|
|
}.limit(1).firstOrNull() ?.get(internalIdColumn) ?.let {
|
|
SpecialContent(it)
|
|
}
|
|
}
|
|
|
|
override suspend fun removeContent(id: ContentId) {
|
|
transaction(
|
|
db = database
|
|
) {
|
|
deleteWhere { idColumn.eq(id) }
|
|
}
|
|
}
|
|
|
|
override suspend fun putContent(id: ContentId, content: SpecialContent) {
|
|
transaction(
|
|
db = database
|
|
) {
|
|
insert {
|
|
it[idColumn] = id
|
|
it[internalIdColumn] = content.internalId
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class SpecialContentHolderRepo(
|
|
database: Database
|
|
) : ContentHolderRepo<SpecialContent> by SpecialContentHolderRepoTable(database) |