Skip to content

Commit

Permalink
Merge pull request #29 from t2v/topic_scalikejdbc
Browse files Browse the repository at this point in the history
Changed DB from anorm to scalikejdbc
  • Loading branch information
gakuzzzz committed Feb 21, 2013
2 parents 258e8fa + 2b9565d commit 6cdae81
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 52 deletions.
7 changes: 5 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ object ApplicationBuild extends Build {
.settings(baseSettings: _*)
.settings(
libraryDependencies += jdbc,
libraryDependencies += anorm,
libraryDependencies += "org.mindrot" % "jbcrypt" % "0.3m",
libraryDependencies += "org.mindrot" % "jbcrypt" % "0.3m",
libraryDependencies += "com.github.seratch" %% "scalikejdbc" % "[1.4,)",
libraryDependencies += "com.github.seratch" %% "scalikejdbc-test" % "[1.4,)",
libraryDependencies += "com.github.seratch" %% "scalikejdbc-play-plugin" % "[1.4,)",
libraryDependencies += "com.github.seratch" %% "scalikejdbc-interpolation" % "[1.4,)",
publishLocal := {},
publish := {}
).dependsOn(core, test % "test")
Expand Down
1 change: 0 additions & 1 deletion sample/app/Global.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import play.api._

import models._
import anorm._

object Global extends GlobalSettings {

Expand Down
66 changes: 20 additions & 46 deletions sample/app/models/Account.scala
Original file line number Diff line number Diff line change
@@ -1,76 +1,50 @@
package models

import play.api.db._
import anorm._
import anorm.SqlParser._
import play.api.Play.current
import java.sql.Clob
import org.mindrot.jbcrypt.BCrypt
import scalikejdbc._
import scalikejdbc.SQLInterpolation._

case class Account(id: String, email: String, password: String, name: String, permission: Permission)

object Account {

object Clob {
def unapply(clob: Clob): Option[String] = Some(clob.getSubString(1, clob.length.toInt))
}

implicit val rowToPermission: Column[Permission] = {
Column.nonNull[Permission] { (value, meta) =>
value match {
case Clob("Administrator") => Right(Administrator)
case Clob("NormalUser") => Right(NormalUser)
case _ => Left(TypeDoesNotMatch(
"Cannot convert %s : %s to Permission for column %s".format(value, value.getClass, meta.column)))
}
}
}

val simple = {
get[String]("account.id") ~
get[String]("account.email") ~
get[String]("account.password") ~
get[String]("account.name") ~
get[Permission]("account.permission") map {
case id~email~pass~name~perm => Account(id, email, pass, name, perm)
}
val * = { rs: WrappedResultSet =>
Account(
id = rs.string("id"),
email = rs.string("email"),
password = rs.string("password"),
name = rs.string("name"),
permission = Permission.valueOf(rs.string("permission"))
)
}

def authenticate(email: String, password: String): Option[Account] = {
findByEmail(email).filter { account => BCrypt.checkpw(password, account.password) }
}

def findByEmail(email: String): Option[Account] = {
DB.withConnection { implicit connection =>
SQL("SELECT * FROM account WHERE email = {email}").on(
'email -> email
).as(simple.singleOpt)
DB localTx { implicit s =>
sql"SELECT * FROM account WHERE email = ${email}".map(*).single.apply()
}
}

def findById(id: String): Option[Account] = {
DB.withConnection { implicit connection =>
SQL("SELECT * FROM account WHERE id = {id}").on(
'id -> id
).as(simple.singleOpt)
DB localTx { implicit s =>
sql"SELECT * FROM account WHERE id = ${id}".map(*).single.apply()
}
}

def findAll: Seq[Account] = {
DB.withConnection { implicit connection =>
SQL("select * from account").as(simple.*)
DB localTx { implicit s =>
sql"SELECT * FROM account".map(*).list.apply()
}
}

def create(account: Account) {
DB.withConnection { implicit connection =>
SQL("INSERT INTO account VALUES ({id}, {email}, {pass}, {name}, {permission})").on(
'id -> account.id,
'email -> account.email,
'pass -> BCrypt.hashpw(account.password, BCrypt.gensalt()),
'name -> account.name,
'permission -> account.permission.toString
).executeUpdate()
DB localTx { implicit s =>
import account._
val pass = BCrypt.hashpw(account.password, BCrypt.gensalt())
sql"INSERT INTO account VALUES (${id}, ${email}, ${pass}, ${name}, ${permission.toString})".update.apply()
}
}

Expand Down
10 changes: 10 additions & 0 deletions sample/app/models/Permission.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ package models
sealed trait Permission
case object Administrator extends Permission
case object NormalUser extends Permission

object Permission {

def valueOf(value: String): Permission = value match {
case "Administrator" => Administrator
case "NormalUser" => NormalUser
case _ => throw new IllegalArgumentException()
}

}
9 changes: 8 additions & 1 deletion sample/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ db.default.password=""
# Evolutions
# ~~~~~
# You can disable evolutions if needed
# evolutionplugin=disabled
#evolutionplugin=disabled

# Logger
# ~~~~~
Expand All @@ -45,3 +45,10 @@ logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG

scalikejdbc.global.loggingSQLAndTime.enabled=true
scalikejdbc.global.loggingSQLAndTime.logLevel=debug
scalikejdbc.global.loggingSQLAndTime.warningEnabled=true
scalikejdbc.global.loggingSQLAndTime.warningThresholdMillis=1000
scalikejdbc.global.loggingSQLAndTime.warningLogLevel=warn

dbplugin=disabled
1 change: 1 addition & 0 deletions sample/conf/play.plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9999:scalikejdbc.PlayPlugin
4 changes: 2 additions & 2 deletions sample/test/IntegrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IntegrationSpec extends Specification {

"Application" should {

"work from within a browser" in {
"work from within a browser" in new WithApplication {
running(TestServer(3333), HTMLUNIT) { browser =>

// login failed
Expand Down Expand Up @@ -37,7 +37,7 @@ class IntegrationSpec extends Specification {
}
}

"authorize" in {
"authorize" in new WithApplication {
running(TestServer(3333), HTMLUNIT) { browser =>

// login succeded
Expand Down

0 comments on commit 6cdae81

Please sign in to comment.