Created
March 14, 2012 09:44
-
-
Save mguillermin/2035421 to your computer and use it in GitHub Desktop.
Play 2.0 Anorm Sample
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
case class User(email: String, name: String, password: String) | |
object User { | |
// -- Parsers | |
/** | |
* Parse a User from a ResultSet | |
*/ | |
val simple = { | |
get[String]("user.email") ~ | |
get[String]("user.name") ~ | |
get[String]("user.password") map { | |
case email~name~password => User(email, name, password) | |
} | |
} | |
// -- Queries | |
/** | |
* Retrieve a User from email. | |
*/ | |
def findByEmail(email: String): Option[User] = { | |
DB.withConnection { implicit connection => | |
SQL("select * from user where email = {email}").on( | |
'email -> email | |
).as(User.simple.singleOpt) | |
} | |
} | |
/** | |
* Retrieve all users. | |
*/ | |
def findAll: Seq[User] = { | |
DB.withConnection { implicit connection => | |
SQL("select * from user").as(User.simple *) | |
} | |
} | |
/** | |
* Create a User. | |
*/ | |
def create(user: User): User = { | |
DB.withConnection { implicit connection => | |
SQL( | |
""" | |
insert into user values ( | |
{email}, {name}, {password} | |
) | |
""" | |
).on( | |
'email -> user.email, | |
'name -> user.name, | |
'password -> user.password | |
).executeUpdate() | |
user | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment