Skip to content

Instantly share code, notes, and snippets.

@cesarferreira
Created November 6, 2019 21:49
Show Gist options
  • Save cesarferreira/ccf1ab1553ea63562fd0feb3a25c55b9 to your computer and use it in GitHub Desktop.
Save cesarferreira/ccf1ab1553ea63562fd0feb3a25c55b9 to your computer and use it in GitHub Desktop.
Clean insert user
// function createUser(user) {
// user.password = bcryptPassword(user.password)
// const userSaved = db.insert(user);
// generateConfirmationEmail(user)
// }
//
//
// function generateConfirmationEmail(user) {
// const key = `${md5(user.email)}.${Random.UUID()}`
// db.insertEmailConfirmation(user, key)
// email.sendConfirmation(userSaved.email);
// }
data class User(
val name: String,
val email: String,
val password: String
)
class FailSendingEmail : Exception()
class FailInsertingUser : Exception()
class Encryptor {
suspend fun encrypt(str: String): String { TODO() }
}
class Emailer {
suspend fun email(content: String, email: String) { TODO() }
}
class DBClient {
suspend fun <T> insert(obj: T) { TODO() }
}
interface UserRepository {
suspend fun sendConfirmationEmail(user: User)
suspend fun insertUser(user: User)
}
class CreateUserUseCase(
private val userRepository: UserRepository,
private val encryptor: Encryptor
) {
suspend fun invoke(name: String, email: String, password: String) {
val user = User(name, email, encryptor.encrypt(password))
try {
userRepository.insertUser(user)
userRepository.sendConfirmationEmail(user)
} catch (fse: FailSendingEmail) {
TODO("handle failure sending email")
} catch (fiu: FailInsertingUser) {
TODO("handle failure inserting user")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment