From fb87d8e7f972d1b52ce77e1cff8fefb43361789d Mon Sep 17 00:00:00 2001 From: avalin Date: Mon, 3 Jun 2024 11:32:48 +0200 Subject: [PATCH 1/2] Fix BetAnswerInfos --- .../data/postgres/PostgresBetDataSource.kt | 21 ++++++++++++++++--- .../PostgresParticipationDataSource.kt | 5 +++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index 58a553a..35c0fd1 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -197,12 +197,27 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } ) - if (bet.type == BetType.CUSTOM) { - bet.response.forEach { selected -> + val responses = if (bet.type == BetType.BINARY) { + listOf(YES_VALUE, NO_VALUE) + } else { + bet.response + } + + responses.forEach { response -> + database.betAnswerInfos.add( + BetAnswerInfoEntity { + this.betId = bet.id + this.response = response + this.totalStakes = 0 + this.odds = 1f + } + ) + + if (bet.type == BetType.CUSTOM) { database.responses.add( ResponseEntity { this.betId = bet.id - this.response = selected + this.response = response } ) } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt index c6b3b96..6a56aa3 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt @@ -34,8 +34,9 @@ class PostgresParticipationDataSource(private val database: Database) : Particip if (it.response == participation.answer) { it.totalStakes += participation.stake } - val probability = it.totalStakes / betInfo.totalStakes.toFloat() - it.odds = 1 / probability + + val probability = (it.totalStakes / betInfo.totalStakes.toFloat()) + it.odds = if (probability == 0f) 1f else 1 / probability it.flushChanges() } } From 4cfc3543e492e9a558458802814b29a30c50d173 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:54:41 +0200 Subject: [PATCH 2/2] :sparkles: add /bets/users routes --- .../postgres/entities/ParticipationEntity.kt | 2 +- .../main/kotlin/allin/routing/betRouter.kt | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) 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 222df52..1397233 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt @@ -28,7 +28,7 @@ interface ParticipationEntity : Entity { } object ParticipationsEntity : Table("participation") { - val id = varchar("id").primaryKey() + val id = varchar("id").primaryKey().bindTo { it.id } val betId = varchar("bet").references(BetsEntity) { it.bet } val username = varchar("username").bindTo { it.username } val answer = varchar("answer").bindTo { it.answer } diff --git a/Sources/src/main/kotlin/allin/routing/betRouter.kt b/Sources/src/main/kotlin/allin/routing/betRouter.kt index a3e4a72..edd7f69 100644 --- a/Sources/src/main/kotlin/allin/routing/betRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/betRouter.kt @@ -1,6 +1,7 @@ package allin.routing import allin.dataSource +import allin.dto.UserDTO import allin.ext.hasToken import allin.ext.verifyUserFromToken import allin.model.* @@ -21,6 +22,7 @@ val tokenManagerBet = AppConfig.tokenManager fun Application.betRouter() { val userDataSource = this.dataSource.userDataSource val betDataSource = this.dataSource.betDataSource + val participationDataSource = this.dataSource.participationDataSource val logManager = AppConfig.logManager routing { @@ -344,11 +346,38 @@ fun Application.betRouter() { logManager.log("Routing","UNAUTHORIZED /bets/confirm/{id}") call.respond(HttpStatusCode.Unauthorized) } - } } } + post("/bets/users", { + description = "gets all userDTO of a bet" + request { + headerParameter("JWT token of the logged user") + pathParameter("Id of the desired bet") + body> { + description = "UserDTO of the bet" + } + } + response { + HttpStatusCode.OK to { + description = "The final answer has been set" + } + HttpStatusCode.Unauthorized to { + description = "The user is not the creator of the bet" + } + } + }) { + logManager.log("Routing","POST /bets/users") + hasToken { principal -> + verifyUserFromToken(userDataSource, principal) { user, _ -> + val id = call.receive>()["id"] ?: "" + val participations = participationDataSource.getParticipationFromBetId(id) + val users = participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4).toList() + call.respond(users) + } + } } } +} } \ No newline at end of file