Skip to content

Instantly share code, notes, and snippets.

@pjazdzewski1990
Created July 8, 2015 17:21
Show Gist options
  • Save pjazdzewski1990/b9175393d85a22d90551 to your computer and use it in GitHub Desktop.
Save pjazdzewski1990/b9175393d85a22d90551 to your computer and use it in GitHub Desktop.
/**
* Update both the number of sold units for a product and total income for a supplier
* Not the best written function WRT missing records, but let's not complicate things ;)
*/
def sellProduct(name: String): DBIO[BigDecimal] = {
val productQuery = products.filter(_.name === name)
for {
(_, supplierId, _, price, unitsSold) <- productQuery.result.map(_.head)
supplierIncomeQuery = suppliers.filter(_.id === supplierId).map(_.income)
income <- supplierIncomeQuery.result.map(_.head)
updatedIncome = income + price
_ <- supplierIncomeQuery.update(updatedIncome)
_ <- productQuery.map(_.unitsSold).update(unitsSold + 1)
} yield updatedIncome
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment