diff --git a/api/src/main/kotlin/org/anvilpowered/ontime/api/user/OnTimeUser.kt b/api/src/main/kotlin/org/anvilpowered/ontime/api/user/OnTimeUser.kt index 948ceeb..bcd29b9 100644 --- a/api/src/main/kotlin/org/anvilpowered/ontime/api/user/OnTimeUser.kt +++ b/api/src/main/kotlin/org/anvilpowered/ontime/api/user/OnTimeUser.kt @@ -5,13 +5,11 @@ import org.anvilpowered.anvil.core.db.DomainEntity import java.time.LocalDateTime import java.util.UUID -data class OnTimeUser( - override val id: UUID, - val username: String, - val createdUtc: LocalDateTime, - val playTime: Long, - val bonusTime: Long, -) : DomainEntity { +interface OnTimeUser : DomainEntity { + val username: String + val createdUtc: LocalDateTime + val playTime: Long + val bonusTime: Long data class CreateDto( val id: UUID, diff --git a/core/src/main/kotlin/org/anvilpowered/ontime/core/OnTimePlugin.kt b/core/src/main/kotlin/org/anvilpowered/ontime/core/OnTimePlugin.kt index db5b0fd..40324da 100644 --- a/core/src/main/kotlin/org/anvilpowered/ontime/core/OnTimePlugin.kt +++ b/core/src/main/kotlin/org/anvilpowered/ontime/core/OnTimePlugin.kt @@ -24,7 +24,7 @@ import org.anvilpowered.anvil.core.config.Registry import org.anvilpowered.kbrig.tree.LiteralCommandNode import org.anvilpowered.ontime.api.config.OnTimeKeys import org.anvilpowered.ontime.core.command.OnTimeCommandFactory -import org.anvilpowered.ontime.core.db.OnTimeUserTable +import org.anvilpowered.ontime.core.db.OnTimeUsers import org.anvilpowered.ontime.core.registrar.Registrar import org.anvilpowered.ontime.core.task.LuckPermsSyncTaskService import org.apache.logging.log4j.Logger @@ -90,7 +90,7 @@ class OnTimePlugin( logger.info("Finished connecting to database.") logger.info("Creating tables...") transaction { - SchemaUtils.createMissingTablesAndColumns(OnTimeUserTable) + SchemaUtils.createMissingTablesAndColumns(OnTimeUsers) } logger.info("Finished creating tables.") } diff --git a/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserRepositoryImpl.kt b/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserRepositoryImpl.kt index 075ab1d..36a2aeb 100644 --- a/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserRepositoryImpl.kt +++ b/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserRepositoryImpl.kt @@ -39,31 +39,31 @@ class OnTimeUserRepositoryImpl( private val logger: Logger, ) : OnTimeUserRepository { override suspend fun create(item: OnTimeUser.CreateDto): OnTimeUser = newSuspendedTransaction { - val user = OnTimeUserEntity.new(item.id) { + val user = DBOnTimeUser.new(item.id) { username = item.username createdUtc = LocalDateTime.now(ZoneId.of("UTC")) playTime = 0 bonusTime = 0 } logger.info("Created new OnTimeUser ${user.id.value} with data $item") - user.toOnTimeUser() + user } override suspend fun put(item: OnTimeUser.CreateDto): MutableRepository.PutResult = newSuspendedTransaction { val existingUser = getById(item.id) if (existingUser == null) { // create a new OnTimeUser - val newUser = OnTimeUserEntity.new(item.id) { + val newUser = DBOnTimeUser.new(item.id) { username = item.username createdUtc = LocalDateTime.now(ZoneId.of("UTC")) playTime = 0 bonusTime = 0 } logger.info("Created new OnTimeUser ${item.username} (${item.id}) with data $item") - MutableRepository.PutResult(newUser.toOnTimeUser(), created = true) + MutableRepository.PutResult(newUser, created = true) } else { logger.info( - "Loaded existing OnTime user ${existingUser.username} (${existingUser.id}) with" + + "Loaded existing OnTime user ${existingUser.username} (${existingUser.uuid}) with" + "play time ${existingUser.playTimeFormatted}, " + "bonus time ${existingUser.bonusTimeFormatted}, " + "and total time ${existingUser.totalTimeFormatted}", @@ -73,77 +73,77 @@ class OnTimeUserRepositoryImpl( } override suspend fun getAllUsernames(contains: String): SizedIterable = newSuspendedTransaction { - OnTimeUserEntity.find { OnTimeUserTable.username.lowerCase() like "%${contains.lowercase()}%" }.mapLazy { it.username } + DBOnTimeUser.find { OnTimeUsers.username.lowerCase() like "%${contains.lowercase()}%" }.mapLazy { it.username } } override suspend fun getByUsername(username: String): OnTimeUser? = newSuspendedTransaction { - OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.toOnTimeUser() + DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull() } - private fun OnTimeUserEntity.addTime(time: Long) { + private fun DBOnTimeUser.addTime(time: Long) { playTime += time logger.debug("Added {} seconds to User {}", time, id.value) } override suspend fun addPlayTime(id: UUID, duration: Long): Long? = newSuspendedTransaction { - OnTimeUserEntity.findById(id)?.let { user -> + DBOnTimeUser.findById(id)?.let { user -> user.addTime(duration) user.playTime + user.bonusTime } } - private fun OnTimeUserEntity.setTime(time: Long) { + private fun DBOnTimeUser.setTime(time: Long) { bonusTime = time - playTime logger.info("Set total time of user $username (${id.value}) to $time") } override suspend fun setTotalTime(id: UUID, duration: Long): Boolean = newSuspendedTransaction { - OnTimeUserEntity.findById(id)?.setTime(duration) != null + DBOnTimeUser.findById(id)?.setTime(duration) != null } override suspend fun setTotalTime(username: String, duration: Long) = newSuspendedTransaction { - OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.setTime(duration) != null + DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()?.setTime(duration) != null } - private fun OnTimeUserEntity.addBonusTime(time: Long) { + private fun DBOnTimeUser.addBonusTime(time: Long) { bonusTime += time logger.info("Added $time seconds to user $username (${id.value})") } override suspend fun addBonusTime(id: UUID, duration: Long): Boolean = newSuspendedTransaction { - OnTimeUserEntity.findById(id)?.addBonusTime(duration) != null + DBOnTimeUser.findById(id)?.addBonusTime(duration) != null } override suspend fun addBonusTime(username: String, duration: Long): Boolean = newSuspendedTransaction { - OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.addBonusTime(duration) != null + DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()?.addBonusTime(duration) != null } - private fun OnTimeUserEntity.setBonusTime(time: Long) { + private fun DBOnTimeUser.setBonusTime(time: Long) { bonusTime = time logger.info("Set bonus time of user $username (${id.value}) to $time") } override suspend fun setBonusTime(id: UUID, duration: Long): Boolean = newSuspendedTransaction { - OnTimeUserEntity.findById(id)?.setBonusTime(duration) != null + DBOnTimeUser.findById(id)?.setBonusTime(duration) != null } override suspend fun setBonusTime(username: String, duration: Long): Boolean = newSuspendedTransaction { - OnTimeUserEntity.find { OnTimeUserTable.username eq username }.firstOrNull()?.setBonusTime(duration) != null + DBOnTimeUser.find { OnTimeUsers.username eq username }.firstOrNull()?.setBonusTime(duration) != null } override suspend fun getById(id: UUID): OnTimeUser? = newSuspendedTransaction { - OnTimeUserEntity.findById(id)?.toOnTimeUser() + DBOnTimeUser.findById(id) } override suspend fun countAll(): Long = newSuspendedTransaction { - OnTimeUserEntity.all().count() + DBOnTimeUser.all().count() } override suspend fun exists(id: UUID): Boolean = newSuspendedTransaction { - OnTimeUserEntity.findById(id) != null + DBOnTimeUser.findById(id) != null } override suspend fun deleteById(id: UUID): Boolean = newSuspendedTransaction { - OnTimeUserTable.deleteWhere { OnTimeUserTable.id eq id } > 0 + OnTimeUsers.deleteWhere { OnTimeUsers.id eq id } > 0 } } diff --git a/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserTable.kt b/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUsers.kt similarity index 58% rename from core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserTable.kt rename to core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUsers.kt index 870fba9..540d6c0 100644 --- a/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUserTable.kt +++ b/core/src/main/kotlin/org/anvilpowered/ontime/core/db/OnTimeUsers.kt @@ -23,38 +23,22 @@ import org.jetbrains.exposed.dao.UUIDEntity import org.jetbrains.exposed.dao.UUIDEntityClass import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.dao.id.UUIDTable -import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.javatime.datetime import java.util.UUID -internal object OnTimeUserTable : UUIDTable("ontime_users") { +internal object OnTimeUsers : UUIDTable("ontime_users") { val username = varchar("username", 16).uniqueIndex() val createdUtc = datetime("created_utc") val playTime = long("play_time") val bonusTime = long("bonus_time") } -internal class OnTimeUserEntity(id: EntityID) : UUIDEntity(id) { - var username by OnTimeUserTable.username - var createdUtc by OnTimeUserTable.createdUtc - var playTime by OnTimeUserTable.playTime - var bonusTime by OnTimeUserTable.bonusTime +internal class DBOnTimeUser(id: EntityID) : UUIDEntity(id), OnTimeUser { + override val uuid: UUID = id.value + override var username by OnTimeUsers.username + override var createdUtc by OnTimeUsers.createdUtc + override var playTime by OnTimeUsers.playTime + override var bonusTime by OnTimeUsers.bonusTime - companion object : UUIDEntityClass(OnTimeUserTable) + companion object : UUIDEntityClass(OnTimeUsers) } - -internal fun OnTimeUserEntity.toOnTimeUser() = OnTimeUser( - id = this.id.value, - username = this.username, - createdUtc = this.createdUtc, - playTime = this.playTime, - bonusTime = this.bonusTime, -) - -internal fun ResultRow.toOnTimeUser() = OnTimeUser( - id = this[OnTimeUserTable.id].value, - username = this[OnTimeUserTable.username], - createdUtc = this[OnTimeUserTable.createdUtc], - playTime = this[OnTimeUserTable.playTime], - bonusTime = this[OnTimeUserTable.bonusTime], -)