use heromodels::db::{Collection, Db}; use heromodels::models::userexample::user::user_index::{is_active, username}; use heromodels::models::{Comment, User}; use heromodels_core::Model; // Helper function to print user details fn print_user_details(user: &User) { println!("\n--- User Details ---"); println!("ID: {}", user.get_id()); println!("Username: {}", user.username); println!("Email: {}", user.email); println!("Full Name: {}", user.full_name); println!("Active: {}", user.is_active); println!("Created At: {}", user.base_data.created_at); println!("Modified At: {}", user.base_data.modified_at); println!("Comments: {:?}", user.base_data.comments); } // Helper function to print comment details fn print_comment_details(comment: &Comment) { println!("\n--- Comment Details ---"); println!("ID: {}", comment.get_id()); println!("User ID: {}", comment.user_id); println!("Content: {}", comment.content); println!("Created At: {}", comment.base_data.created_at); println!("Modified At: {}", comment.base_data.modified_at); } fn main() { // Create a new DB instance in /tmp/ourdb, and reset before every run let db = heromodels::db::hero::OurDB::new("/tmp/ourdb", true).expect("Can create DB"); println!("Hero Models - Basic Usage Example"); println!("================================"); // Create users with different ID configurations // User 1: With explicit ID let user1 = User::new(Some(1)) .username("johndoe") .email("john.doe@example.com") .full_name("John Doe") .is_active(false) .build(); // User 2: With auto-generated ID let user2 = User::new(None) .username("janesmith") .email("jane.smith@example.com") .full_name("Jane Smith") .is_active(true) .build(); // User 3: With explicit ID let user3 = User::new(Some(3)) .username("willism") .email("willis.masters@example.com") .full_name("Willis Masters") .is_active(true) .build(); // User 4: With explicit ID let user4 = User::new(Some(4)) .username("carrols") .email("carrol.smith@example.com") .full_name("Carrol Smith") .is_active(false) .build(); // Save all users to database db.collection().expect("can open user collection").set(&user1).expect("can set user"); db.collection().expect("can open user collection").set(&user2).expect("can set user"); db.collection().expect("can open user collection").set(&user3).expect("can set user"); db.collection().expect("can open user collection").set(&user4).expect("can set user"); // Retrieve all users from database let db_user1 = db.collection::().expect("can open user collection") .get_by_id(user1.get_id()).expect("can load user").expect("user should exist"); let db_user2 = db.collection::().expect("can open user collection") .get_by_id(user2.get_id()).expect("can load user").expect("user should exist"); let db_user3 = db.collection::().expect("can open user collection") .get_by_id(user3.get_id()).expect("can load user").expect("user should exist"); let db_user4 = db.collection::().expect("can open user collection") .get_by_id(user4.get_id()).expect("can load user").expect("user should exist"); // Print all users retrieved from database println!("\n--- Users Retrieved from Database ---"); println!("\n1. User with explicit ID (1):"); print_user_details(&db_user1); println!("\n2. User with auto-generated ID:"); print_user_details(&db_user2); println!("\n3. User with explicit ID (3):"); print_user_details(&db_user3); println!("\n4. User with explicit ID (4):"); print_user_details(&db_user4); // Demonstrate different ways to retrieve users from the database // 1. Retrieve by username index println!("\n--- Retrieving Users by Different Methods ---"); println!("\n1. By Username Index:"); let stored_users = db .collection::() .expect("can open user collection") .get::("johndoe") .expect("can load stored user"); assert_eq!(stored_users.len(), 1); print_user_details(&stored_users[0]); // 2. Retrieve by active status println!("\n2. By Active Status (Active = true):"); let active_users = db .collection::() .expect("can open user collection") .get::(&true) .expect("can load stored users"); assert_eq!(active_users.len(), 2); for (i, active_user) in active_users.iter().enumerate() { print_user_details(active_user); } // 3. Delete a user and show the updated results println!("\n3. After Deleting a User:"); db.collection::() .expect("can open user collection") .delete_by_id(active_users[0].get_id()) .expect("can delete existing user"); // Show remaining active users let active_users = db .collection::() .expect("can open user collection") .get::(&true) .expect("can load stored users"); println!(" a. Remaining Active Users:"); assert_eq!(active_users.len(), 1); for (i, active_user) in active_users.iter().enumerate() { print_user_details(active_user); } // Show inactive users let inactive_users = db .collection::() .expect("can open user collection") .get::(&false) .expect("can load stored users"); println!(" b. Inactive Users:"); assert_eq!(inactive_users.len(), 2); for (i, inactive_user) in inactive_users.iter().enumerate() { print_user_details(inactive_user); } println!("\n--- User Model Information ---"); println!("User DB Prefix: {}", User::db_prefix()); // Demonstrate comment creation and association with a user println!("\n--- Working with Comments ---"); // 1. Create and save a comment println!("\n1. Creating a Comment:"); let comment = Comment::new(None) .user_id(db_user1.get_id()) // commenter's user ID .content("This is a comment on the user") .build(); db.collection() .expect("can open comment collection") .set(&comment) .expect("can set comment"); // 2. Retrieve the comment from database let db_comment = db .collection::() .expect("can open comment collection") .get_by_id(comment.get_id()) .expect("can load comment") .expect("comment should exist"); println!(" a. Comment Retrieved from Database:"); print_comment_details(&db_comment); // 3. Associate the comment with a user println!("\n2. Associating Comment with User:"); let mut updated_user = db_user1.clone(); updated_user.base_data.add_comment(db_comment.get_id()); db.collection::() .expect("can open user collection") .set(&updated_user) .expect("can set updated user"); // 4. Retrieve the updated user let user_with_comment = db .collection::() .expect("can open user collection") .get_by_id(updated_user.get_id()) .expect("can load user") .expect("user should exist"); println!(" a. User with Associated Comment:"); print_user_details(&user_with_comment); println!("\n--- Model Information ---"); println!("User DB Prefix: {}", User::db_prefix()); println!("Comment DB Prefix: {}", Comment::db_prefix()); }