Merge
continuous-integration/drone/push Build is passing Details

pull/17/head
luevard 8 months ago
parent 110aab80a7
commit 441e213d3e

@ -21,7 +21,8 @@ class MockUserDataSource(private val mockData: MockDataSource.MockData) : UserDa
image = null,
nbBets = MockBetDataSource(mockData).getHistory(it.username).count(),
nbFriends = MockFriendDataSource(mockData).getFriendFromUserId(it.id).count(),
bestWin = MockParticipationDataSource(mockData).getBestWinFromUserid(it.id)
bestWin = MockParticipationDataSource(mockData).getBestWinFromUserid(it.id),
friendStatus = null,
),
it.password
)

@ -24,18 +24,22 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc
)
}
override fun getFriendFromUserId(id: String) =
database.friends.map { it.toFriend() }
override fun getFriendFromUserId(id: String): List<UserDTO> {
return database.friends.map { it.toFriend() }
.filter { it.sender == id }
.mapNotNull {
database.users.find { usr ->
usr.id eq it.receiver
}?.toUserDTO(
friendStatus = if (isFriend(it.receiver, id)) {
.mapNotNull { friend ->
val receiverUser = database.users.find { usr ->
usr.id eq friend.receiver
}
receiverUser?.toUserDTO(
database,
friendStatus = if (isFriend(friend.receiver, id)) {
FriendStatus.FRIEND
} else FriendStatus.REQUESTED
)
}
}
override fun getFriendRequestsFromUserId(id: String): List<UserDTO> {
return database.friends
@ -46,7 +50,7 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc
} else {
database.users.find { usr ->
usr.id eq it.sender
}?.toUserDTO(friendStatus = FriendStatus.NOT_FRIEND)
}?.toUserDTO(database,friendStatus = FriendStatus.NOT_FRIEND)
}
}
}
@ -67,5 +71,5 @@ class PostgresFriendDataSource(private val database: Database) : FriendDataSourc
.filter { it.id notEq fromUserId }
.sortedBy { it.username.toLowerCase().levenshtein(search.lowercase()) }
.take(10)
.map { user -> user.toUserDTO(friendStatus = getFriendStatus(fromUserId, user.id)) }
.map { user -> user.toUserDTO(database,friendStatus = getFriendStatus(fromUserId, user.id)) }
}

@ -4,6 +4,7 @@ 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
@ -27,7 +28,7 @@ interface UserEntity : Entity<UserEntity> {
var nbCoins: Int
var lastGift: Instant
fun toUserDTO(database: Database) =
fun toUserDTO(database: Database, friendStatus: FriendStatus? = null) =
UserDTO(
id = id,
username = username,
@ -38,6 +39,7 @@ interface UserEntity : Entity<UserEntity> {
nbBets = PostgresBetDataSource(database).getHistory(username).count(),
nbFriends = PostgresFriendDataSource(database).getFriendFromUserId(id).count(),
bestWin = PostgresParticipationDataSource(database).getBestWinFromUserid(id)?: 0,
friendStatus = friendStatus
)
fun getImage(userId: String, database: Database): String? {

@ -1,5 +1,6 @@
package allin.dto
import allin.model.FriendStatus
import kotlinx.serialization.Serializable
@Serializable
@ -12,5 +13,6 @@ data class UserDTO(
val image: String?,
var nbBets: Int,
var nbFriends: Int,
var bestWin: Int
var bestWin: Int,
var friendStatus: FriendStatus?,
)

@ -1,5 +1,6 @@
package allin.model
import allin.dto.UserDTO
import kotlinx.serialization.Serializable
const val DEFAULT_COIN_AMOUNT = 500
@ -15,8 +16,27 @@ data class User(
var nbCoins: Int = DEFAULT_COIN_AMOUNT,
var token: String? = null,
var image: String? = null,
var bestWin: Int?,
var nbBets: Int,
var nbFriends: Int,
)
{
fun toDto(friendStatus: FriendStatus? = null) =
UserDTO(
id = id,
username = username,
email = email,
nbCoins = nbCoins,
token = token,
friendStatus = friendStatus,
image = image,
bestWin = bestWin?:0,
nbBets = nbBets,
nbFriends=nbFriends
)
}
@Serializable
data class UserRequest(
val username: String,

@ -70,7 +70,10 @@ fun Application.userRouter() {
email = tempUser.email,
password = tempUser.password,
nbCoins = DEFAULT_COINS,
token = null
token = null,
bestWin = 0,
nbFriends = 0,
nbBets = 0,
)
CryptManagerUser.passwordCrypt(user)
user.token = tokenManagerUser.generateOrReplaceJWTToken(user)

Loading…
Cancel
Save