Skip to content

Commit

Permalink
feat: Updated src/main/java/com/yuriytkach/tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Dec 16, 2023
1 parent a1169de commit ceebaf0
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.awssdk.services.dynamodb.model.PutRequest;
import software.amazon.awssdk.services.dynamodb.model.ScanRequest;
import software.amazon.awssdk.services.dynamodb.model.WriteRequest;
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException;

@Slf4j
@Singleton
Expand Down Expand Up @@ -66,11 +67,21 @@ public void addAll(final String fundId, final Collection<Donation> donations) {
.map(putRequest -> WriteRequest.builder().putRequest(putRequest).build())
.toImmutableSet();

final String conditionExpression = "attribute_not_exists(" + COL_CURR + ") OR " + COL_AMOUNT + " = :value";
final Map<String, AttributeValue> expressionAttributeValues = Map.of(":value", AttributeValue.builder().n(String.valueOf(currentFundTotal)).build());

final BatchWriteItemRequest request = BatchWriteItemRequest.builder()
.requestItems(Map.of(fundId, writeRequests))
.conditionExpression(conditionExpression)
.expressionAttributeValues(expressionAttributeValues)
.build();
final BatchWriteItemResponse response = dynamoDB.batchWriteItem(request);
log.debug("Saved donations. Consumed capacity: {}", response.consumedCapacity());
BatchWriteItemResponse response;
try {
response = dynamoDB.batchWriteItem(request);
log.debug("Saved donations. Consumed capacity: {}", response.consumedCapacity());
} catch (ConditionalCheckFailedException e) {
throw new FundTotalMismatchException("Failed to save donations due to a mismatch in fund total.", e);
}
}

@Override
Expand Down

0 comments on commit ceebaf0

Please sign in to comment.