Permalink
Browse files

First hackathon release

  • Loading branch information...
0 parents commit 7fabb1c4e94827374c6a8f7ff2e795f67cbbb7d1 @mariussoutier committed Jun 22, 2012
@@ -0,0 +1,7 @@
+logs
+project/project
+project/target
+target
+tmp
+.history
+dist
4 README
@@ -0,0 +1,4 @@
+This is your new Play 2.0 application
+=====================================
+
+This file will be packaged with your application, when using `play dist`.
@@ -0,0 +1,24 @@
+import play.api._
+import play.api.mvc._
+
+import com.mongodb.casbah.Imports._
+import com.novus.salat.dao._
+import com.novus.salat.annotations._
+import se.radley.plugin.salat._
+
+import models._
+
+object Global extends GlobalSettings {
+
+ override def onStart(app: Application) {
+ if (User.findByEMail("dummy@somewhere.com").isEmpty) {
+ User.save(User(
+ email = Some("dummy@somewhere.com"),
+ firstName = Some("Max"),
+ lastName = Some("Musterbro"),
+ phone = Some("0180-1234567")
+ ))
+ }
+ }
+
+}
@@ -0,0 +1,12 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+
+object Application extends Controller {
+
+ def index = Action {
+ Ok(views.html.index("Your new application is ready."))
+ }
+
+}
@@ -0,0 +1,30 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data.Forms._
+import play.api.data._
+
+import play.api.libs.json._
+
+import com.mongodb.casbah.Imports._
+
+import models._
+
+object Transactions extends Controller {
+
+ def summaries(id: ObjectId) = Action { implicit request =>
+ val others = Transaction.transactedWith(id)
+ val transactionsJs: Seq[JsValue] =
+ others.toSeq.map(otherId =>
+ JsObject(Seq(
+ ("userId", JsString(otherId.toString)),
+ ("userName", JsString("TODO")),
+ ("totalAmount", JsNumber(Transaction.totalAmount(id, otherId))))
+ )
+ )
+
+ Ok(Json.toJson(Map("transactions" -> JsArray(transactionsJs))))
+ }
+
+}
@@ -0,0 +1,45 @@
+package controllers
+
+import play.api._
+import play.api.mvc._
+import play.api.data.Forms._
+import play.api.data._
+
+import play.api.libs.json._
+
+import com.mongodb.casbah.Imports._
+
+import models._
+
+
+object Users extends Controller {
+
+ implicit val UserWrites = new Writes[User] {
+ def writes(user: User): JsValue = {
+ JsObject(fields = Seq(
+ ("id", JsString(user.id.toString)),
+ ("email", JsString(user.email.getOrElse(""))),
+ ("phone", JsString(user.phone.getOrElse(""))),
+ ("firstName", JsString(user.firstName.getOrElse(""))),
+ ("lastName", JsString(user.lastName.getOrElse(""))))
+ )
+ }
+ }
+
+ def dummyUser() = Action { implicit request =>
+ val user = User(
+ email = Some("mps.dev@googlemail.com"),
+ firstName = Some("Marius"),
+ lastName = Some("Soutier"),
+ phone = Some("0124646")
+ )
+ Ok(Json.toJson(user))
+ }
+
+ def userById(id: ObjectId) = Action {
+ User.findOneById(id).map { user =>
+ Ok(Json.toJson(user))
+ }.getOrElse(NotFound)
+ }
+
+}
@@ -0,0 +1,52 @@
+package models
+
+import play.api.Play.current
+
+import com.novus.salat.dao._
+import com.novus.salat.annotations._
+import com.mongodb.casbah.Imports._
+import se.radley.plugin.salat._
+
+import scala.math._
+
+import play.api.libs.json._
+
+// 'from' gives money to 'to'
+case class Transaction(
+ id: ObjectId = new ObjectId,
+ subject: String,
+ amount: BigDecimal,
+ currency: String = "",
+ from: ObjectId,
+ to: ObjectId,
+ created: java.util.Date = new java.util.Date,
+ longitude: Option[Double],
+ latitude: Option[Double],
+ locationName: Option[String]
+) {
+ def fromUser: Option[User] = User.findOneById(from)
+ def toUser: Option[User] = User.findOneById(to)
+}
+
+object Transaction extends ModelCompanion[Transaction, ObjectId] {
+ val collection = mongoCollection("users")
+ val dao = new SalatDAO[Transaction, ObjectId](collection = collection) {}
+
+ def findByUser(userId: ObjectId) =
+ dao.find(MongoDBObject("from" -> userId)).toSeq ++ dao.find(MongoDBObject("to" -> userId)).toSeq
+
+ def transactedWith(userId: ObjectId): Set[ObjectId] = {
+ val usersTransactions = Transaction.findByUser(userId)
+ val froms = usersTransactions.map(_.from).filter(_ != userId).toSet
+ val tos = usersTransactions.map(_.to).filter(_ != userId).toSet
+ froms ++ tos
+ }
+
+ def totalAmount(from: ObjectId, to: ObjectId) = {
+ val iGaveHim: Seq[Transaction] = dao.find(MongoDBObject("from" -> from, "to" -> to)).toSeq
+ val heGaveMe: Seq[Transaction] = dao.find(MongoDBObject("from" -> to, "to" -> from)).toSeq
+ val iGaveHimAmount = iGaveHim.map(_.amount).reduce((tx1, tx2) => tx1 + tx2)
+ val heGaveMeAmount = heGaveMe.map(_.amount).reduce((tx1, tx2) => tx1 + tx2)
+ max(iGaveHimAmount.toDouble, heGaveMeAmount.toDouble) - min(iGaveHimAmount.toDouble, heGaveMeAmount.toDouble)
+ }
+}
@@ -0,0 +1,35 @@
+package models
+
+import play.api.Play.current
+
+import com.novus.salat.dao._
+import com.novus.salat.annotations._
+import com.mongodb.casbah.Imports._
+import se.radley.plugin.salat._
+
+import play.api.libs.json._
+
+case class User(
+ id: ObjectId = new ObjectId,
+ email: Option[String],
+ phone: Option[String],
+ firstName: Option[String],
+ lastName: Option[String]
+)
+
+object User extends ModelCompanion[User, ObjectId] {
+ val collection = mongoCollection("users")
+ val dao = new SalatDAO[User, ObjectId](collection = collection) {}
+
+ def findByEMail(email: String): Option[User] = dao.findOne(MongoDBObject("email" -> email))
+}
+
+
+
+/*implicit object UserFormat extends Format[User] {
+ def read(json: JsValue): User = {
+
+ }
+
+ def writes(o: User)
+}*/
@@ -0,0 +1,9 @@
+package models
+
+import com.novus.salat.dao._
+import com.novus.salat.annotations._
+import com.mongodb.casbah.Imports._
+
+package object db {
+
+}
@@ -0,0 +1,7 @@
+@(message: String)
+
+@main("Welcome to Play 2.0") {
+
+ @play20.welcome(message)
+
+}
@@ -0,0 +1,15 @@
+@(title: String)(content: Html)
+
+<!DOCTYPE html>
+
+<html>
+ <head>
+ <title>@title</title>
+ <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
+ <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
+ <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
+ </head>
+ <body>
+ @content
+ </body>
+</html>
@@ -0,0 +1,36 @@
+# 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="J3k0V^NLB:PNDtVD4qTdju_Bm;GySYRbhIfZ_fVSUYUwDYm3SV3pa3GhHy8XB<bY"
+
+# The application languages
+# ~~~~~
+application.langs="en"
+
+# 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
+
+# Database
+
+dbplugin = disabled
+evolutionplugin = disabled
+
+mongodb.default.db = "iou"
+#mongodb.default.host = "127.0.0.1"
+#mongodb.default.port = 27017
+#mongodb.default.user = "leon"
+#mongodb.default.password = "123456"
@@ -0,0 +1,7 @@
+include "application.conf"
+
+mongodb.default.db = "iou"
+#mongodb.default.host = "127.0.0.1"
+#mongodb.default.port = 27017
+#mongodb.default.user = "leon"
+#mongodb.default.password = "123456"
@@ -0,0 +1 @@
+500:se.radley.plugin.salat.SalatPlugin
@@ -0,0 +1,27 @@
+# Routes
+# This file defines all application routes (Higher priority routes first)
+# ~~~~
+
+# Home page
+GET / controllers.Application.index
+
+# Users
+GET /users/dummy controllers.Users.dummyUser()
+GET /users/:id controllers.Users.userById(id: ObjectId)
+
+GET /users/:id/debts/summary controllers.Transactions.summaries(id: ObjectId)
+#GET /users/:id/debts/detailed
+#GET /debts/:id
+#POST /users
+
+#GET /users controllers.Users.find(email: Option[String], telephone: Option[String])
+#GET /users/email controllers.Users.findByEmail(email: String)
+#GET /users/phone controllers.Users.findByPhone(phone: String)
+#GET /users/facebook controllers.Users.findByFacebook(fbToken: String)
+
+# Transactions
+#GET /transactions/:user1/:user2 controllers.Transactions.txForIds(user1: ObjectId, user2: ObjectId)
+#POST /transactions
+
+# Map static resources from the /public folder to the /assets URL path
+GET /assets/*file controllers.Assets.at(path="/public", file)
@@ -0,0 +1,20 @@
+import sbt._
+import Keys._
+import PlayProject._
+
+object ApplicationBuild extends Build {
+
+ val appName = "iou"
+ val appVersion = "1.0-SNAPSHOT"
+
+ val appDependencies = Seq(
+ "se.radley" %% "play-plugins-salat" % "1.0.4"
+ //"se.radley" %% "play-plugins-enumeration" % "1.0"
+ )
+
+ val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
+ routesImport += "se.radley.plugin.salat.Binders._",
+ templatesImport += "org.bson.types.ObjectId"
+ )
+
+}
@@ -0,0 +1 @@
+sbt.version=0.11.3
@@ -0,0 +1,8 @@
+// Comment to get more information during initialization
+logLevel := Level.Warn
+
+// The Typesafe repository
+resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
+
+// Use the Play sbt plugin for Play projects
+addSbtPlugin("play" % "sbt-plugin" % "2.1-SNAPSHOT")
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Oops, something went wrong.
No changes.
@@ -0,0 +1,33 @@
+package test
+
+import org.specs2.mutable._
+
+import play.api.test._
+import play.api.test.Helpers._
+
+/**
+ * Add your spec here.
+ * You can mock out a whole application including requests, plugins etc.
+ * For more information, consult the wiki.
+ */
+class ApplicationSpec extends Specification {
+
+ "Application" should {
+
+ "send 404 on a bad request" in {
+ running(FakeApplication()) {
+ routeAndCall(FakeRequest(GET, "/boum")) must beNone
+ }
+ }
+
+ "render the index page" in {
+ running(FakeApplication()) {
+ val home = routeAndCall(FakeRequest(GET, "/")).get
+
+ status(home) must equalTo(OK)
+ contentType(home) must beSome.which(_ == "text/html")
+ contentAsString(home) must contain ("Your new application is ready.")
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 7fabb1c

Please sign in to comment.