Skip to content

Commit

Permalink
Fix: removed error when inserting a new user in db
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Dec 26, 2024
1 parent 51caba5 commit 7002d57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 1 addition & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ async fn check_trade_index(pool: &Pool<Sqlite>, 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,
Expand Down
2 changes: 1 addition & 1 deletion src/app/admin_add_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
44 changes: 25 additions & 19 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> {
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(
Expand Down

0 comments on commit 7002d57

Please sign in to comment.