Fix bet status update and createdBy
continuous-integration/drone/push Build is passing Details

pull/15/head
avalin 11 months ago
parent c6c0b67dd3
commit 772a615a4a

@ -1,15 +1,8 @@
package allin.data.mock package allin.data.mock
import allin.data.BetDataSource 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.*
import allin.model.BetStatus.* 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 java.time.ZonedDateTime
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -99,7 +92,7 @@ class MockBetDataSource(private val mockData: MockDataSource.MockData) : BetData
if (date >= bet.endBet) { if (date >= bet.endBet) {
bets[idx] = bet.copy(status = CLOSING) bets[idx] = bet.copy(status = CLOSING)
} else { } else {
bets[idx] = bet.copy(status = IN_PROGRESS) bets[idx] = bet.copy(status = WAITING)
} }
} }
} }

@ -60,12 +60,12 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
.map { it.toBet(database) } .map { it.toBet(database) }
} }
override fun getToConfirm(username: String): List<BetDetail> { override fun getToConfirm(id: String): List<BetDetail> {
return database.bets return database.bets
.filter { .filter {
(it.createdBy eq username) and (BetsEntity.status eq BetStatus.CLOSING) (it.createdBy eq id) and (BetsEntity.status eq BetStatus.CLOSING)
} }
.map { it.toBetDetail(database, username) } .map { it.toBetDetail(database, id) }
} }
override fun confirmBet(betId: String, result: String) { override fun confirmBet(betId: String, result: String) {
@ -144,11 +144,11 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
} }
override fun getMostPopularBet(): Bet? { override fun getMostPopularBet(): Bet? {
val max=database.bets.filter { (it.isPrivate eq false) and (it.status eq BetStatus.WAITING) }.maxBy { it.popularityscore } return database.bets
if(max!=null){ .filter { (it.isPrivate eq false) and (it.status eq BetStatus.IN_PROGRESS) }
return database.bets.filter { (it.popularityscore eq max) and (it.isPrivate eq false) and (it.status eq BetStatus.WAITING) }.map { it.toBet(database) }.first() .sortedByDescending { it.popularityscore }
} .firstOrNull()
return null ?.toBet(database)
} }
override fun updatePopularityScore(betId: String) { override fun updatePopularityScore(betId: String) {
@ -204,7 +204,7 @@ class PostgresBetDataSource(private val database: Database) : BetDataSource {
override fun updateBetStatuses(date: ZonedDateTime) { override fun updateBetStatuses(date: ZonedDateTime) {
database.update(BetsEntity) { database.update(BetsEntity) {
set(BetsEntity.status, BetStatus.IN_PROGRESS) set(BetsEntity.status, BetStatus.WAITING)
where { where {
(date.toInstant() greaterEq BetsEntity.endRegistration) and (date.toInstant() greaterEq BetsEntity.endRegistration) and
(date.toInstant() less BetsEntity.endBet) and (date.toInstant() less BetsEntity.endBet) and

@ -5,7 +5,6 @@ import org.ktorm.database.Database
import org.ktorm.dsl.eq import org.ktorm.dsl.eq
import org.ktorm.entity.* import org.ktorm.entity.*
import org.ktorm.schema.* import org.ktorm.schema.*
import org.postgresql.util.ByteConverter.numeric
import java.time.Instant import java.time.Instant
import java.time.ZoneId import java.time.ZoneId
import java.time.ZonedDateTime import java.time.ZonedDateTime

@ -1,6 +1,6 @@
package allin.model package allin.model
import allin.model.BetStatus.WAITING import allin.model.BetStatus.IN_PROGRESS
import allin.serializer.ZonedDateTimeSerializer import allin.serializer.ZonedDateTimeSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import java.time.ZonedDateTime import java.time.ZonedDateTime
@ -13,7 +13,7 @@ data class Bet(
val id: String = "", val id: String = "",
val theme: String, val theme: String,
val sentenceBet: String, val sentenceBet: String,
val status: BetStatus = WAITING, val status: BetStatus = IN_PROGRESS,
val type: BetType, val type: BetType,
@Serializable(ZonedDateTimeSerializer::class) val endRegistration: ZonedDateTime, @Serializable(ZonedDateTimeSerializer::class) val endRegistration: ZonedDateTime,
@Serializable(ZonedDateTimeSerializer::class) var endBet: ZonedDateTime, @Serializable(ZonedDateTimeSerializer::class) var endBet: ZonedDateTime,

@ -53,7 +53,7 @@ fun Application.betRouter() {
betDataSource.getBetById(id)?.let { betDataSource.getBetById(id)?.let {
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?.id.toString()) val betWithId = bet.copy(id = id, createdBy = user.first?.username.toString())
betDataSource.addBet(betWithId) betDataSource.addBet(betWithId)
call.respond(HttpStatusCode.Created, betWithId) call.respond(HttpStatusCode.Created, betWithId)
} }
@ -111,7 +111,7 @@ fun Application.betRouter() {
if (bet != null) { if (bet != null) {
call.respond(HttpStatusCode.Accepted, bet) call.respond(HttpStatusCode.Accepted, bet)
} }
call.respond(HttpStatusCode.NotFound,"Aucun bet n'a pu être récupérer") call.respond(HttpStatusCode.NotFound, "Aucun bet n'a pu être récupérer")
} }
} }
} }

Loading…
Cancel
Save