Just like we did when creating a product, we can update the product by hitting the /product/{ProductId} endpoint. We specify the request type PUT using the -X flag with the curl command.
Update the lang of the product created by executing the command below,
xxxxxxxxxx
curl -d '{"id": "43992e94-8007-11eb-9439-0242ac130002", "names": [{"name":"A non empty string", "lang": "es"}]}' -H 'Content-Type: application/json' -X PUT localhost:49152/product/43992e94-8007-11eb-9439-0242ac130002
The updateProduct function also uses monadic notation like in the saveProduct function above. The difference is that it deletes all known translations before saving the given ones.
xxxxxxxxxx
override def updateProduct(p: Product): F[Int] = {
val namesSql =
"INSERT INTO names (product_id, lang_code, name) VALUES (?, ?, ?)"
val namesValues = p.names.map(t => (p.id, t.lang, t.name))
val program = for {
dl <- sql"DELETE FROM names WHERE product_id = ${p.id}".update.run
ts <- Update[(ProductId, LanguageCode, ProductName)](namesSql)
.updateMany(namesValues)
} yield dl + ts
program.transact(tx)
}