mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-07 21:01:14 +00:00
some fix on Manager functions.
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
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
|
||||
|
||||
object Managers: IntIdTable("manager") {
|
||||
val user = reference("user", Users)
|
||||
val managerId = long("manager_id")
|
||||
val discordGuildId = long("discord_guild_id")
|
||||
var lastUserName = varchar("last_user_name", 255).nullable()
|
||||
}
|
||||
|
||||
class Manager(id: EntityID<Int>) : IntEntity(id) {
|
||||
companion object : IntEntityClass<Manager>(Managers)
|
||||
|
||||
var user by User referencedOn Managers.user
|
||||
var managerId by Managers.managerId
|
||||
var discordGuildId by Managers.discordGuildId
|
||||
var lastUserName by Managers.lastUserName
|
||||
}
|
@@ -26,4 +26,7 @@ class User(id: EntityID<Int>) : IntEntity(id) {
|
||||
var liveAlertGuild by Users.liveAlertGuild
|
||||
var liveAlertChannel by Users.liveAlertChannel
|
||||
var liveAlertMessage by Users.liveAlertMessage
|
||||
|
||||
val managers by User via UserManagers
|
||||
val suborinates by User via UserManagers
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
package space.mori.chzzk_bot.common.models
|
||||
|
||||
import org.jetbrains.exposed.sql.Table
|
||||
|
||||
object UserManagers: Table("user_managers") {
|
||||
val user = reference("user_id", Users)
|
||||
val manager = reference("manager_id", Users)
|
||||
}
|
@@ -1,79 +0,0 @@
|
||||
package space.mori.chzzk_bot.common.services
|
||||
|
||||
import org.jetbrains.exposed.dao.load
|
||||
import org.jetbrains.exposed.dao.with
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import space.mori.chzzk_bot.common.models.Manager
|
||||
import space.mori.chzzk_bot.common.models.Managers
|
||||
import space.mori.chzzk_bot.common.models.User
|
||||
|
||||
object ManagerService {
|
||||
fun saveManager(user: User, discordId: Long, name: String): Manager {
|
||||
if (user.liveAlertGuild == null)
|
||||
throw RuntimeException("${user.username} has no liveAlertGuild")
|
||||
|
||||
return transaction {
|
||||
Manager.new {
|
||||
this.user = user
|
||||
this.discordGuildId = user.liveAlertGuild!!
|
||||
this.managerId = discordId
|
||||
this.lastUserName = name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateManager(user: User, discordId: Long, name: String): Manager {
|
||||
return transaction {
|
||||
if (user.liveAlertGuild == null)
|
||||
throw RuntimeException("${user.username} has no liveAlertGuild")
|
||||
|
||||
val manager = getUser(user.liveAlertGuild!!, discordId) ?: throw RuntimeException("$name isn't manager.")
|
||||
|
||||
manager.lastUserName = name
|
||||
|
||||
manager
|
||||
}
|
||||
}
|
||||
|
||||
fun getUser(guildId: Long, discordId: Long): Manager? {
|
||||
return transaction {
|
||||
val manager = Manager.find(
|
||||
(Managers.discordGuildId eq guildId) and (Managers.managerId eq discordId),
|
||||
)
|
||||
.with(Manager::user)
|
||||
.firstOrNull()
|
||||
manager
|
||||
}
|
||||
}
|
||||
|
||||
fun getAllUsers(guildId: Long): List<Manager> {
|
||||
return transaction {
|
||||
val result = Manager.find(Managers.discordGuildId eq guildId)
|
||||
.with(Manager::user)
|
||||
.toList()
|
||||
|
||||
result.forEach { it.load(Manager::user) }
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteManager(user: User, discordId: Long): Manager {
|
||||
if (user.liveAlertGuild == null)
|
||||
throw RuntimeException("${user.username} has no liveAlertGuild")
|
||||
return deleteManager(user.liveAlertGuild!!, discordId)
|
||||
}
|
||||
|
||||
fun deleteManager(guildId: Long, discordId: Long): Manager {
|
||||
return transaction {
|
||||
val managerRow = Manager.find((Managers.discordGuildId eq guildId) and (Managers.managerId eq discordId)).firstOrNull()
|
||||
|
||||
managerRow ?: throw RuntimeException("Manager not found! $discordId")
|
||||
managerRow.delete()
|
||||
|
||||
managerRow
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user