Skip to content

Commit

Permalink
Push works
Browse files Browse the repository at this point in the history
  • Loading branch information
cheroliv committed Oct 14, 2024
1 parent 44e5640 commit de773d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 45 deletions.
67 changes: 25 additions & 42 deletions api/src/main/kotlin/school/users/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import school.users.security.Role
import school.users.security.UserRole
import school.users.security.UserRole.UserRoleDao.Dao.signup
import java.util.*
import java.util.UUID.randomUUID
import jakarta.validation.constraints.Email as EmailConstraint

data class User(
Expand Down Expand Up @@ -218,7 +219,7 @@ data class User(
User::class -> {
try {
(getBean<DatabaseClient>()
.sql("SELECT * FROM `user` u WHERE LOWER(u.email) = LOWER(:emailOrLogin) || LOWER(u.login) = LOWER(:emailOrLogin)")
.sql("SELECT `u`.`id` FROM `user` u WHERE LOWER(u.email) = LOWER(:emailOrLogin) || LOWER(u.login) = LOWER(:emailOrLogin)")
.bind("email", emailOrLogin)
.bind("login", emailOrLogin)
.fetch()
Expand All @@ -233,7 +234,6 @@ data class User(
}


//TODO: change sinature to return UUID
// fun findAuthsByUserId(userId: UUID): Either<Throwable, Set<Role>>{}

suspend fun ApplicationContext.findAuthsByEmail(email: String): Either<Throwable, Set<Role>> = try {
Expand Down Expand Up @@ -266,57 +266,38 @@ data class User(
// }


suspend inline fun <reified T : EntityModel<*>> ApplicationContext.findOneByLogin(login: String): Either<Throwable, T> =
suspend inline fun <reified T : EntityModel<UUID>> ApplicationContext.findOneByLogin(login: String): Either<Throwable, UUID> =
when (T::class) {
User::class -> {
try {
val user = getBean<DatabaseClient>()
.sql("SELECT * FROM `user` u WHERE LOWER(u.login) = LOWER(:login)")
(getBean<DatabaseClient>()
.sql("SELECT `u`.`id` FROM `user` u WHERE LOWER(u.login) = LOWER(:login)")
.bind("login", login)
.fetch()
.awaitOne()
.let { row ->
User(
id = row["id"] as UUID?,
login = row["login"] as String,
password = row["password"] as String,
email = row["email"] as String,
langKey = row["lang_key"] as String,
version = row["version"] as Long
)
}
Right(user as T)
.let { it["`id`"] as UUID }).right()
} catch (e: Throwable) {
Left(e)
e.left()
}
}

else -> Left(IllegalArgumentException("Unsupported type: ${T::class.simpleName}"))
}


suspend inline fun <reified T : EntityModel<*>> ApplicationContext.findOneByEmail(email: String): Either<Throwable, T> =
suspend inline fun <reified T : EntityModel<UUID>> ApplicationContext.findOneByEmail(email: String): Either<Throwable, UUID> =
when (T::class) {
User::class -> {
try {
val user = getBean<DatabaseClient>()
.sql("SELECT * FROM `user` u WHERE LOWER(u.email) = LOWER(:email)")
getBean<DatabaseClient>()
.sql("SELECT `u`.`id` FROM `user` u WHERE LOWER(u.email) = LOWER(:email)")
.bind("email", email)
.fetch()
.awaitOne()
.let { row ->
User(
id = row["id"] as UUID?,
login = row["login"] as String,
password = row["password"] as String,
email = row["email"] as String,
langKey = row["lang_key"] as String,
version = row["version"] as Long
)
}
Right(user as T)
.let { it["id"] as UUID }
.right()
} catch (e: Throwable) {
Left(e)
e.left()
}
}

Expand All @@ -331,26 +312,28 @@ data class User(
second.findOneByEmail<User>(first.email)
.mapLeft { return Exception("Unable to find user by email").left() }
.map {
(UserRole(userId = it.id!!, role = ROLE_USER) to second).signup()
return it.id.right()
(UserRole(userId = it, role = ROLE_USER) to second).signup()
return it.right()
}
} catch (e: Throwable) {
e.left()
}

fun ApplicationContext.signupToUser(signup: Signup): User {
fun ApplicationContext.signupToUser(signup: Signup): User = signup.apply {
// Validation du mot de passe et de la confirmation
require(signup.password == signup.repassword) { "Passwords do not match!" }
require(password == repassword) { "Passwords do not match!" }
}.run {
// Création d'un utilisateur à partir des données de Signup
return User(
id = UUID.randomUUID(), // Génération d'un UUID
login = signup.login,
password = getBean<PasswordEncoder>().encode(signup.password),
email = signup.email,
roles = mutableSetOf(Role(ANONYMOUS_USER)), // Role par défaut
User(
id = randomUUID(), // Génération d'un UUID
login = login,
password = getBean<PasswordEncoder>().encode(password),
email = email,
roles = emptySet(),//mutableSetOf(Role(ANONYMOUS_USER)), // Role par défaut
langKey = "en" // Valeur par défaut, ajustez si nécessaire
)
}

}
}
}
2 changes: 1 addition & 1 deletion api/src/test/kotlin/school/users/UserDaoTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class UserDaoTests {
context.findOneByEmail<User>(user.email).apply {
assertTrue(isRight())
assertFalse(isLeft())
}.map { assertEquals(it, user.withId(it.id!!)) }
}.map { assertDoesNotThrow { fromString(it.toString()) } }
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions api/src/test/kotlin/school/users/UserIntegrationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class UserIntegrationTests {
when (this) {
is Left -> assertEquals(EmptyResultDataAccessException::class.java, value::class.java)
is Right -> {
assertEquals(user, value)
assertEquals(user.id, value)
}
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ class UserIntegrationTests {
when (this) {
is Left -> assertEquals(value::class.java, NullPointerException::class.java)
is Right -> {
assertEquals(user, value)
assertEquals(user.id, value)
}
}
}
Expand Down

0 comments on commit de773d2

Please sign in to comment.