+ first version of the workouts endpoint
authorAlexandre Bertails <bertails@w3.org>
Wed, 24 Aug 2011 17:53:46 -0400
changeset 66 b86696030786
parent 65 f959fad73852
child 67 3fb6c55497e4
+ first version of the workouts endpoint
src/main/scala/Main.scala
src/main/scala/Workout.scala
src/test/scala/WorkoutAPISpec.scala
--- a/src/main/scala/Main.scala	Wed Aug 24 15:32:51 2011 -0400
+++ b/src/main/scala/Main.scala	Wed Aug 24 17:53:46 2011 -0400
@@ -40,7 +40,7 @@
     case req @ POST(Path(Seg("user" :: user :: "workout" :: Nil))) & Host(host) => {
       val json:MongoDBObject = com.mongodb.util.JSON.parse(Body.string(req)).asInstanceOf[com.mongodb.DBObject]
       val workout = grater[Workout].asObject(json)
-      RecordDAO.insert(workout) match {
+      WorkoutDAO.insert(workout) match {
         case Some(id) => {
           val path = "%s://%s/user/%s/workout/%s" format (req.underlying.getScheme, host, user, id.toString)
           Created ~> Location(path) ~> ResponseString("object available at %s" format path)
@@ -52,13 +52,29 @@
     }
     case req @ Path(Seg("user" :: user :: "workout" :: id :: Nil)) => req match {
       case GET(_) => {
-        RecordDAO.findOneByID(id) match {
-          case Some(workout) => Ok ~> ContentType("application/json") ~> ResponseString(workout.asJSON)
-          case None => NotFound
+        val realId = id.replaceAll(".json$", "").replaceAll(".gpx$", "")
+        WorkoutDAO.findOneByID(realId) match {
+          case Some(workout) if id endsWith ".gpx" =>
+            Ok ~> ResponseString("TO BE IMPLEMENTED")
+          case Some(workout) if (id endsWith ".json") || (id == realId) =>
+            Ok ~> ContentType("application/json") ~> ResponseString(workout.asJSON)
+          case _ => NotFound
         }
       }
       case _ => MethodNotAllowed ~> Allow("GET") ~> ResponseString("MethodNotAllowed...")
     }
+    case req @ Path(Seg("user" :: user :: "workouts" :: Nil)) => req match {
+      case GET(_) & Host(host) => {
+        def format(workout:Workout) = {
+          import workout._
+          val uri = "%s://%s/user/%s/workout/%s" format (req.underlying.getScheme, host, user, id)
+          """{ "activity": "%s", "time": %d, "duration": %d, "distance": %f, "json": "%s", "gpx": "%s" }""" format (activity, 123456789L, 3600000L, 12.0, uri+".json", uri+".gpx")
+        }
+        val jsonResponse = WorkoutDAO.find(ref = MongoDBObject()) map { w => format(w) } mkString ("[ ", ", ", " ]")
+        Ok ~> ContentType("application/json") ~> ResponseString(jsonResponse)
+      }
+      case _ => MethodNotAllowed ~> Allow("GET") ~> ResponseString("MethodNotAllowed...")
+    }
   }
 
 }
--- a/src/main/scala/Workout.scala	Wed Aug 24 15:32:51 2011 -0400
+++ b/src/main/scala/Workout.scala	Wed Aug 24 17:53:46 2011 -0400
@@ -44,7 +44,7 @@
                  @Key("c") coordinates:Option[(Double, Double, Option[Double])],
                  @Key("s") status:Option[String])
 
-object RecordDAO extends SalatDAO[Workout, ObjectId](collection = MongoConnection("gw42.w3.org", 27017)("virtual_trainer")("user/workouts")) {
+object WorkoutDAO extends SalatDAO[Workout, ObjectId](collection = MongoConnection("gw42.w3.org", 27017)("virtual_trainer")("user/workouts")) {
  
   def findOneByID(id:String):Option[Workout] = {
     try {
--- a/src/test/scala/WorkoutAPISpec.scala	Wed Aug 24 15:32:51 2011 -0400
+++ b/src/test/scala/WorkoutAPISpec.scala	Wed Aug 24 17:53:46 2011 -0400
@@ -4,12 +4,11 @@
 
 import org.specs._
 import java.net.URL
+import unfiltered.response._
+import unfiltered.request._
+import dispatch._
 
 object WorkoutAPISpec extends Specification("Specification for the Workout API") with unfiltered.spec.jetty.Served {
-  import unfiltered.response._
-  import unfiltered.request._
-
-  import dispatch._
 
   def setup = { _.filter(VirtualTrainerApp.workoutAPI) }
 
@@ -52,4 +51,18 @@
     }
   }
   
+}
+
+object PlaySpec extends Specification with unfiltered.spec.jetty.Served {
+
+  def setup = { _.filter(VirtualTrainerApp.workoutAPI) }
+  
+  "GET" should {
+    val get = host / "user" / "jdoe" / "workouts"
+    "return what I want" in {
+      val workouts = Http(get as_str)
+      println(workouts)
+    }
+  }
+  
 }
\ No newline at end of file