in software/src/main/kotlin/com/amazonaws/sample/GetProductHandler.kt [27:60]
override fun handleRequest(event: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
val logger = context.logger
val requestId = event.pathParameters?.get("id") ?: return missingId()
logger.log("INFO: Fetching product [$requestId]")
val response = runBlocking {
dynamoDbClient.getItem(
GetItemRequest {
tableName = productTable
key = mapOf("PK" to AttributeValue.S(requestId))
}
)
}
val id = response.item?.getValue("PK")?.asString
val name = response.item?.getValue("name")?.asString
val price = response.item?.getValue("price")?.asFloat
if (id == null || name == null || price == null) {
return APIGatewayV2HTTPResponse().apply {
statusCode = 500
headers = mapOf("Content-Type" to "application/json")
body = """{"message": "Error fetching product"}"""
}
}
return APIGatewayV2HTTPResponse().apply {
statusCode = 200
headers = mapOf("Content-Type" to "application/json")
body = Json.encodeToString(Product(id, name, price))
}
}