Skip to content

Instantly share code, notes, and snippets.

@retrospectacus
Created April 21, 2017 16:23
Show Gist options
  • Save retrospectacus/eaee6051f6ddab9558dd271a73c1075b to your computer and use it in GitHub Desktop.
Save retrospectacus/eaee6051f6ddab9558dd271a73c1075b to your computer and use it in GitHub Desktop.
case class Yard(yardId: Id[Yard], clientId: Id[Client], yardName: String, deleted: Boolean) extends Location with ClientManaged
package domain.slick
import domain.model._
import slick.collection.heterogeneous.HNil
import slick.collection.heterogeneous.syntax._
import slick.driver.PostgresDriver.api._
import slick.lifted.{ProvenShape, Query, TableQuery, Tag}
class Yards(tag: Tag) extends Table[Yard](tag, "yards") {
def yardId = column[Id[Yard]]("yard_id", O.PrimaryKey)
def clientId = column[Id[Client]]("client_id")
def yardName = column[String]("yard_name")
def deleted = column[Boolean]("deleted")
import Yards._
override def * : ProvenShape[Yard] = (yardId :: clientId :: yardName :: deleted :: HNil) <> (applyYard, unapplyYard)
}
object Yards extends {
protected type YardHList = Id[Yard] :: Id[Client] :: String :: Boolean :: HNil
protected def applyYard(data: YardHList): Yard = data match {
case yardId :: clientId :: yardName :: deleted :: HNil => Yard(yardId, clientId, yardName, deleted)
}
protected def unapplyYard(y: Yard): Option[YardHList] = {
Some(y.yardId :: y.clientId :: y.yardName :: y.deleted :: HNil)
}
def table: Query[Yards, Yard, Seq] = TableQuery[Yards]
def byClient(clientId: Id[Client]): Query[Yards, Yard, Seq] = {
table.filter(_.clientId === clientId)
}
def byId(yardId: Id[Yard]): Query[Yards, Yard, Seq] = {
table.filter(_.yardId === yardId)
}
}
package service
import domain.model._
import domain.slick._
import slick.driver.PostgresDriver.api._
import scala.concurrent.Future
class YardService(db: Database) extends GuardingService[Yard] {
override def getEntityOptById(entityId: Id[Yard]): Future[Option[Yard]] =
db.run(Yards.byId(entityId).result.headOption)
def getYard(yardId: Id[Yard]): Future[Yard] =
db.run(Yards.byId(yardId).result.head)
def getYards(clientId: Id[Client]): Future[Seq[Yard]] =
db.run(Yards.byClient(clientId).result)
def deleteYard(yardId: Id[Yard]): Future[Int] =
db.run(Yards.byId(yardId).map(_.deleted).update(true))
def updateYard(yard: Yard): Future[Int] =
db.run(Yards.byId(yard.yardId).update(yard))
def createYard(yard: Yard): Future[Int] =
db.run(Yards.table += yard)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment