From 75666ad2a3f508638e3ec8ab0e8d996ca0daf9b9 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Wed, 22 May 2024 11:15:02 +0200 Subject: [PATCH] :sparkles: Refactor and add PopularityScore for Mocking Data Source --- .../allin/data/mock/MockBetDataSource.kt | 18 ++++++++++++++---- .../data/postgres/PostgresBetDataSource.kt | 15 ++------------- Sources/src/main/kotlin/allin/model/Bet.kt | 3 ++- .../allin/routing/participationRouter.kt | 5 +++++ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt index d29b790..0efd3e5 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockBetDataSource.kt @@ -1,8 +1,15 @@ package allin.data.mock import allin.data.BetDataSource +import allin.data.postgres.entities.BetsEntity +import allin.data.postgres.entities.bets +import allin.data.postgres.entities.participations import allin.model.* import allin.model.BetStatus.* +import org.ktorm.dsl.and +import org.ktorm.dsl.eq +import org.ktorm.dsl.update +import org.ktorm.entity.* import java.time.ZonedDateTime import kotlin.math.roundToInt @@ -165,11 +172,14 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData ) } - override fun getMostPopularBet(): Bet? { - TODO("Not yet implemented") - } + override fun getMostPopularBet() = + mockData.bets.filter { !it.isPrivate && it.status == WAITING }.maxBy { it.popularityscore } override fun updatePopularityScore(betId: String) { - TODO("Not yet implemented") + val bet = mockData.bets.firstOrNull { it.id == betId } ?: return + val participations = mockData.participations.filter { it.betId == betId } + val score = participations.size * participations.size + participations.sumOf { it.stake } + bet.popularityscore = score } + } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt index 76ea6a1..08f94fd 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresBetDataSource.kt @@ -111,13 +111,7 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } override fun getMostPopularBet(): Bet? { - - database.bets.forEach { - println(it) - } - val max=database.bets.filter { (it.isPrivate eq false) and (it.status eq BetStatus.WAITING) }.maxBy { it.popularityscore } - println(max) if(max!=null){ return database.bets.filter { (it.popularityscore eq max) and (it.isPrivate eq false) and (it.status eq BetStatus.WAITING) }.map { it.toBet(database) }.first() } @@ -125,20 +119,15 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource { } override fun updatePopularityScore(betId: String) { - val bet = database.bets.filter { it.id eq betId }.firstOrNull() - if (bet == null) { - return - } + database.bets.filter { it.id eq betId }.firstOrNull() ?: return val participations = database.participations.filter { it.betId eq betId } - val stakes = participations.map { it.stake } - val score = (participations.count() * participations.count()) + stakes.sum() + val score = (participations.count() * participations.count()) + participations.map { it.stake }.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/model/Bet.kt b/Sources/src/main/kotlin/allin/model/Bet.kt index 23eb638..07a8360 100644 --- a/Sources/src/main/kotlin/allin/model/Bet.kt +++ b/Sources/src/main/kotlin/allin/model/Bet.kt @@ -19,7 +19,8 @@ data class Bet( @Serializable(ZonedDateTimeSerializer::class) var endBet: ZonedDateTime, var isPrivate: Boolean, var response: List<String>, - val createdBy: String = "" + val createdBy: String = "", + var popularityscore: Int ) @Serializable diff --git a/Sources/src/main/kotlin/allin/routing/participationRouter.kt b/Sources/src/main/kotlin/allin/routing/participationRouter.kt index a6147f8..1e0aa8c 100644 --- a/Sources/src/main/kotlin/allin/routing/participationRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/participationRouter.kt @@ -48,6 +48,11 @@ fun Application.participationRouter() { hasToken { principal -> val participation = call.receive<ParticipationRequest>() verifyUserFromToken(userDataSource, principal) { user, _ -> + + if(betDataSource.getBetById(participation.betId)== null){ + call.respond(HttpStatusCode.NotFound, ApiMessage.BET_NOT_FOUND) + } + if (user.nbCoins >= participation.stake) { participationDataSource.addParticipation( Participation(