Saving UUID of the user who created a bet
continuous-integration/drone/push Build is passing Details

pull/12/head
luevard 11 months ago
parent dc929370d1
commit 8f75e78f3c

@ -10,7 +10,6 @@ import allin.utils.kronJob
import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigFactory
import io.github.smiley4.ktorswaggerui.SwaggerUI import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.github.smiley4.ktorswaggerui.data.SwaggerUiSort import io.github.smiley4.ktorswaggerui.data.SwaggerUiSort
import io.github.smiley4.ktorswaggerui.data.SwaggerUiSyntaxHighlight
import io.ktor.serialization.kotlinx.json.* import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.auth.* import io.ktor.server.auth.*
@ -22,7 +21,6 @@ import io.ktor.server.plugins.contentnegotiation.*
import java.time.ZonedDateTime import java.time.ZonedDateTime
import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.minutes
import kotlin.time.ExperimentalTime import kotlin.time.ExperimentalTime
import kotlin.time.minutes
@ExperimentalTime @ExperimentalTime
val BET_VERIFY_DELAY = 1.minutes val BET_VERIFY_DELAY = 1.minutes

@ -1,12 +1,18 @@
package allin.dto package allin.dto
import io.github.smiley4.ktorswaggerui.dsl.Example
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class UserDTO( data class UserDTO(
@Example("cabb366c-5a47-4b0f-81e1-25a08fe2c2fe")
val id: String, val id: String,
@Example("Steve")
val username: String, val username: String,
@Example("stevemaroco@gmail.com")
val email: String, val email: String,
@Example("16027")
val nbCoins: Int, val nbCoins: Int,
@Example("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwOi8vMC4wLjAuMDo4MDgwLyIsImlzcyI6Imh0dHA6Ly8wLjAuMC4wOjgwODAvIiwidXNlcm5hbWUiOiJ0ZXN0IiwiZXhwIjoxNzA3OTIyNjY1fQ.TwaT9Rd4Xkhg3l4fHiba0IEqnM7xUGJVFRrr5oaWOwQ")
var token: String? var token: String?
) )

@ -1,28 +1,40 @@
package allin.model package allin.model
import io.github.smiley4.ktorswaggerui.dsl.Example
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlin.random.Random import kotlin.random.Random
@Serializable @Serializable
data class User( data class User(
@Example("cabb366c-5a47-4b0f-81e1-25a08fe2c2fe")
val id: String, val id: String,
@Example("Steve")
val username: String, val username: String,
@Example("stevemaroco@gmail.com")
val email: String, val email: String,
@Example("MarocoSteveHDOIU978*")
var password: String, var password: String,
@Example("16027")
var nbCoins: Int = 500, var nbCoins: Int = 500,
@Example("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJodHRwOi8vMC4wLjAuMDo4MDgwLyIsImlzcyI6Imh0dHA6Ly8wLjAuMC4wOjgwODAvIiwidXNlcm5hbWUiOiJ0ZXN0IiwiZXhwIjoxNzA3OTIyNjY1fQ.TwaT9Rd4Xkhg3l4fHiba0IEqnM7xUGJVFRrr5oaWOwQ")
var token: String? = null var token: String? = null
) )
@Serializable @Serializable
data class UserRequest( data class UserRequest(
@Example("Steve")
val username: String, val username: String,
@Example("stevemaroco@gmail.com")
val email: String, val email: String,
@Example("MarocoSteveHDOIU978*")
var password: String var password: String
) )
@Serializable @Serializable
data class CheckUser( data class CheckUser(
@Example("stevemaroco@gmail.com")
val login: String, val login: String,
@Example("MarocoSteveHDOIU978*")
val password: String val password: String
) )

@ -53,10 +53,11 @@ fun Application.BetRouter() {
val bet = call.receive<Bet>() val bet = call.receive<Bet>()
val id = UUID.randomUUID().toString() val id = UUID.randomUUID().toString()
val username = tokenManagerBet.getUsernameFromToken(principal) val username = tokenManagerBet.getUsernameFromToken(principal)
val user = userDataSource.getUserByUsername(username)
betDataSource.getBetById(id)?.let { betDataSource.getBetById(id)?.let {
call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST) call.respond(HttpStatusCode.Conflict, ApiMessage.BET_ALREADY_EXIST)
} ?: run { } ?: run {
val betWithId = bet.copy(id = id, createdBy = username) val betWithId = bet.copy(id = id, createdBy = user.first?.id.toString())
betDataSource.addBet(betWithId) betDataSource.addBet(betWithId)
call.respond(HttpStatusCode.Created, betWithId) call.respond(HttpStatusCode.Created, betWithId)
} }

@ -39,7 +39,9 @@ fun Application.UserRouter() {
response { response {
HttpStatusCode.Created to { HttpStatusCode.Created to {
description = "User created" description = "User created"
body<User>() body<User>{
description="The new user"
}
} }
HttpStatusCode.Conflict to { HttpStatusCode.Conflict to {
description = "Email or username already taken" description = "Email or username already taken"
@ -76,7 +78,7 @@ fun Application.UserRouter() {
post("/users/login", { post("/users/login", {
description = "Allows a user to login" description = "Allows a user to login"
request { request {
body<UserRequest> { body<CheckUser> {
description = "User information" description = "User information"
} }
} }
@ -173,7 +175,7 @@ fun Application.UserRouter() {
response { response {
HttpStatusCode.OK to { HttpStatusCode.OK to {
description = "Daily gift allowed !" description = "Daily gift allowed !"
body<Int>() { body<Int> {
description = "Number of coins offered" description = "Number of coins offered"
} }
} }

Loading…
Cancel
Save