From a3aa69b07dfbe9f4f808a412a629b1bbcb68a129 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Wed, 22 May 2024 10:54:12 +0200 Subject: [PATCH] :sparkles: debug Popular bet --- .../main/kotlin/allin/data/BetDataSource.kt | 1 + .../allin/data/mock/MockBetDataSource.kt | 4 +++ .../data/postgres/PostgresBetDataSource.kt | 15 ++++++++++ .../allin/data/postgres/PostgresDataSource.kt | 29 ------------------- .../allin/routing/participationRouter.kt | 2 ++ 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/BetDataSource.kt b/Sources/src/main/kotlin/allin/data/BetDataSource.kt index ad94ffb..2e9ee1d 100644 --- a/Sources/src/main/kotlin/allin/data/BetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/BetDataSource.kt @@ -21,4 +21,5 @@ interface BetDataSource { fun getHistory(username: String): List fun getCurrent(username: String): List fun getMostPopularBet(): Bet? + fun updatePopularityScore(betId: String) } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt index 53278d5..d29b790 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt @@ -168,4 +168,8 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData override fun getMostPopularBet(): Bet? { TODO("Not yet implemented") } + + override fun updatePopularityScore(betId: String) { + TODO("Not yet implemented") + } } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index f554b03..76ea6a1 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -124,6 +124,21 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { return null } + override fun updatePopularityScore(betId: String) { + val bet = database.bets.filter { it.id eq betId }.firstOrNull() + if (bet == null) { + return + } + val participations = database.participations.filter { it.betId eq betId } + val stakes = participations.map { it.stake } + val score = (participations.count() * participations.count()) + stakes.sum() + database.update(BetsEntity) { + set(it.popularityscore, score) + where { it.id eq betId } + } + } + + override fun addBet(bet: Bet) { database.bets.add( BetEntity { diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt index 921d658..41b075b 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresDataSource.kt @@ -125,35 +125,6 @@ class PostgresDataSource : AllInDataSource() { ) """.trimIndent() ) - - database.execute(""" - CREATE OR REPLACE FUNCTION update_popularity_score() - RETURNS TRIGGER AS ${'$'}${'$'} - DECLARE - participant_count INT; - total_stakes INT; - BEGIN - -- Calculate participant count and total stakes for the bet - SELECT COUNT(*), COALESCE(SUM(stake), 0) INTO participant_count, total_stakes - FROM participation - WHERE bet = NEW.bet; - - -- Update the popularityscore in the bet table - UPDATE bet - SET popularityscore = (participant_count * participant_count + total_stakes) - WHERE id = NEW.bet; - - RETURN NEW; - END; - ${'$'}${'$'} LANGUAGE plpgsql; - - DROP TRIGGER IF EXISTS update_popularity_score ON participation; - - CREATE TRIGGER update_popularity_score - AFTER INSERT OR UPDATE ON participation - FOR EACH ROW - EXECUTE FUNCTION update_popularity_score(); - """.trimIndent()) } override val userDataSource: UserDataSource by lazy { PostgresUserDataSource(database) } diff --git a/Sources/src/main/kotlin/allin/routing/participationRouter.kt b/Sources/src/main/kotlin/allin/routing/participationRouter.kt index 43016b6..a6147f8 100644 --- a/Sources/src/main/kotlin/allin/routing/participationRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/participationRouter.kt @@ -22,6 +22,7 @@ fun Application.participationRouter() { val userDataSource = this.dataSource.userDataSource val participationDataSource = this.dataSource.participationDataSource + val betDataSource = this.dataSource.betDataSource routing { authenticate { @@ -59,6 +60,7 @@ fun Application.participationRouter() { ) userDataSource.removeCoins(username = user.username, amount = participation.stake) + betDataSource.updatePopularityScore(participation.betId) call.respond(HttpStatusCode.Created) } else { call.respond(HttpStatusCode.Forbidden, ApiMessage.NOT_ENOUGH_COINS)