use sqlx's query macro in some places
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user