Skip to content

Instantly share code, notes, and snippets.

@iainporter
Last active July 27, 2020 09:47
Show Gist options
  • Save iainporter/496c0a91082a72bb9c6cea2dc1b5962c to your computer and use it in GitHub Desktop.
Save iainporter/496c0a91082a72bb9c6cea2dc1b5962c to your computer and use it in GitHub Desktop.
SMS Resource class
@Path("/v1/sms")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
class SmsResource {
@Inject
@field: Default
lateinit var smsService: SmsService
@POST
fun sendSms(@Valid smsRequest: SendSmsRequest, @Context uriInfo: UriInfo): Response {
val message: SmsMessage = smsService.createMessage(smsRequest)
// build the Location header
val builder = uriInfo.absolutePathBuilder
val uriComponents = builder.path("/{id}").build(message.id)
// return a 202 Accepted response with the correct Location header
return Response.accepted().location(uriComponents.normalize()).build()
}
@GET
fun queryForMessages(@QueryParam("status") status: Message.StatusEnum?,
@QueryParam("toNumber") toNumber: String?,
@QueryParam("page") page: Int?,
@QueryParam("pageSize") pageSize: Int?,
@QueryParam("sort") @DefaultValue("updatedAt:desc") sort: String): Response {
val page: Page = Page.of(page ?: 0, pageSize ?: 25)
val status = (if (status != null) MessageStatus.valueOf(status.name) else null)
val results = smsService.getMessages(PageableQuery(page, QueryRequest
.Builder()
.status(status)
.toNumber(toNumber)
.build()))
return Response.ok(results).build()
}
@GET
@Path("/{id}")
fun getMessage(@PathParam("id") id: UUID): Response {
val message: SmsMessage = smsService.getMessage(id)
return Response.ok(message.toMessageResponse()).build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment