PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/org/steve/resources/ReadResource.scala

http://sample-scala.googlecode.com/
Scala | 47 lines | 37 code | 10 blank | 0 comment | 1 complexity | 7e019155108ce6b6a76e67c9e9f881e1 MD5 | raw file
  1. package org.steve.resources
  2. import org.scalatra.ScalatraServlet
  3. import org.steve.utils.CommonConversions._
  4. import com.mongodb.casbah.commons.MongoDBObject
  5. class SampleResource extends ScalatraServlet {
  6. get("/") {
  7. val names = PersonDAO.find(MongoDBObject()) map { _.name }
  8. <html>
  9. <ul>
  10. { for (name <- names) yield { <li>{name}</li> } }
  11. </ul>
  12. <form action='/' method='POST'>
  13. Name: <input name='name' type='text'/>
  14. <input type='submit'/>
  15. </form>
  16. </html>
  17. }
  18. get("/:id") {
  19. contentType = "application/json"
  20. val id = params("id")
  21. PersonDAO.findOneByID(id.toObjectId).toJson
  22. }
  23. get("/favicon.ico") {
  24. halt(404)
  25. }
  26. get("/hello") {
  27. "world"
  28. }
  29. post("/") {
  30. val name = params("name")
  31. val person = Person(name = name)
  32. PersonDAO.insert(person) match {
  33. case None => halt(400, "Error creating person")
  34. case Some(id) => redirect(id.toString)
  35. }
  36. }
  37. }