fix: Use incremental ID

This commit is contained in:
Mahmoud Emad
2025-05-17 13:00:05 +03:00
parent bde5db0e52
commit a676854251
13 changed files with 149 additions and 116 deletions

View File

@@ -33,68 +33,73 @@ fn main() {
println!("Hero Models - Basic Usage Example");
println!("================================");
// Create users with different ID configurations
// Create users with auto-generated IDs
// User 1: With explicit ID
let user1 = User::new(Some(1))
// User 1
let user1 = User::new()
.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)
// User 2
let user2 = User::new()
.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))
// User 3
let user3 = User::new()
.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))
// 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
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");
// Save all users to database and get their assigned IDs
let user1_id = db.collection().expect("can open user collection").set(&user1).expect("can set user");
let user2_id = db.collection().expect("can open user collection").set(&user2).expect("can set user");
let user3_id = db.collection().expect("can open user collection").set(&user3).expect("can set user");
let user4_id = db.collection().expect("can open user collection").set(&user4).expect("can set user");
// Retrieve all users from database
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);
// Retrieve all users from database using the assigned IDs
let db_user1 = db.collection::<User>().expect("can open user collection")
.get_by_id(user1.get_id()).expect("can load user").expect("user should exist");
.get_by_id(user1_id).expect("can load user").expect("user should exist");
let db_user2 = db.collection::<User>().expect("can open user collection")
.get_by_id(user2.get_id()).expect("can load user").expect("user should exist");
.get_by_id(user2_id).expect("can load user").expect("user should exist");
let db_user3 = db.collection::<User>().expect("can open user collection")
.get_by_id(user3.get_id()).expect("can load user").expect("user should exist");
.get_by_id(user3_id).expect("can load user").expect("user should exist");
let db_user4 = db.collection::<User>().expect("can open user collection")
.get_by_id(user4.get_id()).expect("can load user").expect("user should exist");
.get_by_id(user4_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):");
println!("\n1. First user:");
print_user_details(&db_user1);
println!("\n2. User with auto-generated ID:");
println!("\n2. Second user:");
print_user_details(&db_user2);
println!("\n3. User with explicit ID (3):");
println!("\n3. Third user:");
print_user_details(&db_user3);
println!("\n4. User with explicit ID (4):");
println!("\n4. Fourth user:");
print_user_details(&db_user4);
// Demonstrate different ways to retrieve users from the database
@@ -126,9 +131,11 @@ fn main() {
// 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(active_users[0].get_id())
.delete_by_id(user_to_delete_id)
.expect("can delete existing user");
// Show remaining active users
@@ -165,21 +172,24 @@ fn main() {
// 1. Create and save a comment
println!("\n1. Creating a Comment:");
let comment = Comment::new(None)
let comment = Comment::new()
.user_id(db_user1.get_id()) // commenter's user ID
.content("This is a comment on the user")
.build();
db.collection()
// Save the comment and get its assigned ID
let comment_id = db.collection()
.expect("can open comment collection")
.set(&comment)
.expect("can set comment");
// 2. Retrieve the comment from database
println!("Comment assigned ID: {}", comment_id);
// 2. Retrieve the comment from database using the assigned ID
let db_comment = db
.collection::<Comment>()
.expect("can open comment collection")
.get_by_id(comment.get_id())
.get_by_id(comment_id)
.expect("can load comment")
.expect("comment should exist");

View File

@@ -19,7 +19,7 @@ fn main() {
println!("SimpleUser DB Prefix: {}", SimpleUser::db_prefix());
let user = SimpleUser {
base_data: BaseModelData::new(1),
base_data: BaseModelData::new(),
login: "johndoe".to_string(),
full_name: "John Doe".to_string(),
};