diff --git a/services/identity/src/database.rs b/services/identity/src/database.rs --- a/services/identity/src/database.rs +++ b/services/identity/src/database.rs @@ -1,5 +1,7 @@ +use std::collections::HashMap; + use rusoto_core::Region; -use rusoto_dynamodb::DynamoDbClient; +use rusoto_dynamodb::{AttributeValue, DynamoDbClient}; pub struct DatabaseClient { client: DynamoDbClient, @@ -12,3 +14,90 @@ } } } + +type AttributeName = String; + +fn create_simple_primary_key( + partition_key: (AttributeName, String), +) -> HashMap { + HashMap::from([( + partition_key.0, + AttributeValue { + s: Some(partition_key.1), + ..Default::default() + }, + )]) +} + +fn create_composite_primary_key( + partition_key: (AttributeName, String), + sort_key: (AttributeName, String), +) -> HashMap { + let mut primary_key = create_simple_primary_key(partition_key); + primary_key.insert( + sort_key.0, + AttributeValue { + s: Some(sort_key.1), + ..Default::default() + }, + ); + primary_key +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_create_simple_primary_key() { + let partition_key_name = "userID".to_string(); + let partition_key_value = "12345".to_string(); + let partition_key = + (partition_key_name.clone(), partition_key_value.clone()); + let primary_key = create_simple_primary_key(partition_key); + assert_eq!(primary_key.len(), 1); + let attribute = primary_key.get(&partition_key_name); + assert!(attribute.is_some()); + assert_eq!( + attribute, + Some(AttributeValue { + s: Some(partition_key_value), + ..Default::default() + }) + .as_ref() + ); + } + + #[test] + fn test_create_composite_primary_key() { + let partition_key_name = "userID".to_string(); + let partition_key_value = "12345".to_string(); + let partition_key = + (partition_key_name.clone(), partition_key_value.clone()); + let sort_key_name = "deviceID".to_string(); + let sort_key_value = "54321".to_string(); + let sort_key = (sort_key_name.clone(), sort_key_value.clone()); + let primary_key = create_composite_primary_key(partition_key, sort_key); + assert_eq!(primary_key.len(), 2); + let partition_key_attribute = primary_key.get(&partition_key_name); + assert!(partition_key_attribute.is_some()); + assert_eq!( + partition_key_attribute, + Some(AttributeValue { + s: Some(partition_key_value), + ..Default::default() + }) + .as_ref() + ); + let sort_key_attribute = primary_key.get(&sort_key_name); + assert!(sort_key_attribute.is_some()); + assert_eq!( + sort_key_attribute, + Some(AttributeValue { + s: Some(sort_key_value), + ..Default::default() + }) + .as_ref() + ) + } +}