/src/main/scala/org/steve/resources/ReadResource.scala
Scala | 47 lines | 37 code | 10 blank | 0 comment | 1 complexity | 7e019155108ce6b6a76e67c9e9f881e1 MD5 | raw file
- package org.steve.resources
- import org.scalatra.ScalatraServlet
- import org.steve.utils.CommonConversions._
- import com.mongodb.casbah.commons.MongoDBObject
- class SampleResource extends ScalatraServlet {
- get("/") {
- val names = PersonDAO.find(MongoDBObject()) map { _.name }
- <html>
- <ul>
- { for (name <- names) yield { <li>{name}</li> } }
- </ul>
- <form action='/' method='POST'>
- Name: <input name='name' type='text'/>
- <input type='submit'/>
- </form>
- </html>
- }
- get("/:id") {
- contentType = "application/json"
- val id = params("id")
- PersonDAO.findOneByID(id.toObjectId).toJson
- }
- get("/favicon.ico") {
- halt(404)
- }
- get("/hello") {
- "world"
- }
- post("/") {
- val name = params("name")
- val person = Person(name = name)
- PersonDAO.insert(person) match {
- case None => halt(400, "Error creating person")
- case Some(id) => redirect(id.toString)
- }
- }
- }