bazzar/crates/db-seed/src/accounts.rs

38 lines
1000 B
Rust

use actix::Addr;
use config::SharedAppConfig;
use database_manager::{query_db, Database};
use fake::{Fake, Faker};
use crate::{Result, SharedState};
pub(crate) async fn create_accounts(
db: Addr<Database>,
seed: SharedState,
_config: SharedAppConfig,
) -> Result<()> {
let accounts: Vec<model::FullAccount> =
query_db!(db, database_manager::AllAccounts, default vec![]);
if accounts.len() >= 10 {
seed.lock().unwrap().accounts = accounts;
return Ok(());
}
let mut accounts = Vec::with_capacity(10);
for _ in 0..10 {
let msg: database_manager::CreateAccount = Faker.fake::<database_manager::CreateAccount>();
match db.send(msg).await {
Ok(Ok(account)) => accounts.push(account),
Ok(Err(e)) => {
tracing::error!("{e}")
}
Err(e) => {
tracing::error!("{e}")
}
}
}
seed.lock().unwrap().accounts = accounts;
Ok(())
}