Refactor and add PopularityScore for Mocking Data Source
continuous-integration/drone/push Build is failing Details

pull/13/head
luevard 8 months ago
parent a3aa69b07d
commit 75666ad2a3

@ -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
}
}

@ -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 {

@ -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

@ -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(

Loading…
Cancel
Save