Fix getBetAnswerDetail
continuous-integration/drone/push Build is passing Details

pull/5/head
Arthur VALIN 10 months ago
parent c252415b6e
commit 340772040d

@ -19,16 +19,16 @@ data class BetDetail(
val userParticipation: Participation? // La participation du User current
)
fun getBetAnswerDetail(participations: List<Participation>): List<BetAnswerDetail> {
val groupedParticipations = participations.groupBy { it.answer }
val betAnswerDetails = mutableListOf<BetAnswerDetail>()
for ((answer, participationList) in groupedParticipations) {
val totalStakes = participationList.sumBy { it.stake }
val totalParticipants = participationList.size
val highestStake = participationList.maxByOrNull { it.stake }?.stake ?: 0
val odds = 1.0f
val betAnswerDetail = BetAnswerDetail(answer, totalStakes, totalParticipants, highestStake, odds)
betAnswerDetails.add(betAnswerDetail)
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()
)
}
return betAnswerDetails
}

@ -6,7 +6,6 @@ import allin.entities.ParticipationsEntity.getParticipationEntityFromUserId
import allin.ext.hasToken
import allin.ext.verifyUserFromToken
import allin.model.BetDetail
import allin.model.Participation
import allin.model.getBetAnswerDetail
import io.ktor.http.*
import io.ktor.server.application.*
@ -24,22 +23,22 @@ fun Application.BetDetailRouter() {
val participations = getParticipationEntityFromBetId(id)
val selectedBet = getBets().find { it.id == id }
if (selectedBet != null) {
call.respond(
HttpStatusCode.Accepted,
BetDetail(
selectedBet,
getBetAnswerDetail(participations),
participations.toList(),
getParticipationEntityFromUserId(user.username,id).lastOrNull()
)
call.respond(
HttpStatusCode.Accepted,
BetDetail(
selectedBet,
getBetAnswerDetail(selectedBet, participations),
participations.toList(),
getParticipationEntityFromUserId(user.username, id).lastOrNull()
)
} else {
call.respond(HttpStatusCode.NotFound, "Bet not found")
}
)
} else {
call.respond(HttpStatusCode.NotFound, "Bet not found")
}
}
}
}
}
}
}

Loading…
Cancel
Save