Created
November 8, 2020 02:46
-
-
Save billydh/f2b8e6ea482b2765aacb6bef6fad6a2c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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