1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use mongodb::{
    bson::{Bson, Document},
    options::ClientOptions,
    Client, Collection,
};
use std::error::Error;

use crate::{checker_service::TokenResult, util::env};

pub struct Collector {
    collection: Collection<Document>,
}

pub async fn new() -> Result<Collector, Box<dyn Error>> {
    let client_options = ClientOptions::parse(&env("MONGO_URL")).await?;
    let client = Client::with_options(client_options)?;

    let db = client.database("db");
    let collection = db.collection::<Document>("token_results");

    Ok(Collector { collection })
}

impl Collector {
    pub async fn insert(
        &self,
        token_result: TokenResult,
    ) -> Result<Bson, Box<dyn Error>> {
        let doc = mongodb::bson::to_document(&token_result)?;
        let res = self.collection.insert_one(doc, None).await?;
        Ok(res.inserted_id)
    }

    pub async fn insert_generic(
        &self,
        doc: serde_json::Value,
    ) -> Result<Bson, Box<dyn Error>> {
        let doc = mongodb::bson::to_document(&doc)?;
        let res = self.collection.insert_one(doc, None).await?;
        Ok(res.inserted_id)
    }
}

#[cfg(test)]
mod tests {
    use crate::checker_service::TokenResult;

    #[tokio::test]
    async fn test_collector() {
        let collector = super::new().await.unwrap();
        let token_result = TokenResult::default();
        collector.insert(token_result).await.unwrap();
    }
}