val q4 = coffees.filter { coffee => List( criteriaColombian.map(coffee.name === _), criteriaEspresso.map(coffee.name === _), criteriaRoast.map(coffee.name === _) // not a condition as `criteriaRoast` evaluates to `None` ).collect({case Some(criteria) => criteria}).reduceLeftOption(_ || _).getOrElse(true: Rep[Boolean]) }
val ordersFilterByIdQuery = filterId.map { stringValue =>
try {
val intValue = stringValue.toInt
orders.filter(_.id === intValue)
} catch {
case _ => orders
}
}.getOrElse(orders)
val patientsFilterBySurnameQuery = patients.filter { patient =>
List(
filterPatientSurname.map(stringValue => patient.surname.toLowerCase like s"%${stringValue.toLowerCase}%"),
filterPatientSurname.map(stringValue => patient.firstname.toLowerCase like s"%${stringValue.toLowerCase}%"),
filterPatientSurname.map(stringValue => patient.lastname.map(lastnameColumn=> lastnameColumn.toLowerCase like s"%${stringValue.toLowerCase}%").getOrElse(false))
).collect({ case Some(criteria) => criteria }).reduceLeftOption(_ || _).getOrElse(true: Rep[Boolean])
}
val query = for {
os <- ordersFilterByIdQuery
p <- patientsFilterBySurnameQuery if os.patientId === p.id
d <- doctors if os.doctorId === d.id
orgs <- organizations if os.organizationId === orgs.id
} yield (os, p, d, orgs)
db.run(query.result)