fix: Use incremental ID

This commit is contained in:
Mahmoud Emad
2025-05-17 13:46:16 +03:00
parent a676854251
commit 57f59da43e
10 changed files with 519 additions and 156 deletions

View File

@@ -67,26 +67,18 @@ fn main() {
.is_active(false)
.build();
// 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");
// 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);
// Retrieve all users from database using the assigned IDs
let db_user1 = db.collection::<User>().expect("can open user collection")
.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_id).expect("can load user").expect("user should exist");
let db_user3 = db.collection::<User>().expect("can open user collection")
.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_id).expect("can load user").expect("user should exist");
// 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 ---");
@@ -125,7 +117,7 @@ fn main() {
.expect("can load stored users");
assert_eq!(active_users.len(), 2);
for (i, active_user) in active_users.iter().enumerate() {
for (_i, active_user) in active_users.iter().enumerate() {
print_user_details(active_user);
}
@@ -147,7 +139,7 @@ fn main() {
println!(" a. Remaining Active Users:");
assert_eq!(active_users.len(), 1);
for (i, active_user) in active_users.iter().enumerate() {
for (_i, active_user) in active_users.iter().enumerate() {
print_user_details(active_user);
}
@@ -160,7 +152,7 @@ fn main() {
println!(" b. Inactive Users:");
assert_eq!(inactive_users.len(), 2);
for (i, inactive_user) in inactive_users.iter().enumerate() {
for (_i, inactive_user) in inactive_users.iter().enumerate() {
print_user_details(inactive_user);
}
@@ -177,22 +169,14 @@ fn main() {
.content("This is a comment on the user")
.build();
// Save the comment and get its assigned ID
let comment_id = db.collection()
// 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);
// 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_id)
.expect("can load comment")
.expect("comment should exist");
println!(" a. Comment Retrieved from Database:");
print_comment_details(&db_comment);
@@ -201,19 +185,12 @@ fn main() {
let mut updated_user = db_user1.clone();
updated_user.base_data.add_comment(db_comment.get_id());
db.collection::<User>()
// 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");
// 4. Retrieve the updated user
let user_with_comment = db
.collection::<User>()
.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);