mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-07 21:01:14 +00:00
add TimerConfig.kt, TimerConfigService
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package space.mori.chzzk_bot.common.models
|
||||
|
||||
import org.jetbrains.exposed.dao.IntEntity
|
||||
import org.jetbrains.exposed.dao.IntEntityClass
|
||||
import org.jetbrains.exposed.dao.id.EntityID
|
||||
import org.jetbrains.exposed.dao.id.IntIdTable
|
||||
import org.jetbrains.exposed.sql.ReferenceOption
|
||||
|
||||
object TimerConfigs: IntIdTable("timer_config") {
|
||||
val user = reference("user", Users, onDelete = ReferenceOption.CASCADE)
|
||||
val option = integer("option")
|
||||
}
|
||||
class TimerConfig(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<TimerConfig>(TimerConfigs)
|
||||
|
||||
var user by User referencedOn TimerConfigs.user
|
||||
var option by TimerConfigs.option
|
||||
}
|
@@ -11,7 +11,7 @@ import space.mori.chzzk_bot.common.models.User
|
||||
object CommandService {
|
||||
fun saveCommand(user: User, command: String, content: String, failContent: String): Command {
|
||||
return transaction {
|
||||
return@transaction Command.new {
|
||||
Command.new {
|
||||
this.user = user
|
||||
this.command = command
|
||||
this.content = content
|
||||
|
@@ -0,0 +1,48 @@
|
||||
package space.mori.chzzk_bot.common.services
|
||||
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.jetbrains.exposed.sql.update
|
||||
import space.mori.chzzk_bot.common.events.TimerType
|
||||
import space.mori.chzzk_bot.common.models.TimerConfig
|
||||
import space.mori.chzzk_bot.common.models.TimerConfigs
|
||||
import space.mori.chzzk_bot.common.models.User
|
||||
|
||||
object TimerConfigService {
|
||||
fun saveConfig(user: User, timerConfig: TimerType) {
|
||||
return transaction {
|
||||
TimerConfig.new {
|
||||
this.user = user
|
||||
this.option = timerConfig.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateConfig(user: User, timerConfig: TimerType) {
|
||||
return transaction {
|
||||
val updated = TimerConfigs.update({
|
||||
TimerConfigs.user eq user.id
|
||||
}) {
|
||||
it[option] = timerConfig.value
|
||||
}
|
||||
|
||||
if (updated == 0) throw RuntimeException("TimerConfig not found! ${user.username}")
|
||||
|
||||
TimerConfig.find { TimerConfigs.user eq user.id }.first()
|
||||
}
|
||||
}
|
||||
|
||||
fun getConfig(user: User): TimerConfig? {
|
||||
return transaction {
|
||||
TimerConfig.find(TimerConfigs.user eq user.id).firstOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
fun saveOrUpdateConfig(user: User, timerConfig: TimerType) {
|
||||
return if (getConfig(user) == null) {
|
||||
saveConfig(user, timerConfig)
|
||||
} else {
|
||||
updateConfig(user, timerConfig)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user