--- a/src/main/scala/Main.scala Thu Aug 25 12:07:34 2011 -0400
+++ b/src/main/scala/Main.scala Thu Aug 25 12:22:13 2011 -0400
@@ -66,11 +66,11 @@
case req @ Path(Seg("user" :: user :: "workouts" :: Nil)) => req match {
case GET(_) & Host(host) => {
def format(workout:Workout) = {
- import workout._
+ import workout.{user => u, _}
val uri = "%s://%s/user/%s/workout/%s" format (req.underlying.getScheme, host, user, id)
- """{ "activity": "%s", "date": %d, "duration": %d, "distance": %f, "json": "%s", "gpx": "%s" }""" format (activity, date, distance, 12.0, uri+".json", uri+".gpx")
+ """{ "activity": "%s", "user": %s, "date": %d, "duration": %d, "distance": %f, "json": "%s", "gpx": "%s" }""" format (activity, user, date, distance, 12.0, uri+".json", uri+".gpx")
}
- val jsonResponse = WorkoutDAO.find(ref = MongoDBObject()) map { w => format(w) } mkString ("[ ", ", ", " ]")
+ val jsonResponse = WorkoutDAO.getAllWorkoutsByUser(user) map { w => format(w) } mkString ("[ ", ", ", " ]")
Ok ~> ContentType("application/json") ~> ResponseString(jsonResponse)
}
case _ => MethodNotAllowed ~> Allow("GET") ~> ResponseString("MethodNotAllowed...")
--- a/src/main/scala/Workout.scala Thu Aug 25 12:07:34 2011 -0400
+++ b/src/main/scala/Workout.scala Thu Aug 25 12:22:13 2011 -0400
@@ -33,6 +33,7 @@
*/
case class Workout(@Key("_id") id: ObjectId = new ObjectId,
+ user:String,
activity:String,
date: Long,
distance: Long,
@@ -65,6 +66,11 @@
object WorkoutDAO extends SalatDAO[Workout, ObjectId](collection = MongoConnection("gw42.w3.org", 27017)("virtual_trainer")("user.workouts")) {
+ def getAllWorkoutsByUser(user:String):Seq[Workout] = {
+ val results = find(ref = MongoDBObject("user" -> user)).toSeq
+ results
+ }
+
def findOneByID(id:String):Option[Workout] = {
try {
val objectId = new ObjectId(id)
--- a/src/test/scala/WorkoutAPISpec.scala Thu Aug 25 12:07:34 2011 -0400
+++ b/src/test/scala/WorkoutAPISpec.scala Thu Aug 25 12:22:13 2011 -0400
@@ -15,6 +15,7 @@
val workout = """
{
"activity": "http://dbpedia.org/resource/Running",
+ "user": "jdoe",
"date": 1314049555595,
"distance": 5000,
"events":