Alice
11/27/2018, 7:09 PMpost("/register") {
val newUser = call.receive<NewUser>()
val user = userService.addUser(newUser)
if (user != null) {
call.respond(OutFacingUser(user))
} else {
call.respond("Failed to register")
}
}
where i call a nullable function, and try to recieve an object from json I have not ensured exists. I do a null check and either respond w/ error text or convert the user to an "OutFacingUser" which is just User with the password hash and some other stuff stripped off it.
additionally, the addUser function in my user service is pretty nasty as well
fun addUser(newUser: NewUser): User? {
val nameNotTaken = transaction {
User.find { Users.username eq newUser.username }.none()
}
val emailNotTaken = transaction {
User.find { Users.email eq newUser.email }.none()
}
if (emailNotTaken) { // I could have combined these ifs but I wanted to throw exceptions saying which one was the issue initially and separating the blocks was the easiest way in my mind
if (nameNotTaken) {
return transaction {
User.new {
username = newUser.username
email = newUser.email
passwordHash = authenticationService.hashPassword(newUser.password)
key = ""
createdAt = DateTime()
}
}
}
}
return null
}
I'm sure there's got to be some nice way to handle the Exposed db stuff with arrow, and I'm sure that some category theory fp magic could help get rid of my nested if and possible null return.
Now, kotlin doesn't have the same pattern matching magic scala does I think? In scala I could make a naive improvement by using Optionals and matching for the response which removes the nulls here, but it still doesn't solve the problem of better error handling.
Anyway, I just wanted to give some context and my thoughts with my problem so I can help you help me and show that I'll take you seriously and respect your input 🙂 thanks