Created
July 8, 2015 17:21
-
-
Save pjazdzewski1990/b9175393d85a22d90551 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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