Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better slick dynamic query coding style

Tags:

scala

slick

private def buildQuery(query: TweetQuery) = {
      var q = Tweets.map { t =>
        t
      }
      query.isLocked.foreach { isLocked =>
        q = q.filter(_.isLocked === isLocked)
      }
      query.isProcessed.foreach { isProcessed =>
        q = q.filter(_.processFinished === isProcessed)
      }
      query.maxScheduleAt.foreach { maxScheduleAt =>
        q = q.filter(_.expectScheduleAt < maxScheduleAt)
      }
      query.minScheduleAt.foreach { minScheduleAt =>
        q = q.filter(_.expectScheduleAt > minScheduleAt)
      }
      query.status.foreach { status =>
        q = q.filter(_.status === status)
      }
      query.scheduleType.foreach { scheduleType =>
        q = q.filter(_.scheduleType === scheduleType)
      }
      q
    }

I am writing things like above to do dynamic query. really boring, any way better to do this ?

like image 523
jilen Avatar asked Jan 11 '23 03:01

jilen


1 Answers

Maybe the MaybeFilter can help you https://gist.github.com/cvogt/9193220

like image 104
cvogt Avatar answered Jan 17 '23 14:01

cvogt