From 7002d571b65344ddbc9b48cb85015ab0021973b8 Mon Sep 17 00:00:00 2001 From: arkanoider Date: Thu, 26 Dec 2024 22:15:26 +0100 Subject: [PATCH] Fix: removed error when inserting a new user in db --- src/app.rs | 4 +--- src/app/admin_add_solver.rs | 2 +- src/db.rs | 44 +++++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/app.rs b/src/app.rs index 94ff6e49..d96d3954 100644 --- a/src/app.rs +++ b/src/app.rs @@ -108,9 +108,7 @@ async fn check_trade_index(pool: &Pool, event: &UnwrappedGift, msg: &Mes last_trade_index, ..Default::default() }; - if let Err(e) = - add_new_user(pool, new_user.pubkey, new_user.last_trade_index).await - { + if let Err(e) = add_new_user(pool, new_user).await { tracing::error!("Error creating new user: {}", e); send_cant_do_msg( None, diff --git a/src/app/admin_add_solver.rs b/src/app/admin_add_solver.rs index 3bfb9d71..f00e6731 100644 --- a/src/app/admin_add_solver.rs +++ b/src/app/admin_add_solver.rs @@ -42,7 +42,7 @@ pub async fn admin_add_solver_action( let public_key = PublicKey::from_bech32(npubkey)?.to_hex(); let user = User::new(public_key, 0, 1, 0, 0, trade_index); // Use CRUD to create user - match add_new_user(pool, user.pubkey, user.last_trade_index).await { + match add_new_user(pool, user).await { Ok(r) => info!("Solver added: {:#?}", r), Err(ee) => error!("Error creating solver: {:#?}", ee), } diff --git a/src/db.rs b/src/db.rs index 31132892..f4ce2052 100644 --- a/src/db.rs +++ b/src/db.rs @@ -328,30 +328,36 @@ pub async fn is_user_present(pool: &SqlitePool, public_key: String) -> anyhow::R Ok(user) } -pub async fn add_new_user( - pool: &SqlitePool, - public_key: String, - last_trade_index: i64, -) -> anyhow::Result { +pub async fn add_new_user(pool: &SqlitePool, new_user: User) -> anyhow::Result<()> { // Validate public key format (32-bytes hex) - if !public_key.chars().all(|c| c.is_ascii_hexdigit()) || public_key.len() != 64 { - return Err(anyhow::anyhow!("Invalid public key format")); - } let created_at: Timestamp = Timestamp::now(); - let user = sqlx::query_as::<_, User>( - r#" - INSERT INTO users (pubkey, last_trade_index, created_at) - VALUES (?1, ?2, ?3) - RETURNING pubkey - "#, + let result = sqlx::query( + " + INSERT INTO users (pubkey, is_admin, is_solver, is_banned, category, last_trade_index, total_reviews, total_rating, last_rating, max_rating, min_rating, created_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) + ", ) - .bind(public_key) - .bind(last_trade_index) + .bind(new_user.pubkey) + .bind(new_user.is_admin) + .bind(new_user.is_solver) + .bind(new_user.is_banned) + .bind(new_user.category) + .bind(new_user.last_trade_index) + .bind(new_user.total_reviews) + .bind(new_user.total_rating) + .bind(new_user.last_rating) + .bind(new_user.max_rating) + .bind(new_user.min_rating) .bind(created_at.to_string()) - .fetch_one(pool) - .await?; + .execute(pool) + .await; - Ok(user) + if result.is_ok() { + tracing::info!("New user created successfully"); + Ok(()) + } else { + Err(anyhow::anyhow!("Error creating new user")) + } } pub async fn update_user_trade_index(