Add Private Bet
continuous-integration/drone/push Build is passing Details

pull/18/head
luevard 11 months ago
parent 90b099183c
commit 0d9e0971f8

@ -19,4 +19,5 @@ interface BetDataSource {
fun getCurrent(username: String): List<BetDetail> fun getCurrent(username: String): List<BetDetail>
fun getMostPopularBet(): Bet? fun getMostPopularBet(): Bet?
fun updatePopularityScore(betId: String) fun updatePopularityScore(betId: String)
fun addPrivateBet(bet: Bet)
} }

@ -233,4 +233,8 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData
bet.popularityscore = score bet.popularityscore = score
} }
override fun addPrivateBet(bet: Bet) {
TODO()
}
} }

@ -253,5 +253,13 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
} }
} }
} }
override fun addPrivateBet(bet: Bet) {
addBet(bet)
bet.userInvited?.forEach{
database.privatebets.add(PrivateBetEntity{
betId=bet.id
userId=it
})
}
}
} }

@ -144,6 +144,17 @@ class PostgresDataSource : AllInDataSource() {
) )
""".trimIndent() """.trimIndent()
) )
database.execute(
"""
CREATE TABLE IF NOT EXISTS privatebet
(
betid VARCHAR(255),
userid VARCHAR(255),
CONSTRAINT pk_privatebet PRIMARY KEY (betid,userid)
)
""".trimIndent()
)
} }
override val userDataSource: UserDataSource by lazy { PostgresUserDataSource(database) } override val userDataSource: UserDataSource by lazy { PostgresUserDataSource(database) }

@ -0,0 +1,21 @@
package allin.data.postgres.entities
import org.ktorm.database.Database
import org.ktorm.entity.Entity
import org.ktorm.entity.sequenceOf
import org.ktorm.schema.Table
import org.ktorm.schema.varchar
interface PrivateBetEntity : Entity<PrivateBetEntity> {
companion object : Entity.Factory<PrivateBetEntity>()
var betId: String
var userId: String
}
object PrivateBetsEntity : Table<PrivateBetEntity>("privatebet") {
val betid = varchar("betid").bindTo { it.betId }
val userId = varchar("userid").bindTo { it.userId }
}
val Database.privatebets get() = this.sequenceOf(PrivateBetsEntity)

@ -22,7 +22,8 @@ data class Bet(
val createdBy: String = "", val createdBy: String = "",
var popularityscore: Int = 0, var popularityscore: Int = 0,
val totalStakes: Int = 0, val totalStakes: Int = 0,
val totalParticipants: Int = 0 val totalParticipants: Int = 0,
val userInvited: List<String>? = null
) )
@Serializable @Serializable

@ -57,7 +57,10 @@ fun Application.betRouter() {
call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST) call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST)
} ?: run { } ?: run {
val betWithId = bet.copy(id = id, createdBy = user.first?.username.toString()) val betWithId = bet.copy(id = id, createdBy = user.first?.username.toString())
betDataSource.addBet(betWithId)
if(bet.isPrivate && bet.userInvited?.isNotEmpty() == true){
betDataSource.addPrivateBet(betWithId)
} else betDataSource.addBet(betWithId)
logManager.log("Routing","CREATED /bets/add\t${betWithId}") logManager.log("Routing","CREATED /bets/add\t${betWithId}")
call.respond(HttpStatusCode.Created, betWithId) call.respond(HttpStatusCode.Created, betWithId)
} }

Loading…
Cancel
Save