Last active
July 27, 2020 09:47
-
-
Save iainporter/496c0a91082a72bb9c6cea2dc1b5962c to your computer and use it in GitHub Desktop.
SMS Resource class
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
@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