From 8f75e78f3ca2248c6768adce90066aaaf2ddf260 Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Mon, 13 May 2024 14:40:22 +0200 Subject: [PATCH] :sparkles: Saving UUID of the user who created a bet --- Sources/src/main/kotlin/allin/Application.kt | 8 +++----- Sources/src/main/kotlin/allin/dto/UserDTO.kt | 6 ++++++ Sources/src/main/kotlin/allin/model/User.kt | 20 +++++++++++++++---- .../main/kotlin/allin/routing/BetRouter.kt | 3 ++- .../main/kotlin/allin/routing/UserRouter.kt | 8 +++++--- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Sources/src/main/kotlin/allin/Application.kt b/Sources/src/main/kotlin/allin/Application.kt index 65fcf00..9b4f0f8 100644 --- a/Sources/src/main/kotlin/allin/Application.kt +++ b/Sources/src/main/kotlin/allin/Application.kt @@ -10,7 +10,6 @@ import allin.utils.kronJob import com.typesafe.config.ConfigFactory import io.github.smiley4.ktorswaggerui.SwaggerUI import io.github.smiley4.ktorswaggerui.data.SwaggerUiSort -import io.github.smiley4.ktorswaggerui.data.SwaggerUiSyntaxHighlight import io.ktor.serialization.kotlinx.json.* import io.ktor.server.application.* import io.ktor.server.auth.* @@ -22,7 +21,6 @@ import io.ktor.server.plugins.contentnegotiation.* import java.time.ZonedDateTime import kotlin.time.Duration.Companion.minutes import kotlin.time.ExperimentalTime -import kotlin.time.minutes @ExperimentalTime val BET_VERIFY_DELAY = 1.minutes @@ -60,11 +58,11 @@ private fun Application.extracted() { } } install(ContentNegotiation) { json() } - install(SwaggerUI){ + install(SwaggerUI) { swagger { swaggerUrl = "swagger" - rootHostPath= isCodeFirstContainer - swaggerUrl= "$isCodeFirstContainer/swagger" + rootHostPath = isCodeFirstContainer + swaggerUrl = "$isCodeFirstContainer/swagger" onlineSpecValidator() displayOperationId = true showTagFilterInput = true diff --git a/Sources/src/main/kotlin/allin/dto/UserDTO.kt b/Sources/src/main/kotlin/allin/dto/UserDTO.kt index c34686f..ebeed97 100644 --- a/Sources/src/main/kotlin/allin/dto/UserDTO.kt +++ b/Sources/src/main/kotlin/allin/dto/UserDTO.kt @@ -1,12 +1,18 @@ package allin.dto +import io.github.smiley4.ktorswaggerui.dsl.Example import kotlinx.serialization.Serializable @Serializable data class UserDTO( + @Example("cabb366c-5a47-4b0f-81e1-25a08fe2c2fe") val id: String, + @Example("Steve") val username: String, + @Example("stevemaroco@gmail.com") val email: String, + @Example("16027") val nbCoins: Int, + @Example("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwOi8vMC4wLjAuMDo4MDgwLyIsImlzcyI6Imh0dHA6Ly8wLjAuMC4wOjgwODAvIiwidXNlcm5hbWUiOiJ0ZXN0IiwiZXhwIjoxNzA3OTIyNjY1fQ.TwaT9Rd4Xkhg3l4fHiba0IEqnM7xUGJVFRrr5oaWOwQ") var token: String? ) diff --git a/Sources/src/main/kotlin/allin/model/User.kt b/Sources/src/main/kotlin/allin/model/User.kt index 9930da7..a354b49 100644 --- a/Sources/src/main/kotlin/allin/model/User.kt +++ b/Sources/src/main/kotlin/allin/model/User.kt @@ -1,31 +1,43 @@ package allin.model +import io.github.smiley4.ktorswaggerui.dsl.Example import kotlinx.serialization.Serializable import kotlin.random.Random - @Serializable +@Serializable data class User( + @Example("cabb366c-5a47-4b0f-81e1-25a08fe2c2fe") val id: String, + @Example("Steve") val username: String, + @Example("stevemaroco@gmail.com") val email: String, + @Example("MarocoSteveHDOIU978*") var password: String, + @Example("16027") var nbCoins: Int = 500, + @Example("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwOi8vMC4wLjAuMDo4MDgwLyIsImlzcyI6Imh0dHA6Ly8wLjAuMC4wOjgwODAvIiwidXNlcm5hbWUiOiJ0ZXN0IiwiZXhwIjoxNzA3OTIyNjY1fQ.TwaT9Rd4Xkhg3l4fHiba0IEqnM7xUGJVFRrr5oaWOwQ") var token: String? = null ) @Serializable data class UserRequest( + @Example("Steve") val username: String, + @Example("stevemaroco@gmail.com") val email: String, + @Example("MarocoSteveHDOIU978*") var password: String ) @Serializable data class CheckUser( + @Example("stevemaroco@gmail.com") val login: String, + @Example("MarocoSteveHDOIU978*") val password: String ) - fun getDailyGift() : Int{ - return Random.nextInt(10,150) - } +fun getDailyGift() : Int{ + return Random.nextInt(10,150) +} diff --git a/Sources/src/main/kotlin/allin/routing/BetRouter.kt b/Sources/src/main/kotlin/allin/routing/BetRouter.kt index 77982a0..40bd75c 100644 --- a/Sources/src/main/kotlin/allin/routing/BetRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/BetRouter.kt @@ -53,10 +53,11 @@ fun Application.BetRouter() { val bet = call.receive() val id = UUID.randomUUID().toString() val username = tokenManagerBet.getUsernameFromToken(principal) + val user = userDataSource.getUserByUsername(username) betDataSource.getBetById(id)?.let { call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST) } ?: run { - val betWithId = bet.copy(id = id, createdBy = username) + val betWithId = bet.copy(id = id, createdBy = user.first?.id.toString()) betDataSource.addBet(betWithId) call.respond(HttpStatusCode.Created, betWithId) } diff --git a/Sources/src/main/kotlin/allin/routing/UserRouter.kt b/Sources/src/main/kotlin/allin/routing/UserRouter.kt index 660f2b0..ca3b1e6 100644 --- a/Sources/src/main/kotlin/allin/routing/UserRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/UserRouter.kt @@ -39,7 +39,9 @@ fun Application.UserRouter() { response { HttpStatusCode.Created to { description = "User created" - body() + body{ + description="The new user" + } } HttpStatusCode.Conflict to { description = "Email or username already taken" @@ -76,7 +78,7 @@ fun Application.UserRouter() { post("/users/login", { description = "Allows a user to login" request { - body { + body { description = "User information" } } @@ -173,7 +175,7 @@ fun Application.UserRouter() { response { HttpStatusCode.OK to { description = "Daily gift allowed !" - body() { + body { description = "Number of coins offered" } }