Skip to the content.

How to create a new route

Steps

  1. Create route tests
  2. Create route endpoint definition
  3. Create route implementation
  4. Add route to service routes

1. Create route tests

TODO

2. Create route endpoint definition

The package [...].infra.route.endpoint contains only the endpoint api definition and the contract model. So, to create a new endpoint definition we have to:

.
└── [...]infra.route.endpoint
    └── [CONTEXT]
      ├── contract
      | └──  [... all models ...]
      └── [CONTEXT]EndpointApi.scala

[Context]EndpointApi

Once create out object should be like this:

private[route] object UserEndpointApi {}

Let’s define our private common endpoint for our context

private val user: Endpoint[Unit, Unit, Unit, Any] =
    VersionedEndpoint.v1.in("user")

Once done we can define our real endpoint, in this example we define an endpoint to get user information by Id

UserEndpointApi.scala


  //new type placed in domain model package
  case class UserId(value: Long) extends AnyVal
  
  
  val getById: Endpoint[UserId, UserEndpointError, UserContract, Any] =
    user.get
      .in(query[UserId]("id"))
      .out(jsonBody[UserContract])
      .errorOut(jsonBody[ErrorInfo])

In our contract package we have our models, both private for route package because we don’t want use these class outside that package

private[route] case class UserContract(id: Long, name: String, surname: String)

private[route] sealed trait UserEndpointError
private[route] object UserEndpointError {
  case class UserNotFound(userId: UserId) extends UserEndpointError
}

2. Create route implementation

TODO

3. Add route to service routes

TODO