mod sql;
mod uploads;

use crate::config::Config;
use crate::model::result::*;
use oiseau::{execute, postgres::DataManager as OiseauManager, postgres::Result as PgResult};

#[derive(Clone)]
pub struct DataManager(pub OiseauManager<Config>);

impl DataManager {
    /// Create a new [`DataManager`].
    pub async fn new(config: Config) -> PgResult<Self> {
        Ok(Self(OiseauManager::new(config).await?))
    }

    /// 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())),
        };

        execute!(&conn, sql::CREATE_TABLE_UPLOADS).unwrap();

        Ok(())
    }
}
