Skip to content

Instantly share code, notes, and snippets.

Created May 31, 2016 15:17
Show Gist options
  • Save anonymous/ad726fd48cd41e47df3b608fd33b1233 to your computer and use it in GitHub Desktop.
Save anonymous/ad726fd48cd41e47df3b608fd33b1233 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]
def saveCoffee(coffee: Coffee) = {
save (coffee, coffees)
}
def listCoffees = {
list(coffees)
}
}
trait Dao extends HasDatabaseConfig[JdbcProfile] {
import driver.api._
def save[A <: AbstractTable[_]](newItem: A#TableElementType, tableQuery: TableQuery[A]) = {
db.run(tableQuery.insertOrUpdate(newItem))
}
def list[A <: AbstractTable[_]](tableQuery: TableQuery[A]) = {
db.run(tableQuery.result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment