From dbfc5cbf628adece0b95292daef098ba2c4245d6 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 17 Feb 2026 01:41:42 +0100 Subject: [PATCH] feat(database/bot): adding the database macros --- src/database/bot.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/database/bot.rs b/src/database/bot.rs index 5e3205a..5819933 100644 --- a/src/database/bot.rs +++ b/src/database/bot.rs @@ -4,45 +4,39 @@ use crate::models::bot::{DbBot, BotPresence, BotActivity}; const BOT_ID: i32 = 1; pub async fn init(db: &PgPool) -> Result<(), sqlx::Error> { - query("INSERT INTO bots (id) VALUES ($1) ON CONFLICT DO NOTHING") - .bind(BOT_ID) + query!("INSERT INTO bots (id) VALUES ($1) ON CONFLICT DO NOTHING", BOT_ID) .execute(db) .await?; Ok(()) } pub async fn get(db: &PgPool) -> Result, sqlx::Error> { - let bot: Option = query_as::<_, DbBot>( - "SELECT status, activity_type, presence FROM bots WHERE id = $1", + let bot: Option = query_as!( + DbBot, + r#"SELECT status, activity_type as "activity_type: BotActivity", presence as "presence: BotPresence" FROM bots WHERE id = $1"#, + BOT_ID ) - .bind(BOT_ID) - .fetch_optional(db) - .await?; + .fetch_optional(db) + .await?; Ok(bot) } pub async fn set_status(db: &PgPool, status: &str) -> Result<(), sqlx::Error> { - query("UPDATE bots SET status = $1 WHERE id = $2") - .bind(status) - .bind(BOT_ID) + query!("UPDATE bots SET status = $1 WHERE id = $2", status, BOT_ID) .execute(db) .await?; Ok(()) } pub async fn set_activity(db: &PgPool, activity: BotActivity) -> Result<(), sqlx::Error> { - query("UPDATE bots SET activity_type = $1 WHERE id = $2") - .bind(activity) - .bind(BOT_ID) + query!("UPDATE bots SET activity_type = $1::bot_activity WHERE id = $2", activity as BotActivity, BOT_ID) .execute(db) .await?; Ok(()) } pub async fn set_presence(db: &PgPool, presence: BotPresence) -> Result<(), sqlx::Error> { - query("UPDATE bots SET presence = $1 WHERE id = $2") - .bind(presence) - .bind(BOT_ID) + query!("UPDATE bots SET presence = $1::bot_presence WHERE id = $2", presence as BotPresence, BOT_ID) .execute(db) .await?; Ok(())