From 1aa6eb1329d02efe3b0b9acbd908d4300013ac74 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Mon, 13 May 2024 19:31:58 +0200 Subject: [PATCH] :sparkles: Remove cast UUID to String --- Sources/src/main/kotlin/allin/Application.kt | 2 +- .../data/postgres/PostgresBetDataSource.kt | 17 ++++++++--------- .../allin/data/postgres/PostgresDataSource.kt | 16 ++++++++-------- .../postgres/PostgresParticipationDataSource.kt | 11 +++++------ .../data/postgres/PostgresUserDataSource.kt | 3 +-- .../allin/data/postgres/entities/BetEntity.kt | 2 +- .../data/postgres/entities/BetResultEntity.kt | 8 +++----- .../postgres/entities/ParticipationEntity.kt | 5 ++--- .../data/postgres/entities/ResponseEntity.kt | 6 ++---- .../allin/data/postgres/entities/UserEntity.kt | 7 +++++-- .../kotlin/allin/routing/ParticipationRouter.kt | 2 +- 11 files changed, 37 insertions(+), 42 deletions(-) diff --git a/Sources/src/main/kotlin/allin/Application.kt b/Sources/src/main/kotlin/allin/Application.kt index 9b4f0f8..e9e3297 100644 --- a/Sources/src/main/kotlin/allin/Application.kt +++ b/Sources/src/main/kotlin/allin/Application.kt @@ -37,7 +37,7 @@ val Application.dataSource: AllInDataSource get() = allInDataSource fun main() { - embeddedServer(Netty, port = 8080, host = "0.0.0.0") { + embeddedServer(Netty, port = 10001, host = "0.0.0.0") { extracted() }.start(wait = true) } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index a16a74c..f43af2f 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -8,7 +8,6 @@ import org.ktorm.database.Database import org.ktorm.dsl.* import java.time.ZoneId import java.time.ZonedDateTime -import java.util.* class PostgresBetDataSource(private val database: Database) : BetDataSource { @@ -29,7 +28,7 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { if (type == BetType.CUSTOM) { database.from(ResponsesEntity) .select(response) - .where { ResponsesEntity.id eq UUID.fromString(idBet) } + .where { ResponsesEntity.id eq idBet } .map { it[response].toString() } } else { listOf(YES_VALUE, NO_VALUE) @@ -66,7 +65,7 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { override fun getBetById(id: String): Bet? = database.from(BetsEntity).select().where { - BetsEntity.id eq UUID.fromString(id) + BetsEntity.id eq id }.mapToBet().firstOrNull() override fun getBetsNotFinished(): List { @@ -93,14 +92,14 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } database.update(BetsEntity) { - where { BetsEntity.id eq UUID.fromString(betId) } + where { BetsEntity.id eq betId } set(BetsEntity.status, BetStatus.FINISHED) } database.from(ParticipationsEntity) .select() .where { - (ParticipationsEntity.betId eq UUID.fromString(betId)) and + (ParticipationsEntity.betId eq betId) and (ParticipationsEntity.answer eq result) } .forEach { participation -> @@ -167,7 +166,7 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { override fun addBet(bet: Bet) { database.insert(BetsEntity) { - set(it.id, UUID.fromString(bet.id)) + set(it.id, bet.id) set(it.endBet, bet.endBet.toInstant()) set(it.endRegistration, bet.endRegistration.toInstant()) set(it.sentenceBet, bet.sentenceBet) @@ -181,7 +180,7 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { if (bet.type == BetType.CUSTOM) { bet.response.forEach { selected -> database.insert(ResponsesEntity) { - set(it.id, UUID.fromString(bet.id)) + set(it.id, bet.id) set(it.response, selected) } } @@ -189,13 +188,13 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } override fun removeBet(id: String): Boolean { - return database.delete(BetsEntity) { it.id eq UUID.fromString(id) } > 0 + return database.delete(BetsEntity) { it.id eq id } > 0 } override fun updateBet(data: UpdatedBetData): Boolean { return database.update(BetsEntity) { set(BetsEntity.isPrivate, data.isPrivate) - where { BetsEntity.id eq UUID.fromString(data.id) } + where { BetsEntity.id eq data.id } } > 0 } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt index d9240e8..b0a4e7b 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt @@ -26,10 +26,10 @@ class PostgresDataSource : AllInDataSource() { database.Execute( """ CREATE TABLE IF not exists utilisateur ( - id uuid PRIMARY KEY, + id VARCHAR(255) PRIMARY KEY, username VARCHAR(255), password VARCHAR(255), - coins double precision, + coins int, email VARCHAR(255), lastgift timestamp ) @@ -39,7 +39,7 @@ class PostgresDataSource : AllInDataSource() { database.Execute( """ CREATE TABLE IF not exists bet ( - id uuid PRIMARY KEY, + id VARCHAR(255) PRIMARY KEY, theme VARCHAR(255), endregistration timestamp, endbet timestamp, @@ -55,7 +55,7 @@ class PostgresDataSource : AllInDataSource() { database.Execute( """ CREATE TABLE IF NOT EXISTS betresult ( - betid uuid PRIMARY KEY REFERENCES bet, + betid VARCHAR(255) PRIMARY KEY REFERENCES bet, result varchar(250) ) """.trimIndent() @@ -64,7 +64,7 @@ class PostgresDataSource : AllInDataSource() { database.Execute( """ CREATE TABLE IF NOT EXISTS betresultnotification ( - betid uuid, + betid VARCHAR(255), username varchar(250), CONSTRAINT pk_id_username PRIMARY KEY (betid, username) ) @@ -74,8 +74,8 @@ class PostgresDataSource : AllInDataSource() { database.Execute( """ CREATE TABLE IF NOT EXISTS participation ( - id uuid PRIMARY KEY, - bet uuid, + id VARCHAR(255) PRIMARY KEY, + bet VARCHAR(255), username varchar(250), answer varchar(250), stake int @@ -86,7 +86,7 @@ class PostgresDataSource : AllInDataSource() { database.Execute( """ CREATE TABLE IF NOT EXISTS response ( - id UUID, + id VARCHAR(255), response VARCHAR(250), CONSTRAINT pk_response_id PRIMARY KEY (id, response) ) diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt index bb14527..79b28f1 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt @@ -5,7 +5,6 @@ import allin.data.postgres.entities.ParticipationsEntity import allin.model.Participation import org.ktorm.database.Database import org.ktorm.dsl.* -import java.util.* class PostgresParticipationDataSource(private val database: Database) : ParticipationDataSource { @@ -22,8 +21,8 @@ class PostgresParticipationDataSource(private val database: Database) : Particip override fun addParticipation(participation: Participation) { database.insert(ParticipationsEntity) { - set(it.id, UUID.fromString(participation.id)) - set(it.betId, UUID.fromString(participation.betId)) + set(it.id, participation.id) + set(it.betId, participation.betId) set(it.username, participation.username) set(it.answer, participation.answer) set(it.stake, participation.stake) @@ -33,13 +32,13 @@ class PostgresParticipationDataSource(private val database: Database) : Particip override fun getParticipationFromBetId(betid: String): List = database.from(ParticipationsEntity) .select() - .where { ParticipationsEntity.betId eq UUID.fromString(betid) } + .where { ParticipationsEntity.betId eq betid } .mapToParticipation() override fun getParticipationFromUserId(username: String, betid: String): List = database.from(ParticipationsEntity) .select() - .where { (ParticipationsEntity.betId eq UUID.fromString(betid)) and (ParticipationsEntity.username eq username) } + .where { (ParticipationsEntity.betId eq betid) and (ParticipationsEntity.username eq username) } .mapToParticipation() fun getParticipationEntity(): List = @@ -47,6 +46,6 @@ class PostgresParticipationDataSource(private val database: Database) : Particip override fun deleteParticipation(id: String): Boolean = database.delete(ParticipationsEntity) { - it.id eq UUID.fromString(id) + it.id eq id } > 0 } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt index 69a90da..a29a2ce 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt @@ -9,7 +9,6 @@ import org.ktorm.database.Database import org.ktorm.database.use import org.ktorm.dsl.* import java.time.Instant.now -import java.util.* class PostgresUserDataSource(private val database: Database) : UserDataSource { override fun getUserByUsername(username: String): Pair = @@ -32,7 +31,7 @@ class PostgresUserDataSource(private val database: Database) : UserDataSource { override fun addUser(user: User) { database.insert(UsersEntity) { - set(it.id, UUID.fromString(user.id)) + set(it.id, user.id) set(it.nbCoins, user.nbCoins) set(it.username, user.username) set(it.password, user.password) diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/BetEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/BetEntity.kt index 6c82b1e..04a06e1 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/BetEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/BetEntity.kt @@ -19,7 +19,7 @@ interface BetEntity : Entity { } object BetsEntity : Table("bet") { - val id = uuid("id").primaryKey() + val id = varchar("id").primaryKey() val theme = varchar("theme").bindTo { it.theme } val sentenceBet = varchar("sentencebet").bindTo { it.sentenceBet } val endRegistration = timestamp("endregistration") diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/BetResultEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/BetResultEntity.kt index cbaafc3..c350e81 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/BetResultEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/BetResultEntity.kt @@ -2,9 +2,7 @@ package allin.data.postgres.entities import org.ktorm.entity.Entity import org.ktorm.schema.Table -import org.ktorm.schema.uuid import org.ktorm.schema.varchar -import java.util.* interface BetResultEntity : Entity { @@ -13,16 +11,16 @@ interface BetResultEntity : Entity { } object BetResultsEntity : Table("betresult") { - val betId = uuid("betid").primaryKey().references(BetsEntity) { it.bet } + val betId = varchar("betid").primaryKey().references(BetsEntity) { it.bet } val result = varchar("result").bindTo { it.result } } interface BetResultNotificationEntity : Entity { - val betId: UUID + val betId: String val username: String } object BetResultNotificationsEntity : Table("betresult") { - val betId = uuid("betid").primaryKey() + val betId = varchar("betid").primaryKey() val username = varchar("username").primaryKey() } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt index 6793c0c..7ae17c7 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt @@ -3,7 +3,6 @@ package allin.data.postgres.entities import org.ktorm.entity.Entity import org.ktorm.schema.Table import org.ktorm.schema.int -import org.ktorm.schema.uuid import org.ktorm.schema.varchar interface ParticipationEntity : Entity { @@ -16,8 +15,8 @@ interface ParticipationEntity : Entity { object ParticipationsEntity : Table("participation") { - val id = uuid("id").primaryKey() - val betId = uuid("bet").references(BetsEntity) { it.bet } + val id = varchar("id").primaryKey() + val betId = varchar("bet").references(BetsEntity) { it.bet } val username = varchar("username") val answer = varchar("answer") val stake = int("stake") diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/ResponseEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/ResponseEntity.kt index 4a826ef..7c291ed 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/ResponseEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/ResponseEntity.kt @@ -2,20 +2,18 @@ package allin.data.postgres.entities import org.ktorm.entity.Entity import org.ktorm.schema.Table -import org.ktorm.schema.uuid import org.ktorm.schema.varchar -import java.util.* const val YES_VALUE = "Yes" const val NO_VALUE = "No" interface ResponseEntity : Entity { - val betId: UUID + val betId: String val response: String } object ResponsesEntity : Table("response") { - val id = uuid("id").primaryKey() + val id = varchar("id").primaryKey() val response = varchar("response").primaryKey() } diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt index 62c5f26..3d97dbc 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt @@ -1,7 +1,10 @@ package allin.data.postgres.entities import org.ktorm.entity.Entity -import org.ktorm.schema.* +import org.ktorm.schema.Table +import org.ktorm.schema.int +import org.ktorm.schema.timestamp +import org.ktorm.schema.varchar interface UserEntity : Entity { val username: String @@ -11,7 +14,7 @@ interface UserEntity : Entity { } object UsersEntity : Table("utilisateur") { - val id = uuid("id").primaryKey() + val id = varchar("id").primaryKey() val username = varchar("username").bindTo { it.username } val password = varchar("password").bindTo { it.password } val nbCoins = int("coins").bindTo { it.nbCoins } diff --git a/Sources/src/main/kotlin/allin/routing/ParticipationRouter.kt b/Sources/src/main/kotlin/allin/routing/ParticipationRouter.kt index c4409bb..a588bf4 100644 --- a/Sources/src/main/kotlin/allin/routing/ParticipationRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/ParticipationRouter.kt @@ -52,7 +52,7 @@ fun Application.ParticipationRouter() { Participation( id = UUID.randomUUID().toString(), betId = participation.betId, - username = user.username, + username = user.id, answer = participation.answer, stake = participation.stake )