From deca69585fac5e1b524559a50d165752eb1d21c5 Mon Sep 17 00:00:00 2001 From: avalin Date: Sat, 1 Jun 2024 14:24:28 +0200 Subject: [PATCH] Add total stakes and total participants to bet reponse [db] --- .../main/kotlin/allin/data/mock/MockBetDataSource.kt | 2 +- .../allin/data/mock/MockParticipationDataSource.kt | 12 ++++++++++-- .../allin/data/postgres/PostgresBetDataSource.kt | 8 ++++++++ .../kotlin/allin/data/postgres/PostgresDataSource.kt | 3 ++- .../data/postgres/PostgresParticipationDataSource.kt | 3 +++ .../kotlin/allin/data/postgres/entities/BetEntity.kt | 8 ++++++-- .../allin/data/postgres/entities/BetInfoEntity.kt | 2 ++ Sources/src/main/kotlin/allin/model/Bet.kt | 4 +++- Sources/src/main/kotlin/allin/model/BetInfo.kt | 5 +++-- 9 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt index 3e017d3..2f8c34c 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt @@ -73,7 +73,7 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData override fun addBet(bet: Bet) { bets += bet - betInfos += BetInfo(id = bet.id, totalStakes = 0) + betInfos += BetInfo(id = bet.id, totalStakes = 0, totalParticipants = 0) bet.response.forEach { answerInfos += BetAnswerInfo( betId = bet.id, diff --git a/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt index 68701f3..3c1a8d0 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt @@ -15,7 +15,11 @@ class MockParticipationDataSource(private val mockData: MockDataSource.MockData) betInfos.replaceAll { if (participation.betId == it.id) { betTotalStakes = it.totalStakes + participation.stake - it.copy(totalStakes = betTotalStakes) + val betTotalParticipants = it.totalParticipants + 1 + it.copy( + totalStakes = betTotalStakes, + totalParticipants = betTotalParticipants + ) } else { it } @@ -54,7 +58,11 @@ class MockParticipationDataSource(private val mockData: MockDataSource.MockData) betInfos.replaceAll { if (participation?.betId == it.id) { betTotalStakes = it.totalStakes - participation.stake - it.copy(totalStakes = betTotalStakes) + val betTotalParticipants = it.totalParticipants - 1 + it.copy( + totalStakes = betTotalStakes, + totalParticipants = betTotalParticipants + ) } else { it } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index f68cccc..58a553a 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -189,6 +189,14 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } ) + database.betInfos.add( + BetInfoEntity { + this.id = bet.id + this.totalStakes = 0 + this.totalParticipants = 0 + } + ) + if (bet.type == BetType.CUSTOM) { bet.response.forEach { selected -> database.responses.add( diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt index 9548fbb..3f3e52a 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt @@ -107,7 +107,8 @@ class PostgresDataSource : AllInDataSource() { """ CREATE TABLE IF not exists betInfo ( id VARCHAR(255) PRIMARY KEY, - totalStakes int + totalStakes int, + totalParticipants int ) """.trimIndent() ) diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt index f7d9a29..6c3ae39 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt @@ -23,9 +23,11 @@ class PostgresParticipationDataSource(private val database: Database) : Particip val betInfo = database.betInfos.find { it.id eq participation.betId } ?: BetInfoEntity { this.id = participation.betId this.totalStakes = 0 + this.totalParticipants = 0 } betInfo.totalStakes += participation.stake + betInfo.totalParticipants++ database.betInfos.update(betInfo) database.betAnswerInfos.filter { it.betId eq participation.betId }.forEach { @@ -50,6 +52,7 @@ class PostgresParticipationDataSource(private val database: Database) : Particip val participation = database.participations.find { it.id eq id } ?: return false database.betInfos.find { it.id eq participation.bet.id }?.let { betInfo -> betInfo.totalStakes -= participation.stake + betInfo.totalParticipants-- database.betAnswerInfos.filter { it.betId eq participation.bet.id }.forEach { if (it.response == participation.answer) { 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 90f9509..e1c9e43 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/BetEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/BetEntity.kt @@ -25,8 +25,9 @@ interface BetEntity : Entity { var createdBy: String var popularityscore: Int - fun toBet(database: Database) = - Bet( + fun toBet(database: Database): Bet { + val betInfo = database.betInfos.find { it.id eq this.id } + return Bet( id = id, theme = theme, sentenceBet = sentenceBet, @@ -42,7 +43,10 @@ interface BetEntity : Entity { }, createdBy = createdBy, popularityscore = popularityscore, + totalStakes = betInfo?.totalStakes ?: 0, + totalParticipants = betInfo?.totalParticipants ?: 0 ) + } fun toBetDetail(database: Database, username: String): BetDetail { val bet = this.toBet(database) diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/BetInfoEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/BetInfoEntity.kt index fd2e664..ef7df77 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/BetInfoEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/BetInfoEntity.kt @@ -13,11 +13,13 @@ interface BetInfoEntity : Entity { var id: String var totalStakes: Int + var totalParticipants: Int } object BetInfosEntity : Table("betinfo") { val id = varchar("id").primaryKey().bindTo { it.id } val totalStakes = int("totalstakes").bindTo { it.totalStakes } + val totalParticipants = int("totalparticipants").bindTo { it.totalParticipants } } val Database.betInfos get() = this.sequenceOf(BetInfosEntity) diff --git a/Sources/src/main/kotlin/allin/model/Bet.kt b/Sources/src/main/kotlin/allin/model/Bet.kt index 3012f58..e48a6d6 100644 --- a/Sources/src/main/kotlin/allin/model/Bet.kt +++ b/Sources/src/main/kotlin/allin/model/Bet.kt @@ -20,7 +20,9 @@ data class Bet( var isPrivate: Boolean, var response: List, val createdBy: String = "", - var popularityscore: Int = 0 + var popularityscore: Int = 0, + val totalStakes: Int = 0, + val totalParticipants: Int = 0 ) @Serializable diff --git a/Sources/src/main/kotlin/allin/model/BetInfo.kt b/Sources/src/main/kotlin/allin/model/BetInfo.kt index a904755..474850d 100644 --- a/Sources/src/main/kotlin/allin/model/BetInfo.kt +++ b/Sources/src/main/kotlin/allin/model/BetInfo.kt @@ -4,8 +4,9 @@ import kotlinx.serialization.Serializable @Serializable data class BetInfo( - val id: String, - val totalStakes: Int, + var id: String, + var totalStakes: Int, + var totalParticipants: Int ) @Serializable