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 5dfba90 commit 07a41a9
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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 @@ -67,21 +67,16 @@ 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();
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);
}
final BatchWriteItemResponse response = dynamoDB.batchWriteItem(request);
log.debug("Saved donations. Consumed capacity: {}", response.consumedCapacity());
}

@Override
Expand Down

0 comments on commit 07a41a9

Please sign in to comment.