ParticipationEntity creation
continuous-integration/drone/push Build is passing Details

pull/5/head
luevard 10 months ago
parent d4483be0d3
commit 20ce4c152b

@ -1,9 +1,6 @@
package allin
import allin.entities.BetEntity
import allin.entities.BetsEntity
import allin.entities.ResponsesEntity
import allin.entities.UsersEntity
import allin.entities.*
import allin.routing.BasicRouting
import allin.routing.BetRouter
import allin.routing.ParticipationRouter
@ -57,4 +54,5 @@ private fun Application.extracted() {
UsersEntity.createUserTable()
BetsEntity.createBetsTable()
ResponsesEntity.createResponseTable()
ParticipationsEntity.createParticipationTable()
}

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

@ -14,13 +14,13 @@ interface UserEntity : Entity<UserEntity> {
val username: String
var email: String
var password: String
var nbCoins: Double
var nbCoins: Int
}
object UsersEntity : Table<UserEntity>("utilisateur") {
val id = uuid("id").primaryKey()
val username = varchar("username")
val password = varchar("password")
val nbCoins = double("coins")
val nbCoins = int("coins")
val email = varchar("email")
fun getUserToUserDTO(): MutableList<UserDTO> {
@ -29,7 +29,7 @@ object UsersEntity : Table<UserEntity>("utilisateur") {
row[id].toString(),
row[username].toString(),
row[email].toString(),
row[nbCoins]?:0.0,
row[nbCoins]?:0,
null
)
}.toMutableList()
@ -51,7 +51,7 @@ object UsersEntity : Table<UserEntity>("utilisateur") {
row[id].toString(),
row[username].toString(),
row[email].toString(),
row[nbCoins] ?: 0.0,
row[nbCoins]?:0,
null
),
row[password].toString()

@ -0,0 +1,16 @@
package allin.model
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
)
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
)

@ -2,6 +2,8 @@ package allin.routing
import allin.entities.BetsEntity.addBetEntity
import allin.entities.BetsEntity.getBets
import allin.entities.ParticipationsEntity
import allin.entities.ParticipationsEntity.getParticipationEntity
import allin.ext.hasToken
import allin.ext.verifyUserFromToken
import allin.model.ApiMessage
@ -100,7 +102,7 @@ fun Application.BetRouter() {
val bets= getBets()
hasToken { principal ->
verifyUserFromToken(principal) { user, _ ->
val bets = participations
val bets = getParticipationEntity()
.filter { it.username == user.username }
.mapNotNull { itParticipation -> bets.find { it.id == itParticipation.betId } }
call.respond(HttpStatusCode.OK, bets)

@ -1,5 +1,8 @@
package allin.routing
import allin.entities.ParticipationsEntity.addParticipationEntity
import allin.entities.ParticipationsEntity.deleteParticipation
import allin.entities.ParticipationsEntity.getParticipationEntity
import allin.ext.hasToken
import allin.ext.verifyUserFromToken
import allin.model.ApiMessage
@ -13,8 +16,6 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.util.*
val participations = mutableListOf<Participation>()
fun Application.ParticipationRouter() {
routing {
authenticate {
@ -23,7 +24,7 @@ fun Application.ParticipationRouter() {
val participation = call.receive<ParticipationRequest>()
verifyUserFromToken(principal) { user, _ ->
if (user.nbCoins >= participation.stake) {
participations.add(
addParticipationEntity(
Participation(
id = UUID.randomUUID().toString(),
betId = participation.betId,
@ -42,10 +43,11 @@ fun Application.ParticipationRouter() {
delete("/participations/delete") {
hasToken { principal ->
val participationId = call.receive<String>()
participations.find { it.id == participationId }?.let { participation ->
getParticipationEntity().find { it.id == participationId }?.let { participation ->
verifyUserFromToken(principal) { _, _ ->
// user.nbCoins += participation.stake
participations.remove(participation)
//participations.remove(participation)
deleteParticipation(participation)
call.respond(HttpStatusCode.NoContent)
}
} ?: call.respond(HttpStatusCode.NotFound, ApiMessage.ParticipationNotFound)

Loading…
Cancel
Save