Hello,
I've come to stopping point in trying to implement a multiple filter function. This is my class, and the method I'm trying to filter on is the `all` method. For example, in the `all` method I have many optional values that can be passed in, and I want to be able to do `if` statements on all of these `Optional`'s and then `filter` the query result based on that.
import slick.backend.DatabaseConfig
import slick.driver.{MySQLDriver}
import slick.driver.MySQLDriver.api._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Created by rmccullagh on 3/29/17.
*/
object JurisdictionRepo extends {
val profile = slick.driver.MySQLDriver
} with JurisdictionRepo
// RuleRepo is coded to "attach" to an owner
object JurisdictionRuleRepo extends {
val profile = slick.driver.MySQLDriver
val ownerTableName = JurisdictionRepo.tableName
} with RuleRepo
trait JurisdictionRepo {
private val dbConfig: DatabaseConfig[MySQLDriver] = DatabaseConfig.forConfig("pnga-master-data")
private val db = dbConfig.db
val profile: slick.driver.JdbcProfile
val tableName = "jurisdiction"
def add(jurisdictionRow: JurisdictionRow): Future[Unit] = db.run(query += jurisdictionRow).map { _ => () }
def delete(id: String): Future[Int] = db.run(query.filter(_.id === id).delete)
def get(id: String): Future[Option[JurisdictionRow]] = db.run(query.filter(_.id === id).result.headOption)
def update(id: String, jurisdiction: JurisdictionRow) = {
db.run(query.filter(_.id === id).map(row =>
(row.parentId, row.name, row.code, row.currency)
).update(
(jurisdiction.parentId, jurisdiction.name, jurisdiction.code, jurisdiction.currency)
))
}
def all(name: Option[String],
parentId: Option[String],
limit: Option[String],
offset: Option[String],
sortBy: Option[String],
sortDirection: Option[String]) = {
val result = query
if(name.isDefined) {
result = result.filter(_.name === name)
}
db.run(result.result)
}
import profile.api._
lazy val schema: profile.SchemaDescription = query.schema
case class JurisdictionRow(id: String,
parentId: Option[String],
name: String,
code: String,
currency: String)
class Jurisdiction(_tableTag: Tag) extends Table[JurisdictionRow](_tableTag, tableName) {
val id: Rep[String] = column[String](s"${tableName}_id", O.PrimaryKey, O.Length(36, varying=true))
val parentId = column[Option[String]]("parent_id", O.Default(None), O.Length(36, varying=true))
val name: Rep[String] = column[String]("name", O.Length(255, varying=true))
val code: Rep[String] = column[String]("code", O.Length(255, varying=true))
val currency: Rep[String] = column[String]("currency")
def * = (id, parentId, name, code, currency) <> (JurisdictionRow.tupled, JurisdictionRow.unapply _)
}
val jurisdictions = TableQuery[Jurisdiction]
lazy val query = new TableQuery(tag => new Jurisdiction(tag))
}
Notice, the `all` method attempts to filter multiple times, by checking if the `name.isDefined` is true. How can I do this?