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.

52 lines
1.3 KiB
Rust

use sqlx::PgPool;
use crate::core::*;
use crate::data::{MemAccountDataSource, PgAccountDataSource};
use crate::model::{Account, AccountId, NewAccount};
pub struct AccountRepo {
db: PgAccountDataSource,
mem: MemAccountDataSource,
}
impl AccountRepo {
pub fn new(db_pool: PgPool) -> AccountRepo {
AccountRepo {
db: PgAccountDataSource::new(db_pool),
mem: MemAccountDataSource::new(),
}
}
pub async fn by_id<I>(&self, id: I) -> Result<Account>
where
I: Into<AccountId> + Send,
{
let id = id.into();
match self.mem.by_id(id).await {
Some(account) => Ok(account),
None => self.db.by_id(id).await,
}
}
pub async fn create<T, E>(&self, new: T) -> Result<Account>
where
T: TryInto<Sane<NewAccount>, Error = E> + Send,
E: Into<Error>,
{
let new = new.try_into().map_err(|e| e.into())?.inner();
let account = self.db.create(new).await?;
self.mem.store(account.clone()).await;
Ok(account)
}
pub async fn delete<I>(&self, id: I) -> Result<()>
where
I: Into<AccountId> + Send,
{
let id = id.into();
self.db.delete(id).await?;
self.mem.delete(id).await;
Ok(())
}
}