Last active
November 22, 2016 09:12
-
-
Save visteras/6b5c9b38d34cd060995edf97adf60b83 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
//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