diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index 103ace9..3ff7ffc 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -200,12 +200,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() } } 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 0170980..3fea20c 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 { @@ -352,11 +354,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