Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Created November 29, 2011 19:48
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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"
}
@gre
Copy link

gre commented Dec 6, 2011

Is there some code missing?
Wondering what is in util._

Thanks.

@bwmcadams
Copy link
Author

bwmcadams commented Dec 6, 2011 via email

@conikeec
Copy link

Can you check in the entire project which can be executed

@bwmcadams
Copy link
Author

bwmcadams commented Dec 11, 2011 via email

@conikeec
Copy link

I will have to leave it but thanks for the reply and will look forward to the robust sample.

@marcus-downing
Copy link

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.

@bwmcadams
Copy link
Author

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      
    )

}

@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