diff --git a/Sources/src/main/kotlin/allin/data/ParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/ParticipationDataSource.kt index 5490ff9..1e6b3d8 100644 --- a/Sources/src/main/kotlin/allin/data/ParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/ParticipationDataSource.kt @@ -7,5 +7,4 @@ interface ParticipationDataSource { fun getParticipationFromBetId(betid: String): List fun getParticipationFromUserId(username: String, betid: String): List fun deleteParticipation(id: String): Boolean - fun getBestWinFromUserid(userId: String): Int? } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt index 3c1a8d0..e0e681f 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockParticipationDataSource.kt @@ -88,9 +88,4 @@ class MockParticipationDataSource(private val mockData: MockDataSource.MockData) return result } - - override fun getBestWinFromUserid(userId: String) = - mockData.participations.filter { it.id == userId }.maxBy { it.stake }.stake - - } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/mock/MockUserDataSource.kt b/Sources/src/main/kotlin/allin/data/mock/MockUserDataSource.kt index 87de664..587f7b2 100644 --- a/Sources/src/main/kotlin/allin/data/mock/MockUserDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/mock/MockUserDataSource.kt @@ -10,21 +10,30 @@ class MockUserDataSource(private val mockData: MockDataSource.MockData) : UserDa private val lastGifts get() = mockData.lastGifts override fun getUserByUsername(username: String): Pair = - users.find { (it.username == username) or (it.email == username) }?.let { + users.find { (it.username == username) or (it.email == username) }?.let { usr -> Pair( UserDTO( - id = it.id, - username = it.username, - email = it.email, - nbCoins = it.nbCoins, - token = it.token, + id = usr.id, + username = usr.username, + email = usr.email, + nbCoins = usr.nbCoins, + token = usr.token, image = null, - nbBets = MockBetDataSource(mockData).getHistory(it.username).count(), - nbFriends = MockFriendDataSource(mockData).getFriendFromUserId(it.id).count(), - bestWin = MockParticipationDataSource(mockData).getBestWinFromUserid(it.id), + nbBets = mockData.participations.count { it.username == usr.username }, + nbFriends = mockData.friends.count { f -> + f.receiver == usr.username && + mockData.friends.any { it.sender == usr.username && it.receiver == f.sender } + }, + bestWin = mockData.participations + .filter { + (it.id == usr.id) && + (mockData.results.find { r -> r.betId == it.betId })?.result == it.answer + } + .maxBy { it.stake } + .stake, friendStatus = null, ), - it.password + usr.password ) } ?: Pair(null, null) diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt index 0577a74..7972b42 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresFriendDataSource.kt @@ -5,11 +5,13 @@ import allin.data.postgres.entities.FriendEntity import allin.data.postgres.entities.friends import allin.data.postgres.entities.users import allin.dto.UserDTO +import allin.ext.length import allin.ext.levenshteinLessEq import allin.ext.toLowerCase import allin.model.FriendStatus import org.ktorm.database.Database import org.ktorm.dsl.and +import org.ktorm.dsl.div import org.ktorm.dsl.eq import org.ktorm.dsl.notEq import org.ktorm.entity.* @@ -67,24 +69,29 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc database.friends.any { (it.sender eq firstUser) and (it.receiver eq secondUser) } override fun filterUsersByUsername(fromUserId: String, search: String): List { - val maxSize = search.length / 2 return database.users .filter { (it.id notEq fromUserId) } .mapColumns { tupleOf( it.id, - it.username.toLowerCase().levenshteinLessEq(search.lowercase(), maxSize) + it.username, + it.username.toLowerCase().levenshteinLessEq( + search.lowercase(), + (it.username.length() / 2) + ) ) } - .filter { (_, distance) -> + .filter { (_, username, distance) -> + val maxSize = ((username?.length ?: 0) / 2) distance?.let { it <= maxSize } ?: false } .sortedBy { it.second } - .mapNotNull { (id, _) -> + .mapNotNull { (id, _, _) -> id?.let { val user = database.users.find { it.id eq id } user?.toUserDTO(database, friendStatus = getFriendStatus(fromUserId, user.id)) } } } + } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt index 6c3ae39..c6b3b96 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresParticipationDataSource.kt @@ -68,9 +68,6 @@ class PostgresParticipationDataSource(private val database: Database) : Particip return participation.delete() > 0 } - override fun getBestWinFromUserid(userId: String) = - database.participations.filter { it.id eq userId }.maxBy { it.stake } - } diff --git a/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt b/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt index 6794e5b..ef5d0f4 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/PostgresUserDataSource.kt @@ -93,13 +93,17 @@ class PostgresUserDataSource(private val database: Database) : UserDataSource { } override fun getImage(userid: String): String? { - val resultSet = database.executeWithResult("SELECT encode(image, 'base64') AS image FROM userimage WHERE user_id = '${userid}'")?: return null - if (resultSet.next()) { - val base64Image: String? = resultSet.getString("image") - if (base64Image != null) { - return base64Image - } - } + val resultSet = + database.executeWithResult( + """ + SELECT encode(image, 'base64') AS image + FROM userimage + WHERE user_id = '${userid}' + """.trimIndent() + ) ?: return null + if (resultSet.next()) { + resultSet.getString("image")?.let { return it } + } return null } } \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt b/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt index f0d17b4..01478f7 100644 --- a/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt +++ b/Sources/src/main/kotlin/allin/data/postgres/entities/UserEntity.kt @@ -1,17 +1,13 @@ package allin.data.postgres.entities -import allin.data.postgres.PostgresBetDataSource -import allin.data.postgres.PostgresFriendDataSource -import allin.data.postgres.PostgresParticipationDataSource import allin.dto.UserDTO import allin.model.FriendStatus import allin.routing.imageManagerUser import allin.utils.AppConfig import org.ktorm.database.Database +import org.ktorm.dsl.and import org.ktorm.dsl.eq -import org.ktorm.entity.Entity -import org.ktorm.entity.find -import org.ktorm.entity.sequenceOf +import org.ktorm.entity.* import org.ktorm.schema.Table import org.ktorm.schema.int import org.ktorm.schema.timestamp @@ -36,9 +32,19 @@ interface UserEntity : Entity { nbCoins = nbCoins, token = null, image = getImage(id, database), - nbBets = PostgresBetDataSource(database).getHistory(username).count(), - nbFriends = PostgresFriendDataSource(database).getFriendFromUserId(id).count(), - bestWin = PostgresParticipationDataSource(database).getBestWinFromUserid(id)?: 0, + nbBets = database.participations.count { it.username eq this.username }, + nbFriends = database.friends + .filter { it.receiver eq this.id } + .mapNotNull { p -> database.friends.any { (it.sender eq this.id) and (it.receiver eq p.sender) } } + .count(), + bestWin = database.participations + .filter { (it.id eq this.id) } + .mapNotNull { p -> + if (database.betResults.any { (it.betId eq p.bet.id) and (it.result eq p.answer) }) { + p.stake + } else null + } + .maxOrNull() ?: 0, friendStatus = friendStatus ) diff --git a/Sources/src/main/kotlin/allin/dto/UserDTO.kt b/Sources/src/main/kotlin/allin/dto/UserDTO.kt index edd4c20..a0172b6 100644 --- a/Sources/src/main/kotlin/allin/dto/UserDTO.kt +++ b/Sources/src/main/kotlin/allin/dto/UserDTO.kt @@ -14,5 +14,5 @@ data class UserDTO( var nbBets: Int, var nbFriends: Int, var bestWin: Int, - var friendStatus: FriendStatus?, + var friendStatus: FriendStatus? ) \ No newline at end of file diff --git a/Sources/src/main/kotlin/allin/ext/DatabaseExt.kt b/Sources/src/main/kotlin/allin/ext/DatabaseExt.kt index 5562503..ce6ddef 100644 --- a/Sources/src/main/kotlin/allin/ext/DatabaseExt.kt +++ b/Sources/src/main/kotlin/allin/ext/DatabaseExt.kt @@ -1,7 +1,6 @@ package allin.ext import org.ktorm.database.Database -import org.ktorm.expression.ArgumentExpression import org.ktorm.expression.FunctionExpression import org.ktorm.schema.ColumnDeclaring import org.ktorm.schema.IntSqlType @@ -33,6 +32,14 @@ fun Database.execute(request: String) { } } +fun ColumnDeclaring.length(): FunctionExpression { + return FunctionExpression( + functionName = "LENGTH", + arguments = listOf(this.asExpression()), + sqlType = IntSqlType + ) +} + fun ColumnDeclaring.toLowerCase(): FunctionExpression { return FunctionExpression( functionName = "LOWER", @@ -60,8 +67,8 @@ fun ColumnDeclaring.levenshteinLessEq( ) } -fun ColumnDeclaring.levenshteinLessEq(target: String, max: Int): FunctionExpression = +fun ColumnDeclaring.levenshteinLessEq(target: String, max: ColumnDeclaring): FunctionExpression = levenshteinLessEq( wrapArgument(target), - ArgumentExpression(max, IntSqlType) + max ) \ No newline at end of file