Question

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[...]

Was it helpful?

Solution

I think this will work:

query = serialNumber.map(s => query.where(a => a._1 === s)).getOrElse(query)

OTHER TIPS

map takes Option[A] to Option[B] while you want to take Option[A] to A. You could use map and then getOrElse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top