@@ -3,6 +3,7 @@ use aws_sdk_dynamodb::{Client, config::Credentials, types::AttributeValue};
3
3
use aws_config:: Region ;
4
4
use serde:: { Deserialize , Serialize } ;
5
5
use serde_json:: json;
6
+ use base64;
6
7
7
8
#[ derive( Debug , Deserialize ) ]
8
9
pub struct DynamoCredentials {
@@ -25,6 +26,23 @@ struct ApiResponse {
25
26
data : Option < serde_json:: Value > ,
26
27
}
27
28
29
+ fn convert_json_to_attr_value ( value : & serde_json:: Value ) -> Option < AttributeValue > {
30
+ match value {
31
+ serde_json:: Value :: String ( s) => Some ( AttributeValue :: S ( s. clone ( ) ) ) ,
32
+ serde_json:: Value :: Number ( n) => Some ( AttributeValue :: N ( n. to_string ( ) ) ) ,
33
+ serde_json:: Value :: Bool ( b) => Some ( AttributeValue :: Bool ( * b) ) ,
34
+ serde_json:: Value :: Null => Some ( AttributeValue :: Null ( true ) ) ,
35
+ serde_json:: Value :: Array ( arr) => Some ( AttributeValue :: L (
36
+ arr. iter ( ) . filter_map ( |v| convert_json_to_attr_value ( v) ) . collect ( )
37
+ ) ) ,
38
+ serde_json:: Value :: Object ( map) => Some ( AttributeValue :: M (
39
+ map. iter ( ) . filter_map ( |( k, v) | {
40
+ convert_json_to_attr_value ( v) . map ( |av| ( k. clone ( ) , av) )
41
+ } ) . collect ( )
42
+ ) ) ,
43
+ }
44
+ }
45
+
28
46
#[ tauri:: command]
29
47
pub async fn dynamo_api (
30
48
window : tauri:: Window ,
@@ -65,7 +83,8 @@ pub async fn dynamo_api(
65
83
let table_info = json ! ( {
66
84
"id" : response. table( ) . and_then( |t| t. table_id( ) ) ,
67
85
"name" : response. table( ) . map( |t| t. table_name( ) ) ,
68
- "status" : response. table( ) . and_then( |t| t. table_status( ) . map( |s| s. as_str( ) . to_string( ) ) ) , "itemCount" : response. table( ) . and_then( |t| t. item_count( ) ) ,
86
+ "status" : response. table( ) . and_then( |t| t. table_status( ) . map( |s| s. as_str( ) . to_string( ) ) ) ,
87
+ "itemCount" : response. table( ) . and_then( |t| t. item_count( ) ) ,
69
88
"sizeBytes" : response. table( ) . and_then( |t| t. table_size_bytes( ) ) ,
70
89
"keySchema" : response. table( ) . and_then( |t| {
71
90
Some ( t. key_schema( ) . iter( ) . map( |k| {
@@ -146,18 +165,79 @@ pub async fn dynamo_api(
146
165
} )
147
166
}
148
167
} ,
149
- "put_item" => {
150
- if let Some ( _payload) = & options. payload {
151
- // Implementation for put_item would go here
152
- Ok ( ApiResponse {
153
- status : 200 ,
154
- message : "Item put successfully" . to_string ( ) ,
155
- data : None ,
156
- } )
168
+ "CREATE_ITEM" => {
169
+ if let Some ( payload) = & options. payload {
170
+ // Expecting payload to have an "attributes" array
171
+ if let Some ( attributes) = payload. get ( "attributes" ) . and_then ( |v| v. as_array ( ) ) {
172
+ let mut put_item = client. put_item ( ) . table_name ( & options. table_name ) ;
173
+
174
+ for attr in attributes {
175
+ if let ( Some ( key) , Some ( value) , Some ( attr_type) ) = (
176
+ attr. get ( "key" ) . and_then ( |v| v. as_str ( ) ) ,
177
+ attr. get ( "value" ) ,
178
+ attr. get ( "type" ) . and_then ( |v| v. as_str ( ) ) ,
179
+ ) {
180
+ let attr_value = match attr_type {
181
+ "S" => value. as_str ( ) . map ( |s| AttributeValue :: S ( s. to_string ( ) ) ) ,
182
+ "N" => value. as_f64 ( ) . map ( |n| AttributeValue :: N ( n. to_string ( ) ) ) ,
183
+ "B" => value. as_str ( ) . map ( |s| AttributeValue :: B (
184
+ aws_sdk_dynamodb:: primitives:: Blob :: new ( base64:: decode ( s) . unwrap_or_default ( ) )
185
+ ) ) ,
186
+ "BOOL" => value. as_bool ( ) . map ( AttributeValue :: Bool ) ,
187
+ "NULL" => Some ( AttributeValue :: Null ( true ) ) ,
188
+ "SS" => value. as_array ( ) . map ( |arr| {
189
+ AttributeValue :: Ss ( arr. iter ( ) . filter_map ( |v| v. as_str ( ) . map ( |s| s. to_string ( ) ) ) . collect ( ) )
190
+ } ) ,
191
+ "NS" => value. as_array ( ) . map ( |arr| {
192
+ AttributeValue :: Ns ( arr. iter ( ) . filter_map ( |v| v. as_f64 ( ) . map ( |n| n. to_string ( ) ) ) . collect ( ) )
193
+ } ) ,
194
+ "BS" => value. as_array ( ) . map ( |arr| {
195
+ AttributeValue :: Bs ( arr. iter ( ) . filter_map ( |v| v. as_str ( ) . map ( |s| {
196
+ aws_sdk_dynamodb:: primitives:: Blob :: new ( base64:: decode ( s) . unwrap_or_default ( ) )
197
+ } ) ) . collect ( ) )
198
+ } ) ,
199
+ "L" => value. as_array ( ) . map ( |arr| {
200
+ AttributeValue :: L ( arr. iter ( ) . filter_map ( |v| {
201
+ // Recursively convert each element
202
+ convert_json_to_attr_value ( v)
203
+ } ) . collect ( ) )
204
+ } ) ,
205
+ "M" => value. as_object ( ) . map ( |map| {
206
+ AttributeValue :: M ( map. iter ( ) . filter_map ( |( k, v) | {
207
+ convert_json_to_attr_value ( v) . map ( |av| ( k. clone ( ) , av) )
208
+ } ) . collect ( ) )
209
+ } ) ,
210
+ _ => None ,
211
+ } ;
212
+ if let Some ( av) = attr_value {
213
+ put_item = put_item. item ( key, av) ;
214
+ }
215
+ }
216
+ }
217
+
218
+ match put_item. send ( ) . await {
219
+ Ok ( _) => Ok ( ApiResponse {
220
+ status : 200 ,
221
+ message : "Item created successfully" . to_string ( ) ,
222
+ data : None ,
223
+ } ) ,
224
+ Err ( e) => Ok ( ApiResponse {
225
+ status : 500 ,
226
+ message : format ! ( "Failed to create item: {}" , e) ,
227
+ data : None ,
228
+ } ) ,
229
+ }
230
+ } else {
231
+ Ok ( ApiResponse {
232
+ status : 400 ,
233
+ message : "Attributes array is required" . to_string ( ) ,
234
+ data : None ,
235
+ } )
236
+ }
157
237
} else {
158
238
Ok ( ApiResponse {
159
239
status : 400 ,
160
- message : "Item is required for put_item operation " . to_string ( ) ,
240
+ message : "Item payload is required" . to_string ( ) ,
161
241
data : None ,
162
242
} )
163
243
}
0 commit comments