diff --git a/migrations/20231005195154_users.sql b/migrations/20231005195154_users.sql index 7ead826c..6c825469 100644 --- a/migrations/20231005195154_users.sql +++ b/migrations/20231005195154_users.sql @@ -6,7 +6,7 @@ CREATE TABLE IF NOT EXISTS users ( category integer not null default 0, last_trade_index integer not null default 0, total_reviews integer not null default 0, - total_rating float not null default 0, + total_rating real not null default 0.0, last_rating integer not null default 0, max_rating integer not null default 0, min_rating integer not null default 0, diff --git a/sqlx-data.json b/sqlx-data.json index ab16f536..02ed93f7 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -69,5 +69,15 @@ } }, "query": "\n UPDATE orders\n SET\n seller_pubkey = ?1\n WHERE id = ?2\n " + }, + "fb8c9a8f42f6fc3c70e734db9db4af88c9602143afdbfcf57562f3fecb80b3a7": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 2 + } + }, + "query": "\n UPDATE users SET last_trade_index = ?1 WHERE pubkey = ?2\n " } } \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index 9cf7549c..94ff6e49 100644 --- a/src/app.rs +++ b/src/app.rs @@ -80,8 +80,12 @@ async fn check_trade_index(pool: &Pool, event: &UnwrappedGift, msg: &Mes .verify_signature(event.rumor.pubkey, sig) { user.last_trade_index = index; - if let Err(e) = - update_user_trade_index(pool, user.pubkey, user.last_trade_index).await + if let Err(e) = update_user_trade_index( + pool, + event.sender.to_string(), + user.last_trade_index, + ) + .await { tracing::error!("Error updating user trade index: {}", e); } diff --git a/src/db.rs b/src/db.rs index cd4ef0f5..0945d8d1 100644 --- a/src/db.rs +++ b/src/db.rs @@ -341,7 +341,7 @@ pub async fn add_new_user( r#" INSERT INTO users (pubkey, last_trade_index, created_at) VALUES (?1, ?2, ?3) - RETURNING * + RETURNING pubkey "#, ) .bind(public_key) @@ -357,7 +357,7 @@ pub async fn update_user_trade_index( pool: &SqlitePool, public_key: String, trade_index: i64, -) -> anyhow::Result { +) -> 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")); @@ -366,24 +366,21 @@ pub async fn update_user_trade_index( if trade_index < 0 { return Err(anyhow::anyhow!("Invalid trade_index: must be non-negative")); } - if let Ok(user) = sqlx::query_as::<_, User>( + + let mut conn = pool.acquire().await?; + + let rows_affected = sqlx::query!( r#" - UPDATE users SET trade_index = ?1 WHERE pubkey = ?2 - RETURNING * + UPDATE users SET last_trade_index = ?1 WHERE pubkey = ?2 "#, + trade_index, + public_key, ) - .bind(trade_index) - .bind(public_key.clone()) - .fetch_one(pool) - .await - { - Ok(user) - } else { - Err(anyhow::anyhow!( - "No user found with public key: {}", - public_key - )) - } + .execute(&mut conn) + .await? + .rows_affected(); + + Ok(rows_affected > 0) } pub async fn update_user_rating(