Permalink
Browse files

squeryl impl

  • Loading branch information...
1 parent 0195eb6 commit c19966ea3984f001740897b9bef8e825a41f4555 @jamesward committed Apr 28, 2012
Showing with 61 additions and 41 deletions.
  1. +18 −0 app/Global.scala
  2. +24 −14 app/controllers/Application.scala
  3. +5 −23 app/models/Bar.scala
  4. +2 −4 conf/application.conf
  5. +12 −0 conf/evolutions/default/1.sql
View
@@ -0,0 +1,18 @@
+import org.squeryl.adapters.H2Adapter
+import org.squeryl.{Session, SessionFactory}
+import play.api.db.DB
+import play.api.GlobalSettings
+
+import play.api.Application
+
+object Global extends GlobalSettings {
+
+ override def onStart(app: Application) {
+
+ SessionFactory.concreteFactory = Some(() =>
+ Session.create(
+ DB.getConnection()(app),
+ new H2Adapter))
+ }
+
+}
@@ -1,32 +1,42 @@
package controllers
-import play.api.data.Form
-import play.api.data.Forms.{single, text}
-import play.api.mvc.{Action, Controller}
+import play.api.mvc._
+
import com.codahale.jerkson.Json
+import play.api.data.Form
+import play.api.data.Forms.{mapping, text, optional}
-import models.Bar
+import org.squeryl.PrimitiveTypeMode._
+import models.{AppDB, Bar}
object Application extends Controller {
- val barForm = Form(single("name" -> text))
+ val barForm = Form(
+ mapping(
+ "name" -> optional(text)
+ )(Bar.apply)(Bar.unapply)
+ )
def index = Action {
Ok(views.html.index(barForm))
}
- def addBar() = Action { implicit request =>
- barForm.bindFromRequest.value map { name =>
- Bar.create(new Bar(null, name))
- Redirect(routes.Application.index())
- } getOrElse BadRequest
+ def getBars = Action {
+ val json = inTransaction {
+ val bars = from(AppDB.barTable)(bar =>
+ select(bar)
+ )
+ Json.generate(bars)
+ }
+ Ok(json).as(JSON)
}
- def listBars() = Action {
- val bars = Bar.findAll()
- val json = Json.generate(bars)
- Ok(json).as(JSON)
+ def addBar = Action { implicit request =>
+ barForm.bindFromRequest.value map { bar =>
+ inTransaction(AppDB.barTable insert bar)
+ Redirect(routes.Application.index())
+ } getOrElse BadRequest
}
}
View
@@ -1,29 +1,11 @@
package models
-import play.api.db._
-import play.api.Play.current
-import net.vz.mongodb.jackson.{Id, ObjectId}
-import org.codehaus.jackson.annotate.JsonProperty
-import play.modules.mongodb.jackson.MongoDB
-import reflect.BeanProperty
+import org.squeryl.{Schema, KeyedEntity}
-
-class Bar(@ObjectId @Id val id: String,
- @BeanProperty @JsonProperty("name") val name: String) {
- @ObjectId @Id def getId = id;
+case class Bar(name: Option[String]) extends KeyedEntity[Long] {
+ val id: Long = 0
}
-object Bar {
- private lazy val db = MongoDB.collection("bars", classOf[Bar], classOf[String])
-
- def create(bar: Bar) { db.save(bar) }
- def findAll() = { db.find().toArray }
-
- def apply() = {
-
- }
-
- def unapply() = {
-
- }
+object AppDB extends Schema {
+ val barTable = table[Bar]("BAR")
}
View
@@ -22,8 +22,8 @@ application.langs="en"
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
-# db.default.driver=org.h2.Driver
-# db.default.url="jdbc:h2:mem:play"
+db.default.driver=org.h2.Driver
+db.default.url="jdbc:h2:mem:play"
# db.default.user=sa
# db.default.password=
@@ -44,5 +44,3 @@ logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG
-
-mongodb.uri="mongodb://127.0.0.1:27017/play2bars-scala"
@@ -0,0 +1,12 @@
+# --- First database schema
+
+# --- !Ups
+
+create table bar (
+ id SERIAL PRIMARY KEY,
+ name varchar(255) not null
+);
+
+# --- !Downs
+
+drop table if exists bar;

0 comments on commit c19966e

Please sign in to comment.