in software/src/main/kotlin/com/amazonaws/sample/GetAllProductsHandler.kt [27:62]
override fun handleRequest(input: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
val logger = context.logger
val scanResponse = try {
runBlocking {
dynamoDbClient.scan(
ScanRequest {
tableName = productTable
limit = 20
}
)
}
} catch (e: Exception) {
logger.log("ERROR: ${e.message}")
return APIGatewayV2HTTPResponse().apply {
statusCode = 500
headers = mapOf("Content-Type" to "application/json")
body = """{"message": "Failed to get products"}"""
}
}
val products = ArrayList<Product>()
scanResponse.items?.map {
val id = it.getValue("PK").asString
val name = it.getValue("name").asString
val price = it.getValue("price").asFloat
products.add(Product(id, name, price))
}
return APIGatewayV2HTTPResponse().apply {
statusCode = 200
headers = mapOf("Content-Type" to "application/json")
body = Json.encodeToString(Products(products))
}
}