package models

import javax.inject.Singleton
import javax.inject.Inject
import slick.driver.JdbcProfile
import play.api.db.slick.DatabaseConfigProvider
import play.api.db.slick.HasDatabaseConfig

@Singleton
class UserRepository @Inject() (val provider: DatabaseConfigProvider) extends HasDatabaseConfig[JdbcProfile] with UsersTable {  
  val dbConfig = provider.get[JdbcProfile]
  
  import dbConfig.driver.api._
  import scala.concurrent.ExecutionContext.Implicits.global
  
  def findAll() = db.run(users.result)
  
  def insert(user: User) = db
    .run(users returning users.map(_.id) += user)
    .map(id => user.copy(id = Some(id)))
    
}
