Commit 2abd0747 authored by Mathieu's avatar Mathieu
Browse files

Refactor queries

parent 7113f0b6
...@@ -52,7 +52,7 @@ object DB { ...@@ -52,7 +52,7 @@ object DB {
}, Duration.Inf }, Duration.Inf
) )
// val users = Seq(User(Login("foo"), Password("foo"), UUID("foo-123-567-foo")), User(Login("bar"), Password("bar"), UUID("bar-123-567-bar"))) // val users = Seq(User(Login("foo"), Password("foo"), UUID("foo-123-567-foo")), User(Login("bar"), Password("bar"), UUID("bar-123-567-bar")))
def uuid(email: Email): Option[UUID] = users.find(_.email == email).map { def uuid(email: Email): Option[UUID] = users.find(_.email == email).map {
...@@ -70,13 +70,6 @@ object DB { ...@@ -70,13 +70,6 @@ object DB {
actions: _* actions: _*
).transactionally), Duration.Inf) ).transactionally), Duration.Inf)
def runQuery(query: TableQuery[DB.Users]) =
Await.result(
db.run(
query.result
), Duration.Inf
)
def initDB = { def initDB = {
runTransaction(userTable.schema.createIfNotExists) runTransaction(userTable.schema.createIfNotExists)
if (DB.users.isEmpty) { if (DB.users.isEmpty) {
...@@ -84,16 +77,21 @@ object DB { ...@@ -84,16 +77,21 @@ object DB {
} }
} }
def exists(email: Email) = { type UserQuery = Query[Users, (UUID, Email, Password, Role), Seq]
def runQuery(query: UserQuery) =
Await.result( Await.result(
db.run( db.run(
(for { query.result
u <- userTable if (u.email === email) ), Duration.Inf
} yield (u)).result ).map { case (u, e, p, r) => User(e, p, r, u) }
).map {
_.length != 0 def exists(email: Email) = {
}, Duration.Inf runQuery(
) for {
u <- userTable if (u.email === email)
} yield (u)
).length != 0
} }
def addUser(email: Email, password: Password, role: Role = simpleUser) = { def addUser(email: Email, password: Password, role: Role = simpleUser) = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment