Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Forked from anonymous/coffee.scala
Last active May 31, 2016 15:32
Show Gist options
  • Save Jacoby6000/1e2408b3de5a7c20952aa41548577372 to your computer and use it in GitHub Desktop.
Save Jacoby6000/1e2408b3de5a7c20952aa41548577372 to your computer and use it in GitHub Desktop.
import play.api.db.slick.HasDatabaseConfig
import slick.driver.JdbcProfile
import slick.lifted.AbstractTable
case class Coffee (name: String, id: Option[Int])
abstract class CoffeeDao extends Dao {
import driver.api._
class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
def name = column[String]("COF_NAME")
def id = column[Int]("ID", O.PrimaryKey)
def * = (name, id.?) <> (Coffee.tupled, Coffee.unapply)
}
val coffees = TableQuery[Coffees]
type TableType = TableQuery[Coffees]
def saveCoffee(coffee: Coffee) = {
save (coffee, coffees)
}
def listCoffees = {
list(coffees)
}
}
trait Dao extends HasDatabaseConfig[JdbcProfile] {
import driver.api._
type TableType <: AbstractTable[_]
type TableElement = TableType#TableElementType
def save(newItem: TableElement, tableQuery: TableQuery[TableType]) = {
db.run(tableQuery.insertOrUpdate(newItem))
}
def list(tableQuery: TableQuery[TableType]) = {
db.run(tableQuery.result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment