Add user service tests
This commit is contained in:
@@ -2,7 +2,6 @@ package be.vandewalleh.routing
|
||||
|
||||
import be.vandewalleh.extensions.respondStatus
|
||||
import be.vandewalleh.extensions.userId
|
||||
import be.vandewalleh.services.UserDto
|
||||
import be.vandewalleh.services.UserService
|
||||
import io.ktor.application.*
|
||||
import io.ktor.auth.*
|
||||
@@ -26,9 +25,7 @@ fun Routing.user(kodein: Kodein) {
|
||||
|
||||
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
|
||||
|
||||
userService.createUser(
|
||||
UserDto(user.username, user.email, hashedPassword)
|
||||
)
|
||||
userService.createUser(user.username, user.email, hashedPassword)
|
||||
|
||||
call.respondStatus(HttpStatusCode.Created)
|
||||
}
|
||||
@@ -42,10 +39,7 @@ fun Routing.user(kodein: Kodein) {
|
||||
|
||||
val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt())
|
||||
|
||||
userService.updateUser(
|
||||
call.userId(),
|
||||
UserDto(user.username, user.email, hashedPassword)
|
||||
)
|
||||
userService.updateUser(call.userId(), user.username, user.email, hashedPassword)
|
||||
|
||||
call.respondStatus(HttpStatusCode.OK)
|
||||
}
|
||||
@@ -57,5 +51,6 @@ fun Routing.user(kodein: Kodein) {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private data class UserDto(val username: String, val email: String, val password: String)
|
||||
|
||||
@@ -8,6 +8,7 @@ import me.liuwj.ktorm.entity.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.KodeinAware
|
||||
import org.kodein.di.generic.instance
|
||||
import java.sql.SQLIntegrityConstraintViolationException
|
||||
import java.time.LocalDateTime
|
||||
|
||||
/**
|
||||
@@ -28,19 +29,14 @@ class UserService(override val kodein: Kodein) : KodeinAware {
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a user email and password from it's email if found or null
|
||||
* returns a user email and password from it's username if found or null
|
||||
*/
|
||||
fun getFromUsername(username: String): UserSchema? {
|
||||
fun getFromUsername(username: String): User? {
|
||||
return db.from(Users)
|
||||
.select(Users.email, Users.password, Users.id)
|
||||
.where { Users.username eq username }
|
||||
.map { row ->
|
||||
UserSchema(
|
||||
row[Users.id]!!,
|
||||
username,
|
||||
row[Users.email]!!,
|
||||
row[Users.password]!!
|
||||
)
|
||||
Users.createEntity(row)
|
||||
}
|
||||
.firstOrNull()
|
||||
}
|
||||
@@ -59,11 +55,11 @@ class UserService(override val kodein: Kodein) : KodeinAware {
|
||||
.firstOrNull() != null
|
||||
}
|
||||
|
||||
fun getUserInfo(id: Int): UserInfoDto? {
|
||||
fun getUserInfo(id: Int): User? {
|
||||
return db.from(Users)
|
||||
.select(Users.email, Users.username)
|
||||
.where { Users.id eq id }
|
||||
.map { UserInfoDto(it[Users.username]!!, it[Users.email]!!) }
|
||||
.map { Users.createEntity(it) }
|
||||
.firstOrNull()
|
||||
}
|
||||
|
||||
@@ -71,25 +67,30 @@ class UserService(override val kodein: Kodein) : KodeinAware {
|
||||
* create a new user
|
||||
* password should already be hashed
|
||||
*/
|
||||
fun createUser(user: UserDto) {
|
||||
db.useTransaction {
|
||||
val newUser = User {
|
||||
this.username = user.username
|
||||
this.email = user.email
|
||||
this.password = user.password
|
||||
this.createdAt = LocalDateTime.now()
|
||||
}
|
||||
fun createUser(username: String, email: String, hashedPassword: String): User? {
|
||||
try {
|
||||
db.useTransaction {
|
||||
val newUser = User {
|
||||
this.username = username
|
||||
this.email = email
|
||||
this.password = hashedPassword
|
||||
this.createdAt = LocalDateTime.now()
|
||||
}
|
||||
|
||||
db.sequenceOf(Users).add(newUser)
|
||||
db.sequenceOf(Users).add(newUser)
|
||||
return newUser
|
||||
}
|
||||
} catch (e: SQLIntegrityConstraintViolationException) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun updateUser(userId: Int, user: UserDto) {
|
||||
fun updateUser(userId: Int, username: String, email: String, hashedPassword: String) {
|
||||
db.useTransaction {
|
||||
db.update(Users) {
|
||||
it.username to user.username
|
||||
it.email to user.email
|
||||
it.password to user.password
|
||||
it.username to username
|
||||
it.email to email
|
||||
it.password to hashedPassword
|
||||
where {
|
||||
it.id eq userId
|
||||
}
|
||||
@@ -104,6 +105,3 @@ class UserService(override val kodein: Kodein) : KodeinAware {
|
||||
}
|
||||
}
|
||||
|
||||
data class UserSchema(val id: Int, val username: String, val email: String, val password: String)
|
||||
data class UserDto(val username: String, val email: String, val password: String)
|
||||
data class UserInfoDto(val username: String, val email: String)
|
||||
|
||||
Reference in New Issue
Block a user