Skip to content

Instantly share code, notes, and snippets.

@guillaumebort
Last active December 17, 2015 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaumebort/5620399 to your computer and use it in GitHub Desktop.
Save guillaumebort/5620399 to your computer and use it in GitHub Desktop.
The sample dataset comes from http://www.analytica.com/learn/sample-datasets/
# This is the main configuration file for the application.
# ~~~~~
# Secret key
# ~~~~~
# The secret key is used to secure cryptographics functions.
# If you deploy your application to several instances be sure to use the same key!
application.secret="kyu18P@AD_vV/H^U;LV:6U6fG2tdLcvdo7oy/ZZJpO1A6slw40DtMWC9OP@Lwa`b"
# The application languages
# ~~~~~
application.langs="en"
# MongoDB
# ~~~~~~~
mongodb.uri ="mongodb://localhost:27017/so"
# Global object class
# ~~~~~
# Define the Global object class for this application.
# Default to Global in the root package.
# application.global=Global
# Router
# ~~~~~
# Define the Router object to use for this application.
# This router will be looked up first when the application is starting up,
# so make sure this is the entry point.
# Furthermore, it's assumed your route file is named properly.
# So for an application router like `my.application.Router`,
# you may need to define a router file `conf/my.application.routes`.
# Default to Routes in the root package (and conf/routes)
# application.router=my.application.Routes
# Database configuration
# ~~~~~
# 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.user=sa
# db.default.password=""
# Evolutions
# ~~~~~
# You can disable evolutions if needed
# evolutionplugin=disabled
# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
# Root logger:
logger.root=ERROR
# Logger used by the framework:
logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG
package controllers
import play.api._
import play.api.mvc._
import play.api.templates._
import reactivemongo.api._
import play.modules.reactivemongo._
import play.modules.reactivemongo.json.collection.JSONCollection
import play.api.libs.json._
import play.api.libs.iteratee._
import play.api.libs.functional._
import play.api.libs.functional.syntax._
case class User(id: Int, name: String, location: Option[String], badges: Seq[String] = Nil, otherUsers: Seq[String] = Nil)
object Application extends Controller with MongoController {
private val USERS = db.collection[JSONCollection]("users")
private val BADGES = db.collection[JSONCollection]("badges")
private val badgeJsonAsString = (__ \ "Name").read[String]
private val userJsonAsIntId = (__ \ "UserId").read[Int]
private val userJsonAsDisplayName = (__ \ "DisplayName").read[String]
private val userFromJson = (
(__ \ "Id").read[Int] and
(__ \ "DisplayName").read[String] and
(__ \ "Location").readNullable[String].map(_.filterNot(_.isEmpty))
)(User(_: Int, _: String, _: Option[String]))
def index = Action { request =>
Async(
for(
users <- {
USERS.find(
Json.obj(),
Json.obj(
"Id" -> 1,
"DisplayName" -> 1,
"Location" -> 1,
"_id" -> 0
)
).cursor[JsObject].enumerate.through(Enumeratee.mapM {
case user =>
for {
badges <- BADGES.find(
Json.obj(
"UserId" -> (user \ "Id")
),
Json.obj(
"Name" -> 1,
"_id" -> 0
)
).cursor[JsObject].toList.map(_.map(_.as(badgeJsonAsString)))
usersWithSameBadges <- BADGES.find(
Json.obj(
"Name" -> Json.obj(
"$in" -> badges
)
),
Json.obj(
"UserId" -> 1,
"_id" -> 0
)
).cursor[JsObject].toList.map(_.map(_.as(userJsonAsIntId)))
otherUsers <- USERS.find(
Json.obj(
"Id" -> Json.obj(
"$in" -> usersWithSameBadges
)
),
Json.obj(
"DisplayName" -> 1,
"_id" -> 0
)
).cursor[JsObject].toList.map(_.map(_.as(userJsonAsDisplayName)))
} yield user.as(userFromJson).copy(badges = badges, otherUsers = otherUsers)
}) |>> Iteratee.getChunks
}.flatMap(_.run)
) yield {
Ok(views.html.index(users))
}
)
}
}
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "so-bench"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.9"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)
}
@(users: Seq[User])
@main(users.size + " users found") {
@users.map { user =>
<div>
<h2>@user.name</h2>
<em>@user.location.getOrElse("No location set")</em>, <strong>@user.otherUsers match {
case Nil => { No users share the same badges }
case x :: Nil => { @x shares the same badges }
case x :: y :: Nil => { @x and @y share the same badges }
case x :: others => { @x and @others.size others share the same badges }
}</strong>
@if(user.badges.isEmpty) {
<p>
No badges.
</p>
} else {
<ul>
@user.badges.map { badge =>
<li>@badge</li>
}
</ul>
}
</div>
<hr>
}
}
400:play.modules.reactivemongo.ReactiveMongoPlugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment