forked from eigengo/akka-patterns
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
sbt/src/main/scala/org/cakesolutions/akkapatterns/api/marshalling.scala
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,34 @@ | |||
package org.cakesolutions.akkapatterns.api | |||
|
|||
import java.util.UUID | |||
import spray.json._ | |||
import spray.httpx.marshalling._ | |||
import org.cakesolutions.akkapatterns.domain._ | |||
import org.cakesolutions.akkapatterns.core.application._ | |||
|
|||
// might move upstream: https://github.com/spray/spray-json/issues/24 | |||
@deprecated("expecting to move upstream", "") trait UuidMarshalling { | |||
implicit object UuidJsonFormat extends JsonFormat[UUID] { | |||
def write(x: UUID) = JsString(x toString ()) | |||
def read(value: JsValue) = value match { | |||
case JsString(x) => UUID.fromString(x) | |||
case x => deserializationError("Expected UUID as JsString, but got " + x) | |||
} | |||
} | |||
} | |||
|
|||
trait Marshalling extends DefaultJsonProtocol with UuidMarshalling { | |||
|
|||
implicit val UserFormat = jsonFormat3(User) | |||
implicit val NotRegisteredUserFormat = jsonFormat1(NotRegisteredUser) | |||
implicit val RegisteredUserFormat = jsonFormat1(RegisteredUser) | |||
|
|||
implicit val AddressFormat = jsonFormat3(Address) | |||
implicit val CustomerFormat = jsonFormat5(Customer) | |||
implicit val RegisterCustomerFormat = jsonFormat2(RegisterCustomer) | |||
implicit val NotRegisteredCustomerFormat = jsonFormat1(NotRegisteredCustomer) | |||
implicit val RegisteredCustomerFormat = jsonFormat2(RegisteredCustomer) | |||
|
|||
implicit val ImplementationFormat = jsonFormat3(Implementation) | |||
implicit val SystemInfoFormat = jsonFormat2(SystemInfo) | |||
} |
29 changes: 29 additions & 0 deletions
29
sbt/src/main/scala/org/cakesolutions/akkapatterns/api/user.scala
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
package org.cakesolutions.akkapatterns.api | |||
|
|||
import akka.actor.ActorSystem | |||
import spray.routing.Directives | |||
import akka.pattern.ask | |||
import spray.httpx.marshalling.MetaMarshallers | |||
import spray.httpx.SprayJsonSupport._ | |||
import org.cakesolutions.akkapatterns.domain.User | |||
import org.cakesolutions.akkapatterns.core.application.{ NotRegisteredUser, RegisteredUser } | |||
|
|||
/** | |||
* @author janmachacek | |||
*/ | |||
class UserService(implicit val actorSystem: ActorSystem) extends Directives with DefaultTimeout with Marshalling with MetaMarshallers { | |||
def userActor = actorSystem.actorFor("/user/application/user") | |||
|
|||
val route = | |||
path("user" / "register") { | |||
post { | |||
entity(as[User]) { user => | |||
complete { | |||
import scala.concurrent.ExecutionContext.Implicits._ | |||
(userActor ? RegisteredUser(user)).mapTo[Either[NotRegisteredUser, RegisteredUser]] | |||
} | |||
} | |||
} | |||
} | |||
|
|||
} |