11package wayzer.user
22
3- import org.jetbrains.exposed.dao.IntEntity
4- import org.jetbrains.exposed.dao.IntEntityClass
5- import org.jetbrains.exposed.dao.id.EntityID
63import org.jetbrains.exposed.dao.id.IntIdTable
7- import org.jetbrains.exposed.sql.SqlExpressionBuilder.greater
8- import org.jetbrains.exposed.sql.and
4+ import org.jetbrains.exposed.sql.*
95import org.jetbrains.exposed.sql.javatime.CurrentTimestamp
106import org.jetbrains.exposed.sql.javatime.timestamp
7+ import org.jetbrains.exposed.sql.transactions.transaction
118import wayzer.lib.PlayerData
129import java.time.Duration
1310import java.time.Instant
1411
15- class PlayerBan (id : EntityID <Int >) : IntEntity(id) {
16- var ids by T .ids
17- var reason by T .reason
18- var operator by T .operator
19- val createTime by T .createTime
20- var endTime by T .endTime
12+ data class PlayerBan (
13+ val id : Int ,
14+ val ids : String ,
15+ val reason : String ,
16+ val operator : String? ,
17+ val createTime : Instant ,
18+ val endTime : Instant
19+ ) {
20+ constructor (row: ResultRow ) : this (
21+ row[T .id].value,
22+ row[T .ids],
23+ row[T .reason],
24+ row[T .operator ],
25+ row[T .createTime],
26+ row[T .endTime]
27+ )
2128
2229 object T : IntIdTable(" PlayerBanV2" ) {
2330 val ids = text(" ids" , eagerLoading = true )
@@ -27,21 +34,30 @@ class PlayerBan(id: EntityID<Int>) : IntEntity(id) {
2734 val endTime = timestamp(" endTime" ).defaultExpression(CurrentTimestamp )
2835 }
2936
30- companion object : IntEntityClass < PlayerBan >( T ) {
31- fun create (ids : PlayerData , time : Duration , reason : String , operator : String? ): PlayerBan {
32- return new {
33- this .ids = ids.idsInDB
34- endTime = Instant .now() + time
35- this .operator = operator
36- this .reason = reason
37+ companion object {
38+ fun create (ids : PlayerData , time : Duration , reason : String , operator : String? ): PlayerBan = transaction {
39+ val ban = T .insertReturning {
40+ it[ T .ids] = ids.idsInDB
41+ it[ T . endTime] = Instant .now() + time
42+ it[ T .operator ] = operator
43+ it[ T .reason] = reason
3744 }
45+ PlayerBan (ban.first())
3846 }
3947
40- fun allNotEnd () = find(T .endTime.greater(CurrentTimestamp ))
48+ fun allNotEnd () = transaction {
49+ T .selectAll().where { T .endTime.greater(CurrentTimestamp ) }
50+ .map { PlayerBan (it) }
51+ }
52+
53+ fun findNotEnd (id : String ): PlayerBan ? = transaction {
54+ T .selectAll().where { (T .ids like " %$${id} $%" ) and T .endTime.greater(CurrentTimestamp ) }.firstOrNull()
55+ ?.let { PlayerBan (it) }
56+ }
4157
42- fun findNotEnd (id : String ): PlayerBan ? {
43- return find { ( T .ids like " %$ ${id} $% " ) and ( T .endTime.greater( CurrentTimestamp )) }
44- .firstOrNull()
58+ fun delete (id : Int ): PlayerBan ? = transaction {
59+ T .deleteReturning { T .id eq id }.firstOrNull()
60+ ?. let { PlayerBan (it) }
4561 }
4662 }
4763}
0 commit comments