Skip to content

Instantly share code, notes, and snippets.

@codemwnci
Created August 25, 2017 20:54
Show Gist options
  • Save codemwnci/343ee47a46efecddf17320e7ce330143 to your computer and use it in GitHub Desktop.
Save codemwnci/343ee47a46efecddf17320e7ce330143 to your computer and use it in GitHub Desktop.
Update a todo
put(":id") { req, res ->
val update = jacksonObjectMapper().readTree(req.body())
if (!update.has("text") || !update.has("done")) badRequest("text and done required in JSON body string")
val rowsUpdated = using(sessionOf(HikariCP.dataSource())) { session ->
session.run(queryOf("update todo set text=?, done=? where id=?",
update.get("text").asText(), update.get("done").asBoolean(), req.params("id").toLong())
.asUpdate)
}
if (rowsUpdated == 1) jacksonObjectMapper().writeValueAsString(getTodo(req.params("id").toLong()))
else serverError("something went wrong")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment