Skip to content

Commit

Permalink
Fix: last trade index check working
Browse files Browse the repository at this point in the history
  • Loading branch information
arkanoider committed Dec 23, 2024
1 parent d1176b8 commit 8f00b4c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion migrations/20231005195154_users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
}
}
8 changes: 6 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ async fn check_trade_index(pool: &Pool<Sqlite>, 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);
}
Expand Down
31 changes: 14 additions & 17 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -357,7 +357,7 @@ pub async fn update_user_trade_index(
pool: &SqlitePool,
public_key: String,
trade_index: i64,
) -> anyhow::Result<User> {
) -> anyhow::Result<bool> {
// 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"));
Expand All @@ -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(
Expand Down

0 comments on commit 8f00b4c

Please sign in to comment.