33 */
44package io .pratik .dynamodbapps ;
55
6+ import java .util .HashMap ;
7+ import java .util .Iterator ;
8+ import java .util .Map ;
9+
610import org .springframework .beans .factory .annotation .Autowired ;
711import org .springframework .stereotype .Repository ;
812
913import io .pratik .dynamodbapps .models .Order ;
10- import software .amazon .awssdk .auth .credentials .AwsCredentialsProvider ;
11- import software .amazon .awssdk .auth .credentials .DefaultCredentialsProvider ;
1214import software .amazon .awssdk .enhanced .dynamodb .DynamoDbEnhancedClient ;
1315import software .amazon .awssdk .enhanced .dynamodb .DynamoDbTable ;
16+ import software .amazon .awssdk .enhanced .dynamodb .Expression ;
1417import software .amazon .awssdk .enhanced .dynamodb .Key ;
1518import software .amazon .awssdk .enhanced .dynamodb .TableSchema ;
16- import software .amazon .awssdk .regions .Region ;
17- import software .amazon .awssdk .services .dynamodb .DynamoDbClient ;
19+ import software .amazon .awssdk .enhanced .dynamodb .model .DeleteItemEnhancedRequest ;
20+ import software .amazon .awssdk .enhanced .dynamodb .model .PageIterable ;
21+ import software .amazon .awssdk .enhanced .dynamodb .model .QueryConditional ;
22+ import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
1823
1924/**
2025 * @author pratikdas
2126 *
2227 */
2328@ Repository
2429public class OrderRepository {
25-
30+
2631 @ Autowired
27- private DynamoDbEnhancedClient dynamoDbenhancedClient ;
28-
29- private DynamoDbTable <Order > orderTable ;
30-
31-
32+ private DynamoDbEnhancedClient dynamoDbenhancedClient ;
3233
3334 public OrderRepository () {
3435 super ();
@@ -37,31 +38,70 @@ public OrderRepository() {
3738 // Store this order item in the database
3839 public void save (final Order order ) {
3940 DynamoDbTable <Order > orderTable = getTable ();
40-
4141 orderTable .putItem (order );
4242 }
4343
4444 /**
4545 * @return
4646 */
4747 private DynamoDbTable <Order > getTable () {
48- DynamoDbTable <Order > orderTable =
49- dynamoDbenhancedClient .table ("Order" ,
50- TableSchema .fromBean (Order .class ));
48+ DynamoDbTable <Order > orderTable = dynamoDbenhancedClient .table ("Order" , TableSchema .fromBean (Order .class ));
5149 return orderTable ;
5250 }
5351
5452 // Retrieve a single order item from the database
5553 public Order getOrder (final String customerID , final String orderID ) {
5654 DynamoDbTable <Order > orderTable = getTable ();
5755 // Construct the key with partition and sort key
58- Key key = Key .builder ().partitionValue (customerID )
59- .sortValue (orderID )
60- .build ();
61-
56+ Key key = Key .builder ().partitionValue (customerID ).sortValue (orderID ).build ();
57+
6258 Order order = orderTable .getItem (key );
63-
59+
6460 return order ;
6561 }
6662
63+ public void deleteOrder (final String customerID , final String orderID ) {
64+ DynamoDbTable <Order > orderTable = getTable ();
65+
66+ Key key = Key .builder ().partitionValue (customerID ).sortValue (orderID ).build ();
67+
68+ DeleteItemEnhancedRequest deleteRequest = DeleteItemEnhancedRequest
69+ .builder ()
70+ .key (key )
71+ .build ();
72+
73+ orderTable .deleteItem (deleteRequest );
74+ }
75+
76+ public PageIterable <Order > scanOrders (final String customerID , final String orderID ) {
77+ DynamoDbTable <Order > orderTable = getTable ();
78+
79+ return orderTable .scan ();
80+ }
81+
82+ public PageIterable <Order > findOrdersByValue (final String customerID , final double orderValue ) {
83+ DynamoDbTable <Order > orderTable = getTable ();
84+
85+ AttributeValue attributeValue = AttributeValue .builder ()
86+ .n (String .valueOf (orderValue ))
87+ .build ();
88+
89+ Map <String , AttributeValue > expressionValues = new HashMap <>();
90+ expressionValues .put (":value" , attributeValue );
91+
92+ Expression expression = Expression .builder ()
93+ .expression ("orderValue > :value" )
94+ .expressionValues (expressionValues )
95+ .build ();
96+
97+ // Create a QueryConditional object that is used in the query operation
98+ QueryConditional queryConditional = QueryConditional
99+ .keyEqualTo (Key .builder ().partitionValue (customerID )
100+ .build ());
101+
102+ // Get items in the Customer table and write out the ID value
103+ PageIterable <Order > results = orderTable .query (r -> r .queryConditional (queryConditional ).filterExpression (expression ));
104+ return results ;
105+ }
106+
67107}
0 commit comments