Skip to content

Instantly share code, notes, and snippets.

@ppurang
Last active December 18, 2015 10:19
Show Gist options
  • Save ppurang/5768047 to your computer and use it in GitHub Desktop.
Save ppurang/5768047 to your computer and use it in GitHub Desktop.
spray coffee payment web-service somewhat more complicated example
//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}")
}
}
}
}
}
}
}
@ppurang
Copy link
Author

ppurang commented Jun 12, 2013

To migrate to 1.2-M8 had to

  1. use MediaType.custom instead of CustomMediaType
  2. implement render for a custom header (Allow)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment