Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Created November 29, 2011 19:48
Show Gist options
  • Save bwmcadams/1406158 to your computer and use it in GitHub Desktop.
Save bwmcadams/1406158 to your computer and use it in GitHub Desktop.
Play 2.0 Scala + MongoDB via Salat
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))
}
}
@(books: Iterable[controllers.Book])
<ul>
@books.map { book =>
<li>@book.title by @book.author.mkString(", ")</li>
}
</ul>
# 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)
{
"_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"
}
@mariussoutier
Copy link

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