Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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)) | |||
} | |||
|
|||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,32 +1,42 @@ | |||
package controllers | package controllers | ||
|
|
||
import play.api.data.Form | import play.api.mvc._ | ||
import play.api.data.Forms.{single, text} |
|
||
import play.api.mvc.{Action, Controller} | |||
import com.codahale.jerkson.Json | 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 { | object Application extends Controller { | ||
|
|
||
val barForm = Form(single("name" -> text)) | val barForm = Form( | ||
mapping( | |||
"name" -> optional(text) | |||
)(Bar.apply)(Bar.unapply) | |||
) | |||
|
|
||
def index = Action { | def index = Action { | ||
Ok(views.html.index(barForm)) | Ok(views.html.index(barForm)) | ||
} | } | ||
|
|
||
def addBar() = Action { implicit request => | def getBars = Action { | ||
barForm.bindFromRequest.value map { name => | val json = inTransaction { | ||
Bar.create(new Bar(null, name)) | val bars = from(AppDB.barTable)(bar => | ||
Redirect(routes.Application.index()) | select(bar) | ||
} getOrElse BadRequest | ) | ||
Json.generate(bars) | |||
} | |||
Ok(json).as(JSON) | |||
} | } | ||
|
|
||
def listBars() = Action { | def addBar = Action { implicit request => | ||
val bars = Bar.findAll() | barForm.bindFromRequest.value map { bar => | ||
val json = Json.generate(bars) | inTransaction(AppDB.barTable insert bar) | ||
Ok(json).as(JSON) | Redirect(routes.Application.index()) | ||
} getOrElse BadRequest | |||
} | } | ||
|
|
||
} | } |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,29 +1,11 @@ | |||
package models | package models | ||
|
|
||
import play.api.db._ | import org.squeryl.{Schema, KeyedEntity} | ||
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 | |||
|
|
||
|
case class Bar(name: Option[String]) extends KeyedEntity[Long] { | ||
class Bar(@ObjectId @Id val id: String, | val id: Long = 0 | ||
@BeanProperty @JsonProperty("name") val name: String) { | |||
@ObjectId @Id def getId = id; | |||
} | } | ||
|
|
||
object Bar { | object AppDB extends Schema { | ||
private lazy val db = MongoDB.collection("bars", classOf[Bar], classOf[String]) | val barTable = table[Bar]("BAR") | ||
|
|||
def create(bar: Bar) { db.save(bar) } | |||
def findAll() = { db.find().toArray } | |||
|
|||
def apply() = { | |||
|
|||
} | |||
|
|||
def unapply() = { | |||
|
|||
} | |||
} | } |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; |