Hamza
06/11/2018, 8:21 AMPavlo Liapota
06/11/2018, 8:44 AMprops
, updated it, but never used it. Seems like a bug? 🙂
I guess you can create local function that will get param by name from context and will nullify it if it is empty.
Something like this (I didn’t test):
fun getParam(name: String): String? {
val value = ctx.formParam(name)
return if (value == "") null else value
}
Or you can put this function into context class or make it extension function, whatever makes sense in your case.Hamza
06/11/2018, 8:45 AMHamza
06/11/2018, 8:50 AM<http://app.post|app.post>("/new-book-manual") manual@{ ctx ->
val title = ctx getParam "title"?: return@manual
val author = ctx getParam "author"?: return@manual
val edition = ctx getParam "edition"
val publisher = ctx getParam "publisher"
val subjects = ctx getParam "subjects"
val binding = ctx getParam "binding"
val publishDate = ctx getParam "publish-date"
val overview = ctx getParam "overview"
BookRepo.add(Book(title, author,
edition, publisher,
subjects, binding,
publishDate, overview))
ctx.html("Book has been added!")
}
I simplified it even more with an infix functionsuppoze
06/11/2018, 9:09 AMoperator fun CtxType.get(name: String): String? {
val value = this.formParam(name)
return if (value == "") null else value
}
so you can get the params like this:
val title = ctx["title"] ?: return@manual
val author = ctx["author"] ?: return@manual
val edition = ctx["edition"]
val publisher = ctx["publisher"]
\\ etc
Haven't tried it myself, just wanted to share my idea with you in case you like it 🙂Pavlo Liapota
06/11/2018, 10:22 AMif (value == "") null else value
to separate extension function too 🙂orangy
?: return
is missing on sending back an error htmlorangy
IncompleteDataException
type, ?: throw IncompleteDataException(<paramName>)
and install a generic exception handler to return a standard html page. You can reuse it in other routes, of course.orangy
Bad Request
status code along with HTML, not 200 OK
as you do nowHamza
06/11/2018, 11:09 AMHamza
06/11/2018, 11:12 AM