June 2018
Intermediate to advanced
310 pages
6h 32m
English
Of course, our test doesn't work. That's because we haven't created our database yet. Make sure you run the following line in your command line:
$ createdb cats_db
After we have made sure that our database is up and running, let's implement our first real endpoint.
We'll keep our SQL nicely separated from the actual code. Add this to your ServerVerticle:
private val insert = """insert into cats (name, age) |values (?, ?::integer) RETURNING *""".trimMargin()
We use multiline strings here, with | and trimMargin() to re-align them.
Now use the following code to call this query:
...val db = getDbClient()router.post("/cats").asyncHandler { ctx -> db.queryWithParams(insert, ctx.bodyAsJson.toCat(), { it.handle({ // We'll always ...
Read now
Unlock full access