PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/Mongo.scala

https://github.com/mostlygeek/PlayByExample
Scala | 49 lines | 30 code | 7 blank | 12 comment | 0 complexity | 9ad1ccfc986ed9eb9b0c03226de01455 MD5 | raw file
  1. package controllers
  2. import play.api._
  3. import play.api.mvc._
  4. import models._
  5. import com.mongodb.casbah.Imports._
  6. /**
  7. * Just some stuff to play around w/ casbah and mongodb
  8. *
  9. * Check project/Build.scala to see that salat + casbah has been
  10. * added as a dependency. Running: > play compile will suck in all
  11. * the necessary libraries to make the mongo driver work...
  12. */
  13. object Mongo extends Controller {
  14. val col = MongoConnection()("testDb")("testCol")
  15. val _id:java.lang.Integer = 1
  16. def index() = Action {
  17. Ok("index")
  18. }
  19. def list() = Action {
  20. Ok("list goes here")
  21. }
  22. /**
  23. * looks for a document within mongo, if it can't find it
  24. * it will create it and tell the user to reload the page.
  25. */
  26. def create(id: String) = Action {
  27. col.findOneByID(id) match {
  28. case None => {
  29. // the += operator allows Docs to be added to mongo easily
  30. col += MongoDBObject("_id" -> id, "created" -> true)
  31. Ok("created: " + id + ", refresh to load record")
  32. }
  33. case _ => Ok("already exists")
  34. }
  35. }
  36. def show(id: String) = Action {
  37. col.findOneByID(id) match {
  38. case None => Ok("Not, Found")
  39. case result => Ok("Found: " + result)
  40. }
  41. }
  42. }