From 4cfc3543e492e9a558458802814b29a30c50d173 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:54:41 +0200 Subject: [PATCH] :sparkles: add /bets/users routes --- .../postgres/entities/ParticipationEntity.kt | 2 +- .../main/kotlin/allin/routing/betRouter.kt | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt index 222df52..1397233 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/ParticipationEntity.kt @@ -28,7 +28,7 @@ interface ParticipationEntity : Entity { } object ParticipationsEntity : Table("participation") { - val id = varchar("id").primaryKey() + val id = varchar("id").primaryKey().bindTo { it.id } val betId = varchar("bet").references(BetsEntity) { it.bet } val username = varchar("username").bindTo { it.username } val answer = varchar("answer").bindTo { it.answer } diff --git a/Sources/src/main/kotlin/allin/routing/betRouter.kt b/Sources/src/main/kotlin/allin/routing/betRouter.kt index a3e4a72..edd7f69 100644 --- a/Sources/src/main/kotlin/allin/routing/betRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/betRouter.kt @@ -1,6 +1,7 @@ package allin.routing import allin.dataSource +import allin.dto.UserDTO import allin.ext.hasToken import allin.ext.verifyUserFromToken import allin.model.* @@ -21,6 +22,7 @@ val tokenManagerBet = AppConfig.tokenManager fun Application.betRouter() { val userDataSource = this.dataSource.userDataSource val betDataSource = this.dataSource.betDataSource + val participationDataSource = this.dataSource.participationDataSource val logManager = AppConfig.logManager routing { @@ -344,11 +346,38 @@ fun Application.betRouter() { logManager.log("Routing","UNAUTHORIZED /bets/confirm/{id}") call.respond(HttpStatusCode.Unauthorized) } - } } } + post("/bets/users", { + description = "gets all userDTO of a bet" + request { + headerParameter("JWT token of the logged user") + pathParameter("Id of the desired bet") + body> { + description = "UserDTO of the bet" + } + } + response { + HttpStatusCode.OK to { + description = "The final answer has been set" + } + HttpStatusCode.Unauthorized to { + description = "The user is not the creator of the bet" + } + } + }) { + logManager.log("Routing","POST /bets/users") + hasToken { principal -> + verifyUserFromToken(userDataSource, principal) { user, _ -> + val id = call.receive>()["id"] ?: "" + val participations = participationDataSource.getParticipationFromBetId(id) + val users = participations.map { userDataSource.getUserByUsername(it.username).first }.toSet().take(4).toList() + call.respond(users) + } + } } } +} } \ No newline at end of file -- 2.36.3