Antonio Acuña Prieto
05/26/2024, 4:09 PMAntonio Acuña Prieto
05/26/2024, 4:09 PMfun Application.configureDI() {
di {
bind<GetAllUsersRepository>() with singleton { GetAllUsersRepository() }
bind<GetAllUsersHandler>() with singleton { GetAllUsersHandler(instance()) }
}
}
fun Application.configureRouting() {
routing {
get("/users", executeInvoke<GetAllUsersHandler>())
}
}
inline fun <reified T : Handler> executeInvoke(): suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit = {
val handler by closestDI().instance<T>()
handler(this.call)
}
interface Handler {
suspend operator fun invoke(call: ApplicationCall)
}
class GetAllUsersHandler(val getAllUsersRepository: GetAllUsersRepository) : Handler {
override suspend fun invoke(call: ApplicationCall) {
val users = getAllUsersRepository()
call.respond(HttpStatusCode.OK, users)
}
}
class GetAllUsersRepository {
operator fun invoke() = listOf("<mailto:test@test.com|test@test.com>", "<mailto:other@other.com|other@other.com>")
}
Sergey Aldoukhov
05/28/2024, 5:18 PMAntonio Acuña Prieto
05/28/2024, 7:57 PMSergey Aldoukhov
05/28/2024, 8:15 PMAntonio Acuña Prieto
05/28/2024, 8:21 PMSergey Aldoukhov
05/28/2024, 8:34 PMAntonio Acuña Prieto
05/28/2024, 10:02 PMSergey Aldoukhov
05/28/2024, 10:36 PMAntonio Acuña Prieto
05/29/2024, 8:24 PMAntonio Acuña Prieto
05/29/2024, 8:37 PMfun Application.module() {
routing {
get("/users") {
this@module.createGetAllUsersHandler()(this.call)
}
}
}
fun Application.createGetAllUsersHandler(): GetAllUsersHandler {
val getAllUsersRepository = GetAllUsersRepository(createJdbi())
return GetAllUsersHandler(getAllUsersRepository)
}
class GetAllUsersHandler(private val getAllUsersRepository: GetAllUsersRepository) {
suspend operator fun invoke(call: ApplicationCall) {
val users = getAllUsersRepository.getAll()
call.respond(HttpStatusCode.OK, users)
}
}
class GetAllUsersRepository(private val jdbi: Jdbi) {
fun getAll() {
return jdbi.withHandleUnchecked { handle ->
handle.createQuery(
"""
select id, email from users
""".trimIndent()
)
.map { rs, _ ->
User(
id = rs.getString("id"),
email = rs.getString("email"),
)
}
.list()
}
}
}
fun Application.createJdbi(): Jdbi {
val dbConfig = DatabaseConfig(
host = environment.config.property("database.url").getString(),
port = environment.config.property("database.url").getString(),
database = environment.config.property("database.url").getString(),
user = environment.config.property("database.username").getString(),
password = environment.config.property("database.password").getString(),
)
return Jdbi.create(
"jdbc:postgresql://$(databaseConfig.host):${dbConfig.port}/${dbConfig.database}?user=${dbConfig.user}&password=${dbConfig.password}"
)
}
data class User(var id: String, var email: String)
data class DatabaseConfig(val host: String, val port: String, val database: String, val user: String, val password: String)
Sergey Aldoukhov
05/29/2024, 8:46 PMfun Application.module() {
...
val state = MyState(...)
...
configureDeviceRest(state)
configureUserRest(state)
configureSupportRest(state)
...
}
in another file:
fun Application.configureUserRest(state: MyState) {
routing {
route("api/user") {
userRestUnauthenticatedAPI(this, state)
}
authenticate("user-auth") {
route("api/user") {
userRestAPI(this, state)
}
}
}
}
private fun userRestAPI(route: Route, state: MyState) {
<http://route.post|route.post>("set_bla_bla") {
...
Sergey Aldoukhov
05/29/2024, 8:49 PMAntonio Acuña Prieto
05/29/2024, 8:54 PMAntonio Acuña Prieto
05/29/2024, 8:55 PMSergey Aldoukhov
05/29/2024, 9:07 PMAntonio Acuña Prieto
05/29/2024, 9:13 PMfun Application.module() {
routing {
get("/users", getAllUsersHandler())
}
}
fun getAllUsersHandler(): suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit {
return {
val getAllUsersRepository = GetAllUsersRepository(application.createJdbi())
val getAllUsersHandler = GetAllUsersHandler(getAllUsersRepository)
getAllUsersHandler(call)
}
}
Sergey Aldoukhov
05/29/2024, 9:18 PMSergey Aldoukhov
05/29/2024, 9:23 PMAntonio Acuña Prieto
05/29/2024, 9:24 PMAntonio Acuña Prieto
05/29/2024, 9:24 PMAntonio Acuña Prieto
05/29/2024, 9:24 PMSergey Aldoukhov
05/29/2024, 9:27 PMAntonio Acuña Prieto
05/29/2024, 9:28 PM