2022-12-19 15:47:40 +01:00
|
|
|
use sqlx::PgPool;
|
2022-12-05 20:36:30 +01:00
|
|
|
|
2022-12-06 00:29:24 +01:00
|
|
|
use crate::core::*;
|
|
|
|
use crate::model::account::{Account, NewAccount};
|
2022-12-05 20:36:30 +01:00
|
|
|
|
|
|
|
pub struct PgAccountDataSource {
|
|
|
|
pool: PgPool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PgAccountDataSource {
|
|
|
|
pub fn new(pool: PgPool) -> PgAccountDataSource {
|
|
|
|
PgAccountDataSource { pool }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn by_id<I>(&self, id: I) -> Result<Account>
|
|
|
|
where
|
|
|
|
I: Into<Id> + Send,
|
|
|
|
{
|
|
|
|
let id: Id = id.into();
|
|
|
|
let account: Account = sqlx::query_as("SELECT * FROM accounts WHERE id = $1")
|
|
|
|
.bind(id)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await?;
|
|
|
|
Ok(account)
|
|
|
|
}
|
2022-12-06 00:29:24 +01:00
|
|
|
|
|
|
|
pub async fn create<T>(&self, new: T) -> Result<Account>
|
|
|
|
where
|
|
|
|
T: Into<NewAccount> + Send,
|
|
|
|
{
|
|
|
|
let new = new.into();
|
2022-12-19 15:47:40 +01:00
|
|
|
let account: Account = sqlx::query_as(
|
2022-12-25 15:57:33 +01:00
|
|
|
"INSERT INTO accounts (iri, name, domain, display_name)
|
|
|
|
VALUES ($1, $2, $3, $4)
|
2022-12-19 15:47:40 +01:00
|
|
|
RETURNING *",
|
|
|
|
)
|
2022-12-25 15:57:33 +01:00
|
|
|
.bind(new.iri)
|
2022-12-19 15:47:40 +01:00
|
|
|
.bind(new.name)
|
|
|
|
.bind(new.domain)
|
|
|
|
.bind(new.display_name)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await?;
|
2022-12-06 00:29:24 +01:00
|
|
|
Ok(account)
|
|
|
|
}
|
2022-12-05 20:36:30 +01:00
|
|
|
}
|