Permalink
Browse files

Refactor the record store out of the service.

  • Loading branch information...
1 parent 22332a0 commit 7b4370db48d10d221a493f60604e1d6b11053821 @asnare asnare committed Jul 19, 2011
Showing with 21 additions and 3 deletions.
  1. +18 −0 src/main/scala/com/xebia/rest/RecordStore.scala
  2. +3 −3 src/main/scala/com/xebia/rest/RestService.scala
@@ -0,0 +1,18 @@
+package com.xebia.rest
+
+trait RecordStore {
+ def get(key: Long): Option[Record]
+ def put(key: Long, value: Record)
+}
+
+object HashMapRecordStore extends RecordStore {
+ val records = new collection.mutable.HashMap[Long,Record]()
+
+ def get(key: Long) = {
+ records.get(key)
+ }
+
+ def put(key: Long, value: Record) = {
+ records.put(key, value)
+ }
+}
@@ -10,7 +10,7 @@ import RecordJsonProtocol._
trait RestService extends Directives with SprayJsonMarshalling {
- val records = new collection.mutable.HashMap[Long,Record]()
+ val recordStore: RecordStore = HashMapRecordStore
val restService = {
// Debugging: /ping -> pong
@@ -21,7 +21,7 @@ trait RestService extends Directives with SprayJsonMarshalling {
pathPrefix("rest") {
path("get" / LongNumber) { id =>
get { ctx =>
- records.get(id) match {
+ recordStore.get(id) match {
case Some(record) => ctx.complete(record)
case None => ctx.fail(StatusCodes.NotFound,"Record with id=" + id + " is not in database.")
}
@@ -31,7 +31,7 @@ trait RestService extends Directives with SprayJsonMarshalling {
post {
content(as[Record]) { record =>
if (record.id == id) {
- records += ((id, record))
+ recordStore.put(id, record)
_.complete("")
} else {
_.fail(StatusCodes.Conflict, "The resource ID and ID of the POSTed record do not match.")

0 comments on commit 7b4370d

Please sign in to comment.