Skip to content

Instantly share code, notes, and snippets.

@visteras
Last active November 22, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save visteras/6b5c9b38d34cd060995edf97adf60b83 to your computer and use it in GitHub Desktop.
Save visteras/6b5c9b38d34cd060995edf97adf60b83 to your computer and use it in GitHub Desktop.
//dbConfig class:
class DBConfig @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
//trait with standart methods(add, gets listAll, etc) for any tables:
//trait ModelDef[T, MTD <: ModelTableImpl[T]] extends DBConfig {
trait ModelDef[T, MTD <: ModelTableImpl[T]] {
val db = Database.forConfig("default")
// val db = DatabaseConfigProvider.get[JdbcProfile]("default")(Play.routesCompilerMaybeApplication.get).db
// val db = DatabaseConfigProvider.get[JdbcProfile](Play.current).db
val table: TableQuery[MTD]
// val db = dbConfigProvider.get[JdbcProfile].db
def add(row: T): Future[Any] = {
db.run(table.returning(table.map(_.id)) += row).recover {
case ex: Exception => ex.getMessage
}
}
...
}
// object for working with table ApiKey:
object ApiKeys extends ModelDef[ApiKey, ApiKeyTableDef] {
val table = TableQuery[ApiKeyTableDef]
def isActive(key: String): Future[Option[(Long, Boolean)]] = {
val tmp = table.filter(_.apikey === key).map(r => (r.id, r.active)).result.headOption
println(tmp.statements)
db.run(tmp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment