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;
|
const BOT_ID: i32 = 1;
|
||||||
|
|
||||||
pub async fn init(db: &PgPool) -> Result<(), sqlx::Error> {
|
pub async fn init(db: &PgPool) -> Result<(), sqlx::Error> {
|
||||||
query("INSERT INTO bots (id) VALUES ($1) ON CONFLICT DO NOTHING")
|
query!("INSERT INTO bots (id) VALUES ($1) ON CONFLICT DO NOTHING", BOT_ID)
|
||||||
.bind(BOT_ID)
|
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(db: &PgPool) -> Result<Option<DbBot>, sqlx::Error> {
|
pub async fn get(db: &PgPool) -> Result<Option<DbBot>, sqlx::Error> {
|
||||||
let bot: Option<DbBot> = query_as::<_, DbBot>(
|
let bot: Option<DbBot> = query_as!(
|
||||||
"SELECT status, activity_type, presence FROM bots WHERE id = $1",
|
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)
|
.fetch_optional(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(bot)
|
Ok(bot)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_status(db: &PgPool, status: &str) -> Result<(), sqlx::Error> {
|
pub async fn set_status(db: &PgPool, status: &str) -> Result<(), sqlx::Error> {
|
||||||
query("UPDATE bots SET status = $1 WHERE id = $2")
|
query!("UPDATE bots SET status = $1 WHERE id = $2", status, BOT_ID)
|
||||||
.bind(status)
|
|
||||||
.bind(BOT_ID)
|
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_activity(db: &PgPool, activity: BotActivity) -> Result<(), sqlx::Error> {
|
pub async fn set_activity(db: &PgPool, activity: BotActivity) -> Result<(), sqlx::Error> {
|
||||||
query("UPDATE bots SET activity_type = $1 WHERE id = $2")
|
query!("UPDATE bots SET activity_type = $1::bot_activity WHERE id = $2", activity as BotActivity, BOT_ID)
|
||||||
.bind(activity)
|
|
||||||
.bind(BOT_ID)
|
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_presence(db: &PgPool, presence: BotPresence) -> Result<(), sqlx::Error> {
|
pub async fn set_presence(db: &PgPool, presence: BotPresence) -> Result<(), sqlx::Error> {
|
||||||
query("UPDATE bots SET presence = $1 WHERE id = $2")
|
query!("UPDATE bots SET presence = $1::bot_presence WHERE id = $2", presence as BotPresence, BOT_ID)
|
||||||
.bind(presence)
|
|
||||||
.bind(BOT_ID)
|
|
||||||
.execute(db)
|
.execute(db)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue