Add postgres code
- Set is currently working - Other methods to test Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
		
							
								
								
									
										225
									
								
								heromodels/examples/postgres_model_example.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										225
									
								
								heromodels/examples/postgres_model_example.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,225 @@
 | 
			
		||||
use heromodels::db::postgres::Config;
 | 
			
		||||
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() {
 | 
			
		||||
    let db = heromodels::db::postgres::Postgres::new(
 | 
			
		||||
        Config::new()
 | 
			
		||||
            .user(Some("postgres".into()))
 | 
			
		||||
            .password(Some("test123".into()))
 | 
			
		||||
            .host(Some("localhost".into()))
 | 
			
		||||
            .port(Some(5432)),
 | 
			
		||||
    )
 | 
			
		||||
    .expect("Can connect to postgress");
 | 
			
		||||
 | 
			
		||||
    println!("Hero Models - Basic Usage Example");
 | 
			
		||||
    println!("================================");
 | 
			
		||||
 | 
			
		||||
    // Create users with auto-generated IDs
 | 
			
		||||
 | 
			
		||||
    // User 1
 | 
			
		||||
    let user1 = User::new()
 | 
			
		||||
        .username("johndoe")
 | 
			
		||||
        .email("john.doe@example.com")
 | 
			
		||||
        .full_name("John Doe")
 | 
			
		||||
        .is_active(false)
 | 
			
		||||
        .build();
 | 
			
		||||
 | 
			
		||||
    // User 2
 | 
			
		||||
    let user2 = User::new()
 | 
			
		||||
        .username("janesmith")
 | 
			
		||||
        .email("jane.smith@example.com")
 | 
			
		||||
        .full_name("Jane Smith")
 | 
			
		||||
        .is_active(true)
 | 
			
		||||
        .build();
 | 
			
		||||
 | 
			
		||||
    // User 3
 | 
			
		||||
    let user3 = User::new()
 | 
			
		||||
        .username("willism")
 | 
			
		||||
        .email("willis.masters@example.com")
 | 
			
		||||
        .full_name("Willis Masters")
 | 
			
		||||
        .is_active(true)
 | 
			
		||||
        .build();
 | 
			
		||||
 | 
			
		||||
    // User 4
 | 
			
		||||
    let user4 = User::new()
 | 
			
		||||
        .username("carrols")
 | 
			
		||||
        .email("carrol.smith@example.com")
 | 
			
		||||
        .full_name("Carrol Smith")
 | 
			
		||||
        .is_active(false)
 | 
			
		||||
        .build();
 | 
			
		||||
 | 
			
		||||
    // Save all users to database and get their assigned IDs and updated models
 | 
			
		||||
    let (user1_id, db_user1) = db
 | 
			
		||||
        .collection()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .set(&user1)
 | 
			
		||||
        .expect("can set user");
 | 
			
		||||
    let (user2_id, db_user2) = db
 | 
			
		||||
        .collection()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .set(&user2)
 | 
			
		||||
        .expect("can set user");
 | 
			
		||||
    let (user3_id, db_user3) = db
 | 
			
		||||
        .collection()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .set(&user3)
 | 
			
		||||
        .expect("can set user");
 | 
			
		||||
    let (user4_id, db_user4) = db
 | 
			
		||||
        .collection()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .set(&user4)
 | 
			
		||||
        .expect("can set user");
 | 
			
		||||
 | 
			
		||||
    println!("User 1 assigned ID: {}", user1_id);
 | 
			
		||||
    println!("User 2 assigned ID: {}", user2_id);
 | 
			
		||||
    println!("User 3 assigned ID: {}", user3_id);
 | 
			
		||||
    println!("User 4 assigned ID: {}", user4_id);
 | 
			
		||||
 | 
			
		||||
    // We already have the updated models from the set method, so we don't need to retrieve them again
 | 
			
		||||
 | 
			
		||||
    // Print all users retrieved from database
 | 
			
		||||
    println!("\n--- Users Retrieved from Database ---");
 | 
			
		||||
    println!("\n1. First user:");
 | 
			
		||||
    print_user_details(&db_user1);
 | 
			
		||||
 | 
			
		||||
    println!("\n2. Second user:");
 | 
			
		||||
    print_user_details(&db_user2);
 | 
			
		||||
 | 
			
		||||
    println!("\n3. Third user:");
 | 
			
		||||
    print_user_details(&db_user3);
 | 
			
		||||
 | 
			
		||||
    println!("\n4. Fourth user:");
 | 
			
		||||
    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::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<username, _>("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::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<is_active, _>(&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:");
 | 
			
		||||
    let user_to_delete_id = active_users[0].get_id();
 | 
			
		||||
    println!("Deleting user with ID: {}", user_to_delete_id);
 | 
			
		||||
    db.collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .delete_by_id(user_to_delete_id)
 | 
			
		||||
        .expect("can delete existing user");
 | 
			
		||||
 | 
			
		||||
    // Show remaining active users
 | 
			
		||||
    let active_users = db
 | 
			
		||||
        .collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<is_active, _>(&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::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .get::<is_active, _>(&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()
 | 
			
		||||
        .user_id(db_user1.get_id()) // commenter's user ID
 | 
			
		||||
        .content("This is a comment on the user")
 | 
			
		||||
        .build();
 | 
			
		||||
 | 
			
		||||
    // Save the comment and get its assigned ID and updated model
 | 
			
		||||
    let (comment_id, db_comment) = db
 | 
			
		||||
        .collection()
 | 
			
		||||
        .expect("can open comment collection")
 | 
			
		||||
        .set(&comment)
 | 
			
		||||
        .expect("can set comment");
 | 
			
		||||
 | 
			
		||||
    println!("Comment assigned ID: {}", comment_id);
 | 
			
		||||
 | 
			
		||||
    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());
 | 
			
		||||
 | 
			
		||||
    // Save the updated user and get the new version
 | 
			
		||||
    let (_, user_with_comment) = db
 | 
			
		||||
        .collection::<User>()
 | 
			
		||||
        .expect("can open user collection")
 | 
			
		||||
        .set(&updated_user)
 | 
			
		||||
        .expect("can set updated user");
 | 
			
		||||
 | 
			
		||||
    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());
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user