perwendel / spark-kotlin

A Spark DSL in idiomatic kotlin // dependency: com.sparkjava:spark-kotlin:1.0.0-alpha
Apache License 2.0
988 stars 43 forks source link

Support SparkJava's path groups for routes #15

Open v79 opened 7 years ago

v79 commented 7 years ago

It would be useful to support path groups. I've split my code into separate controllers, like so:

class UserController : AbstractController() {
    val users: MutableList<User> = mutableListOf()
    init {
        get("/users") {
                        val model: MutableMap<String,List<User>> = hashMapOf<String,List<User>>()
            model.put("users",users)
            engine.render(ModelAndView(model,"users"))
                }
                post("/users/add-submit") {
            val u:User = User(request.queryParams("name"),request.queryParams("age").toInt())
            users.add(u)
            redirect("/users")
        }
         }
}

Being able to use paths could reduce code duplication:

init {
  path("/users") {
    get("/") {  // e.g. /users/ }
    get("/add") { // e.g. /users/add }
    post("/submit") { // e.g. /users/submit }
  }
}
perwendel commented 6 years ago

We'll try to get this into our official release!

FromSi commented 5 years ago

We'll try to get this into our official release!

Did you include it in the official release?