You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB
Rust

use sqlx::PgPool;
use crate::core::*;
use crate::model::{Account, AccountId, NewAccount};
pub struct PgAccountDataSource {
pool: PgPool,
}
impl PgAccountDataSource {
pub fn new(pool: PgPool) -> PgAccountDataSource {
PgAccountDataSource { pool }
}
pub async fn by_id(&self, id: AccountId) -> Result<Account> {
let account: Account = sqlx::query_as("SELECT * FROM accounts WHERE id = $1")
.bind(id)
.fetch_one(&self.pool)
.await?;
Ok(account)
}
pub async fn create(&self, new: NewAccount) -> Result<Account> {
let account: Account = sqlx::query_as(
"INSERT INTO accounts (iri, name, domain, display_name, public_key)
VALUES ($1, $2, $3, $4, $5)
RETURNING *",
)
.bind(new.iri)
.bind(new.name)
.bind(new.domain)
.bind(new.display_name)
.bind(new.public_key)
.fetch_one(&self.pool)
.await?;
Ok(account)
}
pub async fn delete(&self, id: AccountId) -> Result<()> {
sqlx::query("DELETE FROM accounts WHERE id = $1")
.bind(id)
.execute(&self.pool)
.await?;
Ok(())
}
}