I am looking for a function that manages programma...
# ktor
f
I am looking for a function that manages programmatically the parameters in a GET, to create DB queries dynamically. At the moment, i have the following:
Copy code
route("/sCap"){
    val dao: DAOFacade = DAOFacadeImpl()
    get{      
        val codiceParam = call.request.queryParameters["codice"]
        val listSCap = dao.allSCapFiltered(codiceParam)
        if(listSCap.isEmpty())
            call.respondText("Nessun elemento trovato", status = HttpStatusCode.NotFound)
        else
            call.respond(listSCap)
    }
...
}
and in the DAO:
Copy code
override suspend fun allSCapFiltered(codice: String?): List<SCap> {
    return if(codice.isNullOrEmpty()){
        DatabaseFactory.dbQuery {
            S_Cap.selectAll().map(::resultRowToSCap)
        }
    } else {
        DatabaseFactory.dbQuery {
            S_Cap
                .select { S_Cap.codice like "%$codice%" }
                .map(::resultRowToSCap)
        }
    }
}
is there a way to manage all the parameters expected dynamically, based on properties of data class?
Copy code
data class SCap(val idcap: Int?,
                    val regione: String?,
                    val prov: String?,
                    val comune: String?,
                    val cap: Int?,
                    val codice: String?
                    )
Thanks everyone for suggestions!
a
Do you mean to map query parameters to the properties of the
SCap
data class?
f
yes, map and create the select properly, based on parameters passed in call (only the ones with value)
a
So what is the problem with having functions that transform query parameters to an object of the data class and that object to a select statement?
f
No problem indeed...my problem was that I haven't thought about that simple solution! But what do you suggest to use to do that transformation properly?
In particular, how to create the select properly (I've created a function that maps the Parameters to SCap)
I've tried to create a function that allows me to create a query in a dynamic way, but I miss the part in which I add an "andPart" in the query...here is the partial code:
Copy code
parameters.forEach { it, list ->
    for(property in S_Cap::class.declaredMemberProperties){
        if(property.name == it) {            parameters["$it"]?.let{query.andWhere { ??? }}
        }
    }

}
parameters is of type io.ktor.http.Parameters
any suggestion?