Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: removed error when inserting a new user in db #406

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
",
grunch marked this conversation as resolved.
Show resolved Hide resolved
)
.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"))
}
arkanoider marked this conversation as resolved.
Show resolved Hide resolved
}

pub async fn update_user_trade_index(
Expand Down
Loading