Disclaimer: I did not use Slick, so I just went to their Plain SQL Queries documentation for this.
To answer your first question:
PATCHis just another HTTP verb in your file routes, so for your example:
PATCH /users/:id controllers.UserController.patchById(id)
Yours UserControllercould be something like this:
val possibleUserFields = Seq("firstName", "middleName", "lastName", "age")
def patchById(id:String) = Action(parse.json) { request =>
def addClause(fieldName:String) = {
(request.body \ fieldName).asOpt[String].map { fieldValue =>
s"$fieldName=$fieldValue"
}
}
val clauses = possibleUserFields.flatMap ( addClause )
val updateStatement = "update users set " + clauses.mkString(",") + s" where id = $id"
Ok(s"$updateStatement")
}
What does it do:
Defines a list of JSON field names that may be present in PATCHJSON
Defines an action that will parse the incoming body as JSON
Iterates over all possible field names, checking if they exist in the incoming JSON
If so, add a form clause fieldname=<newValue>to the list
SQL,
, , , ( Slick) Slick, , , Slick, : -)