Last active
December 27, 2015 19:49
-
-
Save tdrozdowski/7379781 to your computer and use it in GitHub Desktop.
Sample
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
// This lists out the initial steps you need to take in your fresh Play project to install the ReactiveMongo plugin | |
// add play.plugins | |
400:play.modules.reactivemongo.ReactiveMongoPlugin | |
// update build.sbt | |
resolvers += "Sonatype Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/" | |
// Below is optional if you wish to build custom modules and include them into your project | |
//resolvers += Resolver.file("Local repo", file("/Users/<user>/.ivy2/local"))(Resolver.ivyStylePatterns) | |
libraryDependencies ++= Seq( | |
cache, | |
"org.reactivemongo" %% "play2-reactivemongo" % "0.10.0-SNAPSHOT" | |
) | |
// update application.conf | |
# mongo settings | |
mongodb.uri="mongodb://reactive:<password>@paulo.mongohq.com:10065/reactive" |
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
// The Complaint Controller - phase 1 | |
package controllers | |
import play.api.mvc.{Action, Controller} | |
import play.modules.reactivemongo.MongoController | |
import scala.concurrent.ExecutionContext.Implicits.global | |
import play.modules.reactivemongo.json.collection.JSONCollection | |
import play.api.libs.json.Json | |
object ComplaintApi extends Controller with MongoController { | |
def complaints = db.collection[JSONCollection]("complaints") | |
def create = Action.async(parse.json) { | |
request => | |
complaints.save(request.body).map { | |
lastError => | |
if (lastError.ok) | |
Ok(Json.obj("status" -> "success")) | |
else | |
BadRequest(Json.obj("status" -> "error", "details" -> s"${lastError.errMsg.getOrElse("No details provided!")}")) | |
} | |
} | |
} |
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
// adding some finder methods | |
def get(id : String) = Action.async { | |
complaints.find(Json.obj("_id" -> Json.obj("$oid" -> id))).one[JsValue].map { | |
maybeComplaint => | |
if (maybeComplaint.isDefined) | |
Ok(maybeComplaint.get) | |
else | |
NotFound(Json.obj("status" -> "failed", "details" -> s"Entity with ID $id was not found!")) | |
} | |
} | |
def listForUser(user : String) = Action.async { | |
val query = Json.obj("email" -> user) | |
complaints.find(query).cursor[JsValue].toList().map(Json.arr(_)).map(Ok(_)).recover { | |
case e:Exception => | |
Logger.debug(s"Error listing for user $user. Details: ${e.getMessage}") | |
BadRequest(Json.obj("status" -> "failed", "details" -> e.getMessage )) | |
} | |
} | |
def remove(id : String) = Action.async { | |
complaints.remove(Json.obj("id" -> Json.obj("$oid" -> id))).map { | |
lastError => | |
if (lastError.ok) | |
Ok(Json.obj("status" -> "success")) | |
else | |
BadRequest(Json.obj("status" -> "failed", "details" -> s"${lastError.errMsg.getOrElse("No Details!")}")) | |
} | |
} | |
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
// a unit test | |
import org.joda.time.DateTime | |
import org.specs2.mutable._ | |
import org.specs2.runner._ | |
import play.api.libs.json.Json | |
import play.api.test._ | |
import play.api.test.Helpers._ | |
/** | |
* Created with IntelliJ IDEA. | |
* User: terry | |
* Date: 11/8/13 | |
* Time: 2:27 PM | |
* | |
*/ | |
class ComplaintApiSpec extends Specification { | |
"ComplaintAPI" should { | |
"create a complaint" in new WithApplication { | |
val complaint = Json.obj("complainee" -> "[email protected]", "dateTime" -> DateTime.now().getMillis, "complaint" -> "just a test - hopefully it works unless last time I tried!") | |
val headers = FakeHeaders(Seq("Content-Type" -> Seq("application/json"))) | |
route(FakeRequest("POST", "/complaints", headers, complaint)).map { | |
results => | |
status(results) must be equalTo(OK) | |
contentType(results) must beSome.which(_ == "application/json") | |
(contentAsJson(results) \ "status").as[String] must equalTo("success") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment