/ src / database / accounts.rs
accounts.rs
 1  use anyhow::Result;
 2  use sqlx::SqlitePool;
 3  
 4  #[derive(sqlx::FromRow, Debug, PartialEq, Eq)]
 5  pub struct Account {
 6    pub user_id: String,
 7    pub token: String,
 8  }
 9  
10  pub async fn insert(
11    pool: &SqlitePool,
12    user_id: String,
13    token: String,
14  ) -> Result<()> {
15    sqlx::query(
16      r#"
17  INSERT INTO accounts ( user_id, token )
18  VALUES ( $1, $2 )
19      "#,
20    )
21    .bind(user_id)
22    .bind(token)
23    .execute(pool)
24    .await?;
25  
26    Ok(())
27  }
28  
29  pub async fn get_all(pool: &SqlitePool) -> Result<Vec<Account>> {
30    let accounts = sqlx::query_as(
31      r#"
32  SELECT user_id, token
33  FROM accounts
34      "#,
35    )
36    .fetch_all(pool)
37    .await?;
38  
39    Ok(accounts)
40  }