nyanoblog/src/repo/account.rs

34 lines
768 B
Rust
Raw Normal View History

2022-12-06 00:29:24 +01:00
use sqlx::PgPool;
use crate::core::*;
use crate::data::account::PgAccountDataSource;
use crate::model::account::{Account, NewAccount};
pub struct AccountRepo {
db: PgAccountDataSource,
}
impl AccountRepo {
pub fn new(db_pool: PgPool) -> AccountRepo {
AccountRepo {
db: PgAccountDataSource::new(db_pool),
}
}
pub async fn by_id<I>(&self, id: I) -> Result<Account>
where
I: Into<Id> + Send,
{
self.db.by_id(id.into()).await
2022-12-06 00:29:24 +01:00
}
2022-12-19 15:47:40 +01:00
pub async fn create<T, E>(&self, new: T) -> Result<Account>
2022-12-06 00:29:24 +01:00
where
2022-12-19 15:47:40 +01:00
T: TryInto<Sane<NewAccount>, Error = E> + Send,
E: Into<Error>,
2022-12-06 00:29:24 +01:00
{
let new = new.try_into().map_err(|e| e.into())?.inner();
self.db.create(new).await
2022-12-06 00:29:24 +01:00
}
}