Last active
April 12, 2016 04:47
-
-
Save leandrob13/21e9fd8949c6923f327772ce701bb1b1 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
package user.persistence.repository | |
import user.persistence.tables.UserRegister | |
import user.persistence.tables.UserTables.Users | |
import play.api.Play | |
import play.api.db.slick.DatabaseConfigProvider | |
import scala.concurrent.Future | |
case class UserFilter(name: Option[ String ] = None, lastname: Option[ String ] = None, | |
email: Option[ String ] = None, username: Option[ String ] = None, | |
active: Option[ Boolean ] = None) | |
class UserRepository extends HasDatabaseConfigProvider[JdbcProfile] with SlickQueryOps { | |
val dbConfigProvider = DatabaseConfigProvider.get[JdbcProfile](Play.current) | |
import driver.api._ | |
val users = TableQuery[ Users ] | |
implicit val ec: ExecutionContext = | |
ExecutionContext.fromExecutorService( Executors.newFixedThreadPool( 10 ) ) | |
def find( filter: UserFilter ): Future[ Option[ UserRegister ] ] = { | |
val usuarioQuery = users.foundBy( List( u => filter.username.map( u.username === _ ), | |
u => filter.email.map( u.email === _) ) )( _ || _ ) | |
db.run( usuarioQuery.result.map( _.headOption) ) | |
} | |
def list( filter: UserFilter ): Future[ List[ UserRegister ] ] = { | |
val u = users.filteredBy( filter.active )( _.active === _ ) | |
.filteredBy( filter.name )( _.name like _ ) | |
.filteredBy( filter.lastname )( _.lastname like _ ) | |
db.run( u.result.map( _.toList ) ) | |
} | |
} | |
object UserSlickRepository { | |
def apply() = new UserSlickRepository | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment