Skip to content

Instantly share code, notes, and snippets.

@billydh
Created November 8, 2020 02:46
Show Gist options
  • Select an option

  • Save billydh/f2b8e6ea482b2765aacb6bef6fad6a2c to your computer and use it in GitHub Desktop.

Select an option

Save billydh/f2b8e6ea482b2765aacb6bef6fad6a2c to your computer and use it in GitHub Desktop.
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient
import software.amazon.awssdk.services.dynamodb.model.*
import java.util.concurrent.CompletableFuture
class CustomerRepo(private val client: DynamoDbAsyncClient,
private val customerTableName: String) {
fun saveCustomer(customer: CustomerPersist): CompletableFuture<PutItemResponse> {
val putItemRequest = PutItemRequest.builder()
.item(
mapOf(
"customerId" to AttributeValue.builder().s(customer.customerId).build(),
"emailAddress" to AttributeValue.builder().s(customer.emailAddress).build(),
"firstName" to AttributeValue.builder().s(customer.firstName).build(),
"lastName" to AttributeValue.builder().s(customer.lastName).build()
)
)
.tableName(customerTableName)
.build()
return client.putItem(putItemRequest)
}
fun retrieveCustomer(customerId: String): CustomerPersist {
val getItemRequest = GetItemRequest.builder()
.key(mapOf("customerId" to AttributeValue.builder().s(customerId).build()))
.tableName(customerTableName)
.build()
return client.getItem(getItemRequest).get()
.let { getItemResponse: GetItemResponse ->
CustomerPersist(
customerId,
(getItemResponse.item()["emailAddress"] ?: error("emailAddress N/A")).s(),
(getItemResponse.item()["firstName"] ?: error("firstName N/A")).s(),
(getItemResponse.item()["lastName"] ?: error("lastName N/A")).s())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment