Hi all, I am an Android dev experimenting with creating a ktor api, to be consumed by front a front end. I cannot figure out how to get a list of a custom object would anybody be able to connect with me and let me know? I have read the docs and looked around for examples but I do not see any for my usecase. Any help would be appreciated.
r
Ruben Holen
11/03/2023, 3:21 AM
First off, I recommend asking in #ktor for specific help about Ktor issues. Secondly, I think it's a good idea to separate the data you load from the database from the data you respond with to a client. Here's a simple example:
Copy code
object FooTable : IntIdTable("foo") {
val bar = text("bar")
val baz = text("baz")
}
class FooRow(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<FooRow>(FooTable)
var bar by FooTable.bar
var baz by FooTable.baz
}
data class FooDto(
val bar: String,
val baz: String
)
fun main() {
val foos = FooRow.all().toList()
val responseDto = foos.map { FooDto(it.bar, it.baz) }
println(responseDto)
}
❤️ 1
j
James
11/03/2023, 10:25 AM
Makes sense thanks for that, I will play around with it more using your example then if I am still stuck go to the ktor chat