feat(database/bot): adding the database macros
This commit is contained in:
parent
b0fd8dcc40
commit
dbfc5cbf62
1 changed files with 10 additions and 16 deletions
|
|
@ -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<Option<DbBot>, sqlx::Error> {
|
||||
let bot: Option<DbBot> = query_as::<_, DbBot>(
|
||||
"SELECT status, activity_type, presence FROM bots WHERE id = $1",
|
||||
let bot: Option<DbBot> = 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?;
|
||||
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(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue