How can I merge these two Coffee
queries into one and indicate whether or not it's coffee
or otherCoffee
?
Code:
protected lazy val coffeeQuery = for {
id <- Parameters[Long]
coffee <- CoffeesTable if coffee.userId === id
} yield (coffee)
protected lazy val otherCoffeeQuery = for {
id <- Parameters[Long]
employment <- EmployeesTable if employment.employedId === id
sup <- SuppliersTable if employment.supId === sup.id
coffee <- CoffeesTable if (coffee.userId === id || coffee.userId === sup.userId)
} yield (otherCoffee)
Now I would like to get the list of coffees & distinguish whether it's the otherCoffee
or not:
def coffeeList(userId: Long): JValue = withSession { implicit db: Session =>
val coffee = coffeeQuery(userId)
val otherCoffee = otherCoffeeQuery
val coffeeUnion = coffee union otherCoffee
// How would I efficiently distinguish which item is "coffee" and which is "otherCoffee"?
("coffee" ->
coffeeUnion.map { c =>
("name" -> c.name) ~
("coffee_or_other_coffee" -> // is this coffee or otherCoffee?
}
)
}
I'm open to other ways of tackling this (doesn't have to be Unions)