Skip to content

Instantly share code, notes, and snippets.

@miere
Last active September 11, 2019 04:12
Show Gist options
  • Save miere/af608c380c266740f0aada48919cc85b to your computer and use it in GitHub Desktop.
Save miere/af608c380c266740f0aada48919cc85b to your computer and use it in GitHub Desktop.

RPC Style Request Mapping

Assuming you have the following model for a user.

data class User(
  val id: UUID = UUID.randomUUID(),
  val name: String,
  val username: String,
  val password: String
)

And you want to insert it into the database, relating the payload to a logged in tenant account. You can do it by mapping the request into a RPC Style payload.

@RequestAware
data class InsertUserReq(
  @Path val tenantId: UUID,
  @Body val user: User
)

@Singleton class App {

  val tenantUsers = mutableMapOf<UUID, MutableList<User>>()

  @deploy
  fun setup(router: SimplifiedRouter){
    router.post('/users', ::insertUser)
  }

  fun insertUser(req: InsertUserReq) {
    val users = tenantUsers.computeIfAbsent(req.tenantId) { mutableListOf() }
    users.add(req.user)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment