2025-08-23 22:07:14 -04:00
|
|
|
mod chats;
|
|
|
|
mod messages;
|
2025-08-20 16:19:08 +00:00
|
|
|
mod sql;
|
|
|
|
|
|
|
|
use crate::config::Config;
|
2025-08-23 22:07:14 -04:00
|
|
|
use buckets_core::{Config as BucketsConfig, DataManager as BucketsManager};
|
2025-08-20 16:19:08 +00:00
|
|
|
use oiseau::{execute, postgres::DataManager as OiseauManager, postgres::Result as PgResult};
|
2025-08-23 22:07:14 -04:00
|
|
|
use std::collections::HashMap;
|
2025-08-24 12:29:36 -04:00
|
|
|
use tetratto_core::{
|
|
|
|
DataManager as TetrattoManager,
|
|
|
|
config::Config as TetrattoConfig,
|
|
|
|
model::{Error, Result},
|
|
|
|
};
|
2025-08-20 16:19:08 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2025-08-24 12:29:36 -04:00
|
|
|
pub struct DataManager(
|
|
|
|
pub OiseauManager<Config>,
|
|
|
|
pub BucketsManager,
|
|
|
|
pub TetrattoManager,
|
|
|
|
);
|
2025-08-20 16:19:08 +00:00
|
|
|
|
|
|
|
impl DataManager {
|
|
|
|
/// Create a new [`DataManager`].
|
|
|
|
pub async fn new(config: Config) -> PgResult<Self> {
|
2025-08-23 22:07:14 -04:00
|
|
|
let buckets_manager = BucketsManager::new(BucketsConfig {
|
|
|
|
directory: config.uploads_dir.clone(),
|
|
|
|
bucket_defaults: HashMap::new(),
|
|
|
|
database: config.database.clone(),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.expect("failed to create buckets manager");
|
|
|
|
|
2025-08-24 12:29:36 -04:00
|
|
|
let tetratto_manager = TetrattoManager::new(TetrattoConfig::get_config())
|
|
|
|
.await
|
|
|
|
.expect("failed to create tetratto manager");
|
|
|
|
|
|
|
|
Ok(Self(
|
|
|
|
OiseauManager::new(config).await?,
|
|
|
|
buckets_manager,
|
|
|
|
tetratto_manager,
|
|
|
|
))
|
2025-08-20 16:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize tables.
|
|
|
|
pub async fn init(&self) -> Result<()> {
|
|
|
|
let conn = match self.0.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
2025-08-23 22:07:14 -04:00
|
|
|
execute!(&conn, sql::CREATE_TABLE_CHATS).unwrap();
|
|
|
|
execute!(&conn, sql::CREATE_TABLE_MESSAGES).unwrap();
|
2025-08-20 16:19:08 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|