diff --git a/project/build.scala b/project/build.scala index 4e24f8b..d6ffa37 100644 --- a/project/build.scala +++ b/project/build.scala @@ -31,6 +31,7 @@ "commons-io" % "commons-io" % "2.4", "org.pegdown" % "pegdown" % "1.2.1", "org.apache.commons" % "commons-compress" % "1.5", + "com.typesafe.slick" %% "slick" % "1.0.0", "com.h2database" % "h2" % "1.3.171", "ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime", "org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container", diff --git a/src/main/scala/model/Account.scala b/src/main/scala/model/Account.scala new file mode 100644 index 0000000..7412fd3 --- /dev/null +++ b/src/main/scala/model/Account.scala @@ -0,0 +1,39 @@ +package model + +import scala.slick.driver.H2Driver.simple._ + +object Accounts extends Table[Account]("ACCOUNT") { + def userId = column[Long]("USER_ID", O AutoInc) + def userName = column[String]("USER_NAME") + def mailAddress = column[String]("MAIL_ADDRESS") + def password = column[String]("PASSWORD") + def userType = column[Int]("USER_TYPE") + def url = column[String]("URL") + def registeredDate = column[java.sql.Date]("REGISTERED_DATE") // TODO convert java.util.Date later + def updatedDate = column[java.sql.Date]("UPDATED_DATE") + def lastLoginDate = column[java.sql.Date]("LAST_LOGIN_DATE") + def * = userId.? ~ userName ~ mailAddress ~ password ~ userType ~ url.? ~ registeredDate ~ updatedDate ~ lastLoginDate.? <> (Account, Account.unapply _) + def ins = userName ~ mailAddress ~ password ~ userType ~ url.? ~ registeredDate ~ updatedDate ~ lastLoginDate.? <> ({ t => Account(None, t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8)}, { (o: Account) => Some((o.userName, o.mailAddress, o.password, o.userType, o.url, o.registeredDate, o.updatedDate, o.lastLoginDate))}) +} + +case class Account( + userId: Option[Long], + userName: String, + mailAddress: String, + password: String, + userType: Int, + url: Option[String], + registeredDate: java.sql.Date, + updatedDate: java.sql.Date, + lastLoginDate: Option[java.sql.Date] +) + +class AccountDao { + import Database.threadLocalSession + +// def insert(o: Account): Account = Accounts.ins returning Accounts.* insert o + def insert(o: Account): Long = Accounts.ins returning Accounts.userId insert o + + def select(key: Long): Option[Account] = Query(Accounts) filter(_.userId is key.bind) firstOption + +} diff --git a/src/main/scala/servlet/TransactionFilter.scala b/src/main/scala/servlet/TransactionFilter.scala index cfc21ee..df73db5 100644 --- a/src/main/scala/servlet/TransactionFilter.scala +++ b/src/main/scala/servlet/TransactionFilter.scala @@ -3,6 +3,7 @@ import javax.servlet._ import org.slf4j.LoggerFactory import javax.servlet.http.HttpServletRequest +import scala.slick.session.Database /** * Controls the transaction with the open session in view pattern. @@ -21,9 +22,14 @@ chain.doFilter(req, res) } else { // TODO begin transaction! - logger.debug("TODO begin transaction") - chain.doFilter(req, res) - logger.debug("TODO end transaction") + val context = req.getServletContext + Database.forURL(context.getInitParameter("db.url"), + context.getInitParameter("db.user"), + context.getInitParameter("db.password")) withTransaction { + logger.debug("TODO begin transaction") + chain.doFilter(req, res) + logger.debug("TODO end transaction") + } } }