Add total stakes and total participants to bet reponse [db]
continuous-integration/drone/push Build is passing Details

pull/17/head
avalin 11 months ago
parent 59ef808123
commit deca69585f

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

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

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

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

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

@ -25,8 +25,9 @@ interface BetEntity : Entity<BetEntity> {
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<BetEntity> {
},
createdBy = createdBy,
popularityscore = popularityscore,
totalStakes = betInfo?.totalStakes ?: 0,
totalParticipants = betInfo?.totalParticipants ?: 0
)
}
fun toBetDetail(database: Database, username: String): BetDetail {
val bet = this.toBet(database)

@ -13,11 +13,13 @@ interface BetInfoEntity : Entity<BetInfoEntity> {
var id: String
var totalStakes: Int
var totalParticipants: Int
}
object BetInfosEntity : Table<BetInfoEntity>("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)

@ -20,7 +20,9 @@ data class Bet(
var isPrivate: Boolean,
var response: List<String>,
val createdBy: String = "",
var popularityscore: Int = 0
var popularityscore: Int = 0,
val totalStakes: Int = 0,
val totalParticipants: Int = 0
)
@Serializable

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

Loading…
Cancel
Save