I there! I'm following Play-Auth-Slick-Seed example that works excellent! and I'm trying to propose the following model mimicking the User model in there (User.scala, DBTableDefinitions.scala, UserDAOImpl.scala, UserDAO.scala) The model:
Node.scala
case class Node(
nodeID: Option[Long],
name: Option[String],
nodeCode: Option[String],
nodeStamp: Option[Date]
)
In the NodeDAOImpl class I defined the following functions: find, count and create, just to test if I can create the data in my postgres as the app does with users!
def find(nodeID: Long) = {
val nodeQuery = for {
dbNode <- slickNodes.filter(_.id === nodeID)
} yield dbNode
db.run(nodeQuery.result.headOption).map { dbNodeOption =>
dbNodeOption.map { node =>
Node(node.nodeID, node.name,node.nodeCode,node.nodeStamp)
}
}
}
def count(): Future[Int] = {
db.run(slickNodes.length.result)
}
def create(node: Node) = {
val dbNode = DBNode(node.nodeID, node.name, node.nodeCode, node.nodeStamp)
val actions = (for {
_ <- slickNodes.insertOrUpdate(dbNode)
} yield ()).transactionally
db.run(actions).map(_ => node)
}
Now, How can I push some data to the database when app starts? I tried to create a Database.scala in module (similar to SilhouetteModule.scala) and I made the following
class DatabaseModule extends AbstractModule {
def configure() = {
bind[NodeDAO].to[NodeDAOImpl]
}
}
But I can't realize where I can add a kind of function like
StartData.insert()
defined as (many examples suggest)
object StartData {
private def nodeDAOImpl = new NodeDAOImpl
def insert(): Unit = {
nodeDAOImpl.count map { size =>
if (size == 0) {
Logger.info("Insertando nodes");
val nodes = Seq(
Node(Option(1L), Option("Node1"), Option("XMZ12RT"), Option(new java.util.Date)))
)
nodes.map(nodeDAOImpl.create)
}
}
}
}
Thnks for any suggestion or guidance! ReneX