use serde::{Deserialize, Serialize};
use super::{Item, KeyId, NumberAttribute, StringAttribute};
crate::attribute_definitions! {
PK { "PK": StringAttribute }
SK { "SK": StringAttribute }
ItemType { "_TYPE": StringAttribute }
Expiration { "expiration_timestamp": NumberAttribute }
Email { "email": StringAttribute }
}
crate::table_definitions! {
PlatformTable {
type PartitionKey = PK;
type SortKey = SK;
fn table_name() -> String {
std::env::var("TABLE_NAME").unwrap_or_else(|_| "platform".to_owned())
}
}
}
crate::index_definitions! {
#[table = PlatformTable]
TypeIndex {
type PartitionKey = ItemType;
fn index_name() -> String { "iType".to_owned() }
}
#[table = PlatformTable]
EmailIndex {
type PartitionKey = Email;
fn index_name() -> String { "iEmail".to_owned() }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformConfig {
pub max_enrollments: u32,
pub maintenance_mode: bool,
}
crate::dynamodb_item! {
#[table = PlatformTable]
PlatformConfig {
#[partition_key]
PK { const VALUE: &'static str = "PLATFORM_CONFIG"; }
#[sort_key]
SK { const VALUE: &'static str = "PLATFORM_CONFIG"; }
ItemType { const VALUE: &'static str = "PLATFORM_CONFIG"; }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub id: String,
pub name: String,
pub email: String,
pub role: String,
}
crate::dynamodb_item! {
#[table = PlatformTable]
User {
#[partition_key]
PK {
fn attribute_id(&self) -> &'id str { &self.id }
fn attribute_value(id) -> String { format!("USER#{id}") }
}
#[sort_key]
SK { const VALUE: &'static str = "USER"; }
ItemType { const VALUE: &'static str = "USER"; }
#[marker_only]
Email {
fn attribute_id(&self) -> &'id str { &self.email }
fn attribute_value(id) -> String { id.to_owned() }
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Enrollment {
pub user_id: String,
pub course_id: String,
pub enrolled_at: u64,
pub progress: f64,
}
crate::dynamodb_item! {
#[table = PlatformTable]
Enrollment {
#[partition_key]
PK {
fn attribute_id(&self) -> &'id str { &self.user_id }
fn attribute_value(id) -> String { format!("USER#{id}") }
}
#[sort_key]
SK {
fn attribute_id(&self) -> &'id str { &self.course_id }
fn attribute_value(id) -> String { format!("ENROLL#{id}") }
}
ItemType { const VALUE: &'static str = "ENROLLMENT"; }
}
}
pub fn sample_config() -> PlatformConfig {
PlatformConfig {
max_enrollments: 10,
maintenance_mode: false,
}
}
pub fn sample_user() -> User {
User {
id: "user-1".to_owned(),
name: "Alice".to_owned(),
email: "alice@example.com".to_owned(),
role: "student".to_owned(),
}
}
pub fn sample_enrollment() -> Enrollment {
Enrollment {
user_id: "user-1".to_owned(),
course_id: "course-42".to_owned(),
enrolled_at: 1_700_000_000,
progress: 0.0,
}
}
pub fn sample_user_key_id() -> KeyId<&'static str, super::NoId> {
KeyId::pk("user-1")
}
pub fn sample_enrollment_key_id() -> KeyId<&'static str, &'static str> {
KeyId::pk("user-1").sk("course-42")
}
pub fn sample_user_item() -> Item<PlatformTable> {
use super::DynamoDBItem;
sample_user().to_item()
}