2022-12-05 20:36:30 +01:00
|
|
|
use serde;
|
|
|
|
use sqlx::{Executor, PgPool};
|
|
|
|
|
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();
|
|
|
|
let account: Account =
|
|
|
|
sqlx::query_as("INSERT INTO accounts (name, domain, display_name) VALUES ($1, $2, $3)")
|
|
|
|
.bind(new.name)
|
|
|
|
.bind(new.domain)
|
|
|
|
.bind(new.display_name)
|
|
|
|
.fetch_one(&self.pool)
|
|
|
|
.await?;
|
|
|
|
Ok(account)
|
|
|
|
}
|
2022-12-05 20:36:30 +01:00
|
|
|
}
|