Created
November 29, 2011 19:48
-
-
Save bwmcadams/1406158 to your computer and use it in GitHub Desktop.
Play 2.0 Scala + MongoDB via Salat
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
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import com.novus.salat._ | |
import com.novus.salat.global._ | |
import com.novus.salat.dao._ | |
import util._ | |
import com.mongodb.casbah.Imports._ | |
case class Book(_id: ObjectId, author: Seq[String], isbn: String, | |
price: Price, publicationYear: Option[Int], tags: Seq[String], | |
title: String, publisher: Option[String], edition: Option[String]) | |
case class Price(currency: String, discount: Double, msrp: Double) | |
object BookDAO extends SalatDAO[Book, ObjectId](collection = MongoConnection()("playbookstore")("books")) | |
object Application extends Controller { | |
// Straight up use of grater | |
def index = Action { | |
val mongo = MongoConnection()("playbookstore")("books") | |
val books = mongo.find() | |
Ok(views.html.index(books.map(book => grater[Book].asObject(book)).toSeq)) | |
} | |
// Use Salat DAO instead, with same template code | |
def dao = Action { | |
val mongo = MongoConnection()("playbookstore")("books") | |
val books = mongo.find() | |
Ok(views.html.index(BookDAO.find(MongoDBObject.empty).toSeq)) | |
} | |
} |
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
@(books: Iterable[controllers.Book]) | |
<ul> | |
@books.map { book => | |
<li>@book.title by @book.author.mkString(", ")</li> | |
} | |
</ul> |
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
# Routes | |
# This file defines all application routes (Higher priority routes first) | |
# ~~~~ | |
# Home page | |
GET / controllers.Application.index() | |
GET /dao controllers.Application.dao() | |
# Map static resources from the /public folder to the /assets URL path | |
GET /assets/*file controllers.Assets.at(path="/public", file) |
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
{ | |
"_id" : ObjectId("4d2a6084c6237b412fcd5597"), | |
"author" : [ | |
"Brian P. Hogan" | |
], | |
"isbn" : "978-1-93435-668-5", | |
"price" : { | |
"currency" : "USD", | |
"discount" : 21.78, | |
"msrp" : 33 | |
}, | |
"publicationYear" : 2010, | |
"tags" : [ | |
"html5", | |
"cascading style sheets", | |
"css", | |
"html", | |
"pragmatic programmer", | |
"silverlight", | |
"web development", | |
"web standards", | |
"css3", | |
"foo" | |
], | |
"title" : "HTML5 and CSS3" | |
} |
Author
bwmcadams
commented
Dec 6, 2011
via email
I had some stuff in util that wasn't used in the final code.
…On Dec 6, 2011 4:55 AM, "Renaudeau Gaetan" < ***@***.***> wrote:
Is there some code missing?
Wondering what is in util._
Thanks.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1406158
Can you check in the entire project which can be executed
That's what I have for now. Take it or leave It. Ill be providing a more
robust sample app at a later date.
…On Dec 11, 2011 1:36 AM, "conikeec" < ***@***.***> wrote:
Can you check in the entire project which can be executed
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1406158
I will have to leave it but thanks for the reply and will look forward to the robust sample.
What's the best way of getting Salat and its dependencies onto the classpath? I'm not familiar with the way Play 2.0 interacts with SBT config.
The same way you add it to any other project: SBT is the build tool for Play 2.0
Here's my sample project's SBT Build:
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "play-mongodb-scala"
val appVersion = "1.0"
val novusRels = "repo.novus rels" at "http://repo.novus.com/releases/"
val appDependencies = Seq(
"com.mongodb.casbah" %% "casbah" % "2.1.5-1",
"com.novus" %% "salat-core" % "0.0.8-SNAPSHOT"
// Add your project dependencies here,
)
val main = PlayProject(appName, appVersion, appDependencies).settings(defaultScalaSettings:_*).settings(
// Add your own project settings here
)
}
Is there a reason you specify Casbah explicitly?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment