|
|
@ -2,193 +2,146 @@ package allin.data.postgres
|
|
|
|
|
|
|
|
|
|
|
|
import allin.data.BetDataSource
|
|
|
|
import allin.data.BetDataSource
|
|
|
|
import allin.data.postgres.entities.*
|
|
|
|
import allin.data.postgres.entities.*
|
|
|
|
import allin.data.postgres.entities.ResponsesEntity.response
|
|
|
|
|
|
|
|
import allin.model.*
|
|
|
|
import allin.model.*
|
|
|
|
import org.ktorm.database.Database
|
|
|
|
import org.ktorm.database.Database
|
|
|
|
import org.ktorm.dsl.*
|
|
|
|
import org.ktorm.dsl.*
|
|
|
|
|
|
|
|
import org.ktorm.entity.*
|
|
|
|
import java.time.ZoneId
|
|
|
|
import java.time.ZoneId
|
|
|
|
import java.time.ZonedDateTime
|
|
|
|
import java.time.ZonedDateTime
|
|
|
|
|
|
|
|
|
|
|
|
class PostgresBetDataSource(private val database: Database) : BetDataSource {
|
|
|
|
class PostgresBetDataSource(private val database: Database) : BetDataSource {
|
|
|
|
|
|
|
|
|
|
|
|
private fun QueryRowSet.toBet() =
|
|
|
|
|
|
|
|
Bet(
|
|
|
|
|
|
|
|
id = this[BetsEntity.id].toString(),
|
|
|
|
|
|
|
|
theme = this[BetsEntity.theme].toString(),
|
|
|
|
|
|
|
|
sentenceBet = this[BetsEntity.sentenceBet].toString(),
|
|
|
|
|
|
|
|
endRegistration = this[BetsEntity.endRegistration]!!.atZone(ZoneId.of("Europe/Paris")),
|
|
|
|
|
|
|
|
endBet = this[BetsEntity.endBet]!!.atZone(ZoneId.of("Europe/Paris")),
|
|
|
|
|
|
|
|
isPrivate = this[BetsEntity.isPrivate] ?: false,
|
|
|
|
|
|
|
|
status = this[BetsEntity.status] ?: BetStatus.IN_PROGRESS,
|
|
|
|
|
|
|
|
type = this[BetsEntity.type] ?: BetType.CUSTOM,
|
|
|
|
|
|
|
|
createdBy = this[BetsEntity.createdBy].toString(),
|
|
|
|
|
|
|
|
response = let {
|
|
|
|
|
|
|
|
val idBet = this[BetsEntity.id].toString()
|
|
|
|
|
|
|
|
val type = this[BetsEntity.type] ?: BetType.CUSTOM
|
|
|
|
|
|
|
|
if (type == BetType.CUSTOM) {
|
|
|
|
|
|
|
|
database.from(ResponsesEntity)
|
|
|
|
|
|
|
|
.select(response)
|
|
|
|
|
|
|
|
.where { ResponsesEntity.id eq idBet }
|
|
|
|
|
|
|
|
.map { it[response].toString() }
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
listOf(YES_VALUE, NO_VALUE)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun QueryRowSet.toParticipation() =
|
|
|
|
|
|
|
|
Participation(
|
|
|
|
|
|
|
|
id = this[ParticipationsEntity.id]?.toString() ?: "",
|
|
|
|
|
|
|
|
betId = this[ParticipationsEntity.betId]?.toString() ?: "",
|
|
|
|
|
|
|
|
username = this[ParticipationsEntity.username] ?: "",
|
|
|
|
|
|
|
|
answer = this[ParticipationsEntity.answer] ?: "",
|
|
|
|
|
|
|
|
stake = this[ParticipationsEntity.stake] ?: 0
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun QueryRowSet.toBetResultDetail() =
|
|
|
|
|
|
|
|
BetResultDetail(
|
|
|
|
|
|
|
|
betResult = BetResult(
|
|
|
|
|
|
|
|
betId = this[BetResultsEntity.betId]?.toString() ?: "",
|
|
|
|
|
|
|
|
result = this[BetResultsEntity.result] ?: ""
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
bet = this.toBet(),
|
|
|
|
|
|
|
|
participation = this.toParticipation(),
|
|
|
|
|
|
|
|
amount = this[ParticipationsEntity.stake] ?: 0,
|
|
|
|
|
|
|
|
won = this[ParticipationsEntity.answer] == this[BetResultsEntity.result]
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun Query.mapToBet() = this.map { it.toBet() }
|
|
|
|
|
|
|
|
private fun Query.mapToBetResultDetail() = this.map { it.toBetResultDetail() }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun getAllBets(): List<Bet> =
|
|
|
|
override fun getAllBets(): List<Bet> =
|
|
|
|
database.from(BetsEntity).select().mapToBet()
|
|
|
|
database.bets.map { it.toBet(database) }
|
|
|
|
|
|
|
|
|
|
|
|
override fun getBetById(id: String): Bet? =
|
|
|
|
override fun getBetById(id: String): Bet? =
|
|
|
|
database.from(BetsEntity).select().where {
|
|
|
|
database.bets.find { it.id eq id }?.toBet(database)
|
|
|
|
BetsEntity.id eq id
|
|
|
|
|
|
|
|
}.mapToBet().firstOrNull()
|
|
|
|
override fun getBetDetailById(id: String, username: String): BetDetail? =
|
|
|
|
|
|
|
|
database.bets.find { it.id eq id }?.toBetDetail(database, username)
|
|
|
|
|
|
|
|
|
|
|
|
override fun getBetsNotFinished(): List<Bet> {
|
|
|
|
override fun getBetsNotFinished(): List<Bet> {
|
|
|
|
val currentTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"))
|
|
|
|
val currentTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"))
|
|
|
|
return database.from(BetsEntity)
|
|
|
|
return database.bets
|
|
|
|
.select()
|
|
|
|
.filter { it.endBet greaterEq currentTime.toInstant() }
|
|
|
|
.where { BetsEntity.endBet greaterEq currentTime.toInstant() }
|
|
|
|
.map { it.toBet(database) }
|
|
|
|
.mapToBet()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun getToConfirm(username: String): List<Bet> {
|
|
|
|
override fun getToConfirm(username: String): List<BetDetail> {
|
|
|
|
return database.from(BetsEntity)
|
|
|
|
return database.bets
|
|
|
|
.select()
|
|
|
|
.filter {
|
|
|
|
.where {
|
|
|
|
(it.createdBy eq username) and (BetsEntity.status eq BetStatus.CLOSING)
|
|
|
|
(BetsEntity.createdBy eq username) and
|
|
|
|
}
|
|
|
|
(BetsEntity.status eq BetStatus.CLOSING)
|
|
|
|
.map { it.toBetDetail(database, username) }
|
|
|
|
}.mapToBet()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun confirmBet(betId: String, result: String) {
|
|
|
|
override fun confirmBet(betId: String, result: String) {
|
|
|
|
database.insert(BetResultsEntity) {
|
|
|
|
database.bets.find { it.id eq betId }?.let { bet ->
|
|
|
|
set(it.betId, betId)
|
|
|
|
bet.status = BetStatus.FINISHED
|
|
|
|
set(it.result, result)
|
|
|
|
bet.flushChanges()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
database.update(BetsEntity) {
|
|
|
|
database.betResults.add(
|
|
|
|
where { BetsEntity.id eq betId }
|
|
|
|
BetResultEntity {
|
|
|
|
set(BetsEntity.status, BetStatus.FINISHED)
|
|
|
|
this.bet = bet
|
|
|
|
|
|
|
|
this.result = result
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
database.from(ParticipationsEntity)
|
|
|
|
database.participations.filter {
|
|
|
|
.select()
|
|
|
|
|
|
|
|
.where {
|
|
|
|
|
|
|
|
(ParticipationsEntity.betId eq betId) and
|
|
|
|
(ParticipationsEntity.betId eq betId) and
|
|
|
|
(ParticipationsEntity.answer eq result)
|
|
|
|
(ParticipationsEntity.answer eq result)
|
|
|
|
|
|
|
|
}.forEach {
|
|
|
|
|
|
|
|
database.betResultNotifications.add(
|
|
|
|
|
|
|
|
BetResultNotificationEntity {
|
|
|
|
|
|
|
|
this.betId = betId
|
|
|
|
|
|
|
|
this.username = it.username
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.forEach { participation ->
|
|
|
|
)
|
|
|
|
database.insert(BetResultNotificationsEntity) {
|
|
|
|
|
|
|
|
set(it.betId, betId)
|
|
|
|
|
|
|
|
set(it.username, participation[ParticipationsEntity.username])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun getWonNotifications(username: String): List<BetResultDetail> {
|
|
|
|
override fun getWonNotifications(username: String): List<BetResultDetail> {
|
|
|
|
return database.from(BetsEntity)
|
|
|
|
return database.betResultNotifications
|
|
|
|
.innerJoin(ParticipationsEntity, on = BetsEntity.id eq ParticipationsEntity.betId)
|
|
|
|
.filter { it.username eq username }
|
|
|
|
.innerJoin(BetResultsEntity, on = BetsEntity.id eq BetResultsEntity.betId)
|
|
|
|
.flatMap { notif ->
|
|
|
|
.innerJoin(BetResultNotificationsEntity, on = BetsEntity.id eq BetResultNotificationsEntity.betId)
|
|
|
|
notif.delete()
|
|
|
|
.select()
|
|
|
|
|
|
|
|
.where {
|
|
|
|
database.participations
|
|
|
|
(BetResultsEntity.result eq ParticipationsEntity.answer) and
|
|
|
|
.filter {
|
|
|
|
(ParticipationsEntity.username eq username)
|
|
|
|
(it.username eq username) and
|
|
|
|
}.let {
|
|
|
|
(it.betId eq notif.betId)
|
|
|
|
it.forEach { row ->
|
|
|
|
}
|
|
|
|
row[BetsEntity.id]?.let { betId ->
|
|
|
|
.mapNotNull { participation ->
|
|
|
|
database.delete(BetResultNotificationsEntity) {
|
|
|
|
database.betResults
|
|
|
|
(it.betId eq betId) and (it.username eq username)
|
|
|
|
.find { it.betId eq participation.bet.id }
|
|
|
|
}
|
|
|
|
?.toBetResultDetail(
|
|
|
|
|
|
|
|
database,
|
|
|
|
|
|
|
|
participation
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it
|
|
|
|
|
|
|
|
}.mapToBetResultDetail()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun getHistory(username: String): List<BetResultDetail> {
|
|
|
|
override fun getHistory(username: String): List<BetResultDetail> {
|
|
|
|
return database.from(BetsEntity)
|
|
|
|
return database.participations
|
|
|
|
.innerJoin(ParticipationsEntity, on = BetsEntity.id eq ParticipationsEntity.betId)
|
|
|
|
.filter { it.username eq username }
|
|
|
|
.innerJoin(BetResultsEntity, on = BetsEntity.id eq BetResultsEntity.betId)
|
|
|
|
.mapNotNull { participation ->
|
|
|
|
.select()
|
|
|
|
database.betResults
|
|
|
|
.where { ParticipationsEntity.username eq username }.mapToBetResultDetail()
|
|
|
|
.find { it.betId eq participation.bet.id }
|
|
|
|
|
|
|
|
?.toBetResultDetail(
|
|
|
|
|
|
|
|
database,
|
|
|
|
|
|
|
|
participation
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun getCurrent(username: String): List<BetDetail> {
|
|
|
|
override fun getCurrent(username: String): List<BetDetail> {
|
|
|
|
return database.from(BetsEntity)
|
|
|
|
return database.participations
|
|
|
|
.innerJoin(ParticipationsEntity, on = BetsEntity.id eq ParticipationsEntity.betId)
|
|
|
|
.filter { it.username eq username }
|
|
|
|
.select()
|
|
|
|
.mapNotNull {
|
|
|
|
.where {
|
|
|
|
if (it.bet.status !in listOf(BetStatus.FINISHED, BetStatus.CANCELLED)) {
|
|
|
|
(BetsEntity.status notEq BetStatus.FINISHED) and
|
|
|
|
it.bet.toBetDetail(
|
|
|
|
(BetsEntity.status notEq BetStatus.CANCELLED) and
|
|
|
|
database = database,
|
|
|
|
(ParticipationsEntity.username eq username)
|
|
|
|
username = username
|
|
|
|
}.map {
|
|
|
|
|
|
|
|
val participations = it[BetsEntity.id]?.let { betId ->
|
|
|
|
|
|
|
|
database.from(ParticipationsEntity)
|
|
|
|
|
|
|
|
.select().where { ParticipationsEntity.betId eq betId }.map { it.toParticipation() }
|
|
|
|
|
|
|
|
} ?: emptyList()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val bet = it.toBet()
|
|
|
|
|
|
|
|
BetDetail(
|
|
|
|
|
|
|
|
bet = bet,
|
|
|
|
|
|
|
|
answers = getBetAnswerDetail(bet, participations),
|
|
|
|
|
|
|
|
participations = participations,
|
|
|
|
|
|
|
|
userParticipation = it.toParticipation()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
} else null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun addBet(bet: Bet) {
|
|
|
|
override fun addBet(bet: Bet) {
|
|
|
|
database.insert(BetsEntity) {
|
|
|
|
database.bets.add(
|
|
|
|
set(it.id, bet.id)
|
|
|
|
BetEntity {
|
|
|
|
set(it.endBet, bet.endBet.toInstant())
|
|
|
|
this.id = bet.id
|
|
|
|
set(it.endRegistration, bet.endRegistration.toInstant())
|
|
|
|
this.endBet = bet.endBet.toInstant()
|
|
|
|
set(it.sentenceBet, bet.sentenceBet)
|
|
|
|
this.endRegistration = bet.endRegistration.toInstant()
|
|
|
|
set(it.theme, bet.theme)
|
|
|
|
this.zoneId = bet.endBet.zone.id
|
|
|
|
set(it.isPrivate, bet.isPrivate)
|
|
|
|
this.sentenceBet = bet.sentenceBet
|
|
|
|
set(it.createdBy, bet.createdBy)
|
|
|
|
this.theme = bet.theme
|
|
|
|
set(it.status, bet.status)
|
|
|
|
this.isPrivate = bet.isPrivate
|
|
|
|
set(it.type, bet.type)
|
|
|
|
this.createdBy = bet.createdBy
|
|
|
|
|
|
|
|
this.status = bet.status
|
|
|
|
|
|
|
|
this.type = bet.type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (bet.type == BetType.CUSTOM) {
|
|
|
|
if (bet.type == BetType.CUSTOM) {
|
|
|
|
bet.response.forEach { selected ->
|
|
|
|
bet.response.forEach { selected ->
|
|
|
|
database.insert(ResponsesEntity) {
|
|
|
|
database.responses.add(
|
|
|
|
set(it.id, bet.id)
|
|
|
|
ResponseEntity {
|
|
|
|
set(it.response, selected)
|
|
|
|
this.betId = bet.id
|
|
|
|
|
|
|
|
this.response = selected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun removeBet(id: String): Boolean {
|
|
|
|
override fun removeBet(id: String): Boolean {
|
|
|
|
return database.delete(BetsEntity) { it.id eq id } > 0
|
|
|
|
database.betInfos.removeIf { it.id eq id }
|
|
|
|
|
|
|
|
database.betAnswerInfos.removeIf { it.betId eq id }
|
|
|
|
|
|
|
|
return database.bets.removeIf { it.id eq id } > 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun updateBet(data: UpdatedBetData): Boolean {
|
|
|
|
override fun updateBet(data: UpdatedBetData): Boolean {
|
|
|
|