Reputation: 9621
I have a specific query in slick defined like:
var query = for {
a <- Assets if a.ownerId === partyIdLoggedInUser || a.userId === partyIdLoggedInUser
m <- a.model
l <- a.location
ad <- l.address
sl <- SpecificLocations if sl.assetId === a.id
} yield (a._type ~ m.make ~ m.model ~ a.serialNumber ~
a.linkQualityValue.? ~ a.activationDate.? ~ l.id ~
l.description ~ ad.id ~ ad.street ~ ad.number ~
ad.zipcode ~ ad.city ~ ad.country ~ ad.geoLocationId.? ~
sl.building.? ~ sl.floor.? ~ sl.room.? ~ sl.other.?)
Later, based on some parameters, I do some pattern matchings and apply some additional filters to this query, eg:
query = serialNumber match {
case Some(serialNumber) => query.where(a => a._4 === serialNumber)
case None => query
}
Well, I want to make these checking(s) shorter by using a map:
query = serialNumber.map(s => query.where(a => a._1 === s))
I don't understand why my map solution is not accepted. Do you see any issue?
It says that found Option[Query[....]] but required Query[...]
Upvotes: 1
Views: 233
Reputation: 12783
I think this will work:
query = serialNumber.map(s => query.where(a => a._1 === s)).getOrElse(query)
Upvotes: 3
Reputation: 3771
map
takes Option[A]
to Option[B]
while you want to take Option[A]
to A
. You could use map
and then getOrElse
.
Upvotes: 2