BetDetail_Implementation #5
Merged
lucas.evard
merged 5 commits from BetDetail_Implementation
into master
10 months ago
@ -1,4 +1,4 @@
|
||||
package allin.dto
|
||||
import kotlinx.serialization.Serializable
|
||||
@Serializable
|
||||
data class UserDTO(val id: String, val username: String, val email: String, val nbCoins: Double, var token:String?)
|
||||
data class UserDTO(val id: String, val username: String, val email: String, val nbCoins: Int, var token:String?)
|
||||
|
@ -0,0 +1,90 @@
|
||||
package allin.entities
|
||||
|
||||
import allin.database
|
||||
import allin.model.Participation
|
||||
import allin.utils.Execute
|
||||
import org.ktorm.dsl.*
|
||||
import org.ktorm.entity.Entity
|
||||
import org.ktorm.schema.*
|
||||
import java.util.*
|
||||
|
||||
interface ParticipationEntity : Entity<ParticipationEntity> {
|
||||
val id: String
|
||||
val betId: String
|
||||
val username: String
|
||||
val answer: String
|
||||
val stake: Int
|
||||
}
|
||||
|
||||
|
||||
object ParticipationsEntity : Table<BetEntity>("participation") {
|
||||
val id = uuid("id").primaryKey()
|
||||
val betId = uuid("bet")
|
||||
val username = varchar("username")
|
||||
val answer = varchar("answer")
|
||||
val stake = int("stake")
|
||||
|
||||
fun createParticipationTable(){
|
||||
val request="CREATE TABLE IF NOT EXISTS participation (id uuid PRIMARY KEY,bet uuid,username varchar(250),answer varchar(250),stake int);"
|
||||
database.Execute(request)
|
||||
}
|
||||
|
||||
fun addParticipationEntity(participation : Participation){
|
||||
database.insert(ParticipationsEntity){
|
||||
set(it.id, UUID.fromString(participation.id))
|
||||
set(it.betId,UUID.fromString(participation.betId))
|
||||
set(it.username,participation.username)
|
||||
set(it.answer,participation.answer)
|
||||
set(it.stake,participation.stake)
|
||||
}
|
||||
}
|
||||
|
||||
fun getParticipationEntityFromBetId(betid: String): MutableList<Participation> {
|
||||
return database.from(ParticipationsEntity)
|
||||
.select()
|
||||
.where { betId eq UUID.fromString(betid) }
|
||||
.map { row ->
|
||||
Participation(
|
||||
row[id].toString(),
|
||||
row[betId].toString(),
|
||||
row[username].toString(),
|
||||
row[answer].toString(),
|
||||
row[stake] ?: 0,
|
||||
)
|
||||
}.toMutableList()
|
||||
}
|
||||
|
||||
fun getParticipationEntityFromUserId(user: String, betid: String): MutableList<Participation> {
|
||||
return database.from(ParticipationsEntity)
|
||||
.select()
|
||||
.where { (betId eq UUID.fromString(betid)) and (username eq user) }
|
||||
.map { row ->
|
||||
Participation(
|
||||
row[id].toString(),
|
||||
row[betId].toString(),
|
||||
row[username].toString(),
|
||||
row[answer].toString(),
|
||||
row[stake] ?: 0,
|
||||
)
|
||||
}.toMutableList()
|
||||
}
|
||||
|
||||
fun getParticipationEntity(): MutableList<Participation> {
|
||||
return database.from(ParticipationsEntity).select().map {
|
||||
row -> Participation(
|
||||
row[id].toString(),
|
||||
row[betId].toString(),
|
||||
row[username].toString(),
|
||||
row[answer].toString(),
|
||||
row[stake]?:0,
|
||||
)
|
||||
}.toMutableList()
|
||||
}
|
||||
|
||||
fun deleteParticipation(participation: Participation): Boolean {
|
||||
val deletedCount = database.delete(ParticipationsEntity) {
|
||||
it.id eq UUID.fromString(participation.id)
|
||||
}
|
||||
return deletedCount > 0
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package allin.model
|
||||
|
||||
import allin.dto.UserDTO
|
||||
data class BetAction(val id:Int, val coins: Int, val user: String, val bet: Int)
|
||||
data class BetActionCompleted(val id:Int, val coins: Int, val user: UserDTO, val bet: Bet)
|
@ -0,0 +1,34 @@
|
||||
package allin.model
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class BetAnswerDetail(
|
||||
val response: String, // La réponse (ex: "Yes", "No" etc...)
|
||||
val totalStakes: Int, // Le nombre total d'argent misé sur cette réponse
|
||||
val totalParticipants: Int, // Le nombre total de participant
|
||||
val highestStake: Int, // Plus grosse mise
|
||||
val odds: Float // Cote du bet
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class BetDetail(
|
||||
val bet: Bet, // Le Bet
|
||||
val answers: List<BetAnswerDetail>?, // Pour chaque réponse possible du bet les détails
|
||||
val participations: List<Participation>?, // La liste des participations
|
||||
val userParticipation: Participation? // La participation du User current
|
||||
)
|
||||
|
||||
fun getBetAnswerDetail(bet: Bet, participations: List<Participation>): List<BetAnswerDetail> {
|
||||
return bet.response.map { response ->
|
||||
val responseParticipations = participations.filter { it.answer == response }
|
||||
BetAnswerDetail(
|
||||
response = response,
|
||||
totalStakes = responseParticipations.sumOf { it.stake },
|
||||
totalParticipants = responseParticipations.size,
|
||||
highestStake = responseParticipations.maxOfOrNull { it.stake } ?: 0,
|
||||
odds = if (participations.isEmpty()) 1f else responseParticipations.size / participations.size.toFloat()
|
||||
)
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package allin.routing
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
fun Application.BetActionRouter(){
|
||||
routing {
|
||||
route("/BetAction/add"){
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package allin.routing
|
||||
|
||||
import allin.entities.BetsEntity.getBets
|
||||
import allin.entities.ParticipationsEntity.getParticipationEntityFromBetId
|
||||
import allin.entities.ParticipationsEntity.getParticipationEntityFromUserId
|
||||
import allin.ext.hasToken
|
||||
import allin.ext.verifyUserFromToken
|
||||
import allin.model.BetDetail
|
||||
import allin.model.getBetAnswerDetail
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
fun Application.BetDetailRouter() {
|
||||
routing {
|
||||
authenticate {
|
||||
get("/betdetail/get/{id}") {
|
||||
hasToken { principal ->
|
||||
verifyUserFromToken(principal) { user, _ ->
|
||||
val id = call.parameters["id"].toString()
|
||||
val participations = getParticipationEntityFromBetId(id)
|
||||
val selectedBet = getBets().find { it.id == id }
|
||||
if (selectedBet != null) {
|
||||
call.respond(
|
||||
HttpStatusCode.Accepted,
|
||||
BetDetail(
|
||||
selectedBet,
|
||||
getBetAnswerDetail(selectedBet, participations),
|
||||
participations.toList(),
|
||||
getParticipationEntityFromUserId(user.username, id).lastOrNull()
|
||||
)
|
||||
)
|
||||
} else {
|
||||
call.respond(HttpStatusCode.NotFound, "Bet not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue