use mongodb::{options::ClientOptions, Client, Database};
use std::sync::Arc;

#[derive(Clone)]
pub struct MongoRepo {
    pub db: Arc<Database>,
}

impl MongoRepo {
    pub async fn init(db_url: &str) -> Self {
        let client_options = ClientOptions::parse(db_url).await.unwrap();
        let client = Client::with_options(client_options).unwrap();
        let db = client.database("socialnetwork");
        MongoRepo { db: Arc::new(db) }
    }

    pub fn collection<T: Send + Sync>(&self, name: &str) -> mongodb::Collection<T> {
        self.db.collection(name)
    }
}