Skip to content

Instantly share code, notes, and snippets.

@retrospectacus
Last active June 14, 2018 19:53
Show Gist options
  • Save retrospectacus/2c2f232337c7b68f42dee3ea92762633 to your computer and use it in GitHub Desktop.
Save retrospectacus/2c2f232337c7b68f42dee3ea92762633 to your computer and use it in GitHub Desktop.
package domain.slick
import domain.model.{Area, Client, Id}
import slick.collection.heterogeneous.HNil
import slick.collection.heterogeneous.syntax._
import slick.driver.PostgresDriver.api._
import slick.lifted.{ProvenShape, Query, TableQuery, Tag}
class Areas(tag: Tag) extends Table[Area](tag, "areas") {
def areaId: Rep[Int] = column[Int]("area_id", O.PrimaryKey)
def clientId: Rep[Int] = column[Int]("client_id")
def areaName: Rep[String] = column[String]("area_name")
def deleted: Rep[Boolean] = column[Boolean]("deleted")
import Areas._
override def * : ProvenShape[Area] = (areaId :: clientId :: areaName :: deleted :: HNil) <>(applyArea, unapplyArea)
}
object Areas {
protected type AreaHList = Int :: Int :: String :: Boolean :: HNil
protected def applyArea(data: AreaHList): Area = data match {
case areaId :: clientId :: areaName :: deleted :: HNil =>
Area(areaId, clientId, areaName, deleted)
}
protected def unapplyArea(s: Area): Option[AreaHList] =
Some(s.areaId :: s.clientId :: s.name :: s.deleted :: HNil)
def table: Query[Areas, Area, Seq] = TableQuery[Areas]
def byClient(clientId: Int): Query[Areas, Area, Seq] =
table.filter(_.clientId === clientId)
def byId(areaId: Int): Query[Areas, Area, Seq] =
table.filter(_.areaId === areaId)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment