From cbc26a9581ec24e916279caf0e84459172e2143f Mon Sep 17 00:00:00 2001 From: luevard <99143550+saucepommefrite@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:12:46 +0100 Subject: [PATCH] :sparkles: Add DailyGift route --- Sources/src/main/kotlin/allin/dto/UserDTO.kt | 2 +- .../main/kotlin/allin/entities/UserEntity.kt | 61 ++++++++++++------- .../main/kotlin/allin/routing/UserRouter.kt | 8 ++- .../main/kotlin/allin/utils/DatabaseUtil.kt | 11 +++- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/Sources/src/main/kotlin/allin/dto/UserDTO.kt b/Sources/src/main/kotlin/allin/dto/UserDTO.kt index c0bec1d..2ed2a42 100644 --- a/Sources/src/main/kotlin/allin/dto/UserDTO.kt +++ b/Sources/src/main/kotlin/allin/dto/UserDTO.kt @@ -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?) diff --git a/Sources/src/main/kotlin/allin/entities/UserEntity.kt b/Sources/src/main/kotlin/allin/entities/UserEntity.kt index bbd0321..1bb82ea 100644 --- a/Sources/src/main/kotlin/allin/entities/UserEntity.kt +++ b/Sources/src/main/kotlin/allin/entities/UserEntity.kt @@ -4,51 +4,56 @@ import allin.database import allin.dto.UserDTO import allin.model.User import allin.utils.Execute -import io.ktor.util.date.* -import org.h2.util.DateTimeUtils.currentTimestamp +import allin.utils.ExecuteWithResult +import org.ktorm.database.use import org.ktorm.dsl.* import org.ktorm.entity.* import org.ktorm.schema.* -import java.time.Duration -import java.util.* +import java.time.Instant.now import java.util.UUID.fromString interface UserEntity : Entity { val username: String var email: String var password: String - var nbCoins: Double + var nbCoins: Int } object UsersEntity : Table("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") - val lastGift = varchar("lastgift") + val lastGift = timestamp("lastgift") fun getUserToUserDTO(): MutableList { return database.from(UsersEntity).select().map { - row -> UserDTO( - row[id].toString(), - row[username].toString(), - row[email].toString(), - row[nbCoins]?:0.0, - null - ) + row -> UserDTO( + row[id].toString(), + row[username].toString(), + row[email].toString(), + row[nbCoins]?:0, + null + ) }.toMutableList() } fun createUserTable(){ - val request="CREATE TABLE IF not exists utilisateur ( id uuid PRIMARY KEY, username VARCHAR(255), password VARCHAR(255),coins double precision,email VARCHAR(255), lastgift timestamp)" + val request="CREATE TABLE IF not exists utilisateur ( id uuid PRIMARY KEY, username VARCHAR(255), password VARCHAR(255),coins numeric,email VARCHAR(255), lastgift timestamp)" database.Execute(request) } fun modifyCoins(user: String, cost : Int){ val request = "UPDATE utilisateur SET coins = coins - $cost WHERE username = '$user';" database.Execute(request) + } + fun addCoinByUsername(username: String, coins : Int){ + database.update(UsersEntity){ + set(nbCoins,coins+ nbCoins) + where {it.username eq username} + } } fun getUserByUsernameAndPassword(login: String): Pair { @@ -61,7 +66,7 @@ object UsersEntity : Table("utilisateur") { row[id].toString(), row[username].toString(), row[email].toString(), - row[nbCoins] ?: 0.0, + row[nbCoins] ?: 0, null ), row[password].toString() @@ -77,6 +82,7 @@ object UsersEntity : Table("utilisateur") { set(it.username,user.username) set(it.password,user.password) set(it.email,user.email) + set(it.lastGift,now()) } } fun deleteUserByUsername(username: String): Boolean { @@ -87,14 +93,25 @@ object UsersEntity : Table("utilisateur") { } fun canHaveDailyGift(username: String): Boolean { - val request = "SELECT CASE WHEN lastgift IS NULL THEN TRUE ELSE lastgift < current_timestamp - interval '1 day' END AS can_have_daily_gift, " + - "CASE WHEN lastgift IS NULL THEN null ELSE current_timestamp - lastgift END AS time_remaining " + - "FROM utilisateur WHERE username = '$username';" - val returnCode= database.Execute(request) + val request = "SELECT CASE WHEN NOW() - lastgift > INTERVAL '1 day' THEN true ELSE false END AS is_lastgift_greater_than_1_day FROM utilisateur WHERE username = '$username';" + val resultSet = database.ExecuteWithResult(request) - if(returnCode?.next().toString()=="true"){ - return true + resultSet?.use { + if (resultSet.next()) { + val isDailyGift = resultSet.getBoolean("is_lastgift_greater_than_1_day") + if (isDailyGift) { + database.update(UsersEntity) { + set(lastGift, now()) + where { it.username eq username } + } + } + return isDailyGift + } } return false } + } + + + diff --git a/Sources/src/main/kotlin/allin/routing/UserRouter.kt b/Sources/src/main/kotlin/allin/routing/UserRouter.kt index 9c64ebf..88bd746 100644 --- a/Sources/src/main/kotlin/allin/routing/UserRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/UserRouter.kt @@ -1,5 +1,6 @@ package allin.routing +import allin.entities.UsersEntity.addCoinByUsername import allin.entities.UsersEntity.addUserEntity import allin.entities.UsersEntity.canHaveDailyGift import allin.entities.UsersEntity.deleteUserByUsername @@ -94,8 +95,11 @@ fun Application.UserRouter() { get("/users/gift") { hasToken { principal -> verifyUserFromToken(principal) { userDto, _ -> - if(canHaveDailyGift(userDto.username)) - call.respond(HttpStatusCode.OK, getDailyGift()) + if(canHaveDailyGift(userDto.username)){ + val dailyGift = getDailyGift() + addCoinByUsername(userDto.username,dailyGift) + call.respond(HttpStatusCode.OK,dailyGift) + } else call.respond(HttpStatusCode.MethodNotAllowed,"Le cadeau ne peut pas être récupéré") } } diff --git a/Sources/src/main/kotlin/allin/utils/DatabaseUtil.kt b/Sources/src/main/kotlin/allin/utils/DatabaseUtil.kt index bd83e54..28fefea 100644 --- a/Sources/src/main/kotlin/allin/utils/DatabaseUtil.kt +++ b/Sources/src/main/kotlin/allin/utils/DatabaseUtil.kt @@ -4,7 +4,7 @@ import allin.database import org.ktorm.database.Database import java.sql.ResultSet -fun Database.Execute(request: String): ResultSet? { +fun Database.ExecuteWithResult(request: String): ResultSet? { try { if (!request.isNullOrEmpty()) { return database.useTransaction { transaction -> @@ -19,3 +19,12 @@ fun Database.Execute(request: String): ResultSet? { } return null } + +fun Database.Execute(request: String){ + if(!request.isNullOrEmpty()) + database.useTransaction { + val connection = it.connection + connection.prepareStatement(request).execute() + connection.commit() + } +} \ No newline at end of file