score:0

def search(params: ItemSearchParameters): Either[Failure, List[Item]] = {
    try {
      db withDynSession {
        var q = Items.query

        q = params.keywords.map{ k => q.filter(
          i => (i.title like "%" + k + "%")
              || (i.description  like "%" + k + "%")
          )} getOrElse q

        q = params.shopId.map{ sid => q.filter(
          _.shopId === sid
          )} getOrElse q

        Right(q.run.toList)
      }
    } catch {
      case e: SQLException =>
        Left(databaseError(e))
    }
  }

I am not sure it is the best answer because of var q

score:0

As I understood you correct, you want to make a filter by optional fields. Your second attempt is quiet closer to reality, the first has incorrect matching, you compare option fields to non option. You've answered your own answer while I was writing this response :)

I'd like to recommend you this MaybeFilter https://gist.github.com/cvogt/9193220

Or here is modified version: https://github.com/neowinx/hello-slick-2.1-dynamic-filter/blob/master/src/main/scala/HelloSlick.scala#L3-L7

Maybe this can help you to solve your problem in a more generic way.


Related Query

More Query from same tag