-
-
Save Jacoby6000/1e2408b3de5a7c20952aa41548577372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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