use sqlx's query macro in some places
Some checks failed
Docker Image CI / build_amd64 (push) Has been cancelled
Docker Image CI / build_arm64 (push) Has been cancelled
Docker Image CI / create_manifest (push) Has been cancelled

This commit is contained in:
2025-06-27 13:30:38 -04:00
parent c054ef1c1c
commit a1cd0e6a25
8 changed files with 82 additions and 95 deletions

View File

@@ -9,7 +9,7 @@ pub mod blackjack;
use crate::{inventory::{self, Inventory}, common::{Context, Error}};
use poise::serenity_prelude::{self as serenity, futures::StreamExt, UserId};
use sqlx::{Row, PgExecutor};
use sqlx::PgConnection;
use std::collections::HashMap;
#[derive(Clone)]
@@ -79,16 +79,15 @@ mod items {
}
}
pub async fn get_balance<'a, E>(id: UserId, db: E) -> Result<i32, Error>
where
E: PgExecutor<'a>,
pub async fn get_balance(id: UserId, db: &mut PgConnection) -> Result<i32, Error>
{
let row = sqlx::query("SELECT balance FROM bank WHERE id = $1")
.bind(id.get() as i64)
.fetch_one(db).await.ok();
let row = sqlx::query!(
"SELECT balance FROM bank WHERE id = $1",
id.get() as i64
).fetch_one(db).await.ok();
let balance = if let Some(row) = row {
row.try_get("balance")?
row.balance.unwrap_or(100)
} else {
100
};
@@ -96,14 +95,13 @@ where
Ok(balance)
}
pub async fn change_balance<'a, E>(id: UserId, balance: i32, db: E) -> Result<(), Error>
where
E: PgExecutor<'a>,
pub async fn change_balance(id: UserId, balance: i32, db: &mut PgConnection) -> Result<(), Error>
{
sqlx::query("INSERT INTO bank (id, balance) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance")
.bind(id.get() as i64)
.bind(balance)
.execute(db).await?;
sqlx::query!(
r#"INSERT INTO bank (id, balance) VALUES ($1, $2)
ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance"#,
id.get() as i64, balance
).execute(db).await?;
Ok(())
}