Last active
December 18, 2015 10:19
-
-
Save ppurang/5768047 to your computer and use it in GitHub Desktop.
spray coffee payment web-service somewhat more complicated example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//from https://github.com/ppurang/spray-example | |
//below retreive(id) always returns Option[Order] | |
trait CoffeePaymentService extends HttpService with LiftJsonSupport { | |
self: PersistenceCoffee => | |
override implicit def liftJsonFormats: Formats = Order.format | |
val paymentRoute = | |
pathPrefix("payment"/"order") { | |
path(IntNumber) { | |
id => allowed(GET, PUT) ~ | |
get { | |
complete { | |
someOrNotFound(retrieve(id)) | |
} | |
} ~ put { | |
entity(as[Payment]) { | |
payment => complete { | |
val oporder = retrieve(id) | |
oporder.fold[CompletionMagnet](NotFound) { | |
porder => | |
if (porder.status != Option("paid") && Option(payment.amount) == porder.cost) { | |
//todo really? | |
val n = porder.copy(status = Option("paid")) | |
update(id, n) | |
(OK, n) | |
} else { | |
(Conflict, s"order.status: ${porder.status}, order.cost: ${porder.cost}, payment.amount: ${payment.amount}") | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To migrate to 1.2-M8 had to