1
//! Pure MongoDB model.
2

            
3
use std::{error::Error as StdError, sync::Arc};
4

            
5
use async_trait::async_trait;
6
use mongodb::Database;
7

            
8
use super::{
9
    access_token, authorization_code, client, login_session,
10
    mongodb::{
11
        access_token::Model as AccessTokenModel,
12
        authorization_code::Model as AuthorizationCodeModel,
13
        client::Model as ClientModel,
14
        conn::{self, Options},
15
        login_session::Model as LoginSessionModel,
16
        refresh_token::Model as RefreshTokenModel,
17
        user::Model as UserModel,
18
    },
19
    refresh_token, user,
20
};
21

            
22
/// Pure MongoDB model.
23
#[derive(Clone)]
24
pub struct Model {
25
    conn: Arc<Database>,
26
    user: Arc<UserModel>,
27
    client: Arc<ClientModel>,
28
    login_session: Arc<LoginSessionModel>,
29
    authorization_code: Arc<AuthorizationCodeModel>,
30
    access_token: Arc<AccessTokenModel>,
31
    refresh_token: Arc<RefreshTokenModel>,
32
}
33

            
34
impl Model {
35
    /// Create an instance.
36
7
    pub async fn new(opts: &Options) -> Result<Self, Box<dyn StdError>> {
37
35
        let conn = Arc::new(conn::connect(opts).await?);
38
        Ok(Model {
39
7
            conn: conn.clone(),
40
14
            user: Arc::new(UserModel::new(conn.clone()).await?),
41
14
            client: Arc::new(ClientModel::new(conn.clone()).await?),
42
14
            login_session: Arc::new(LoginSessionModel::new(conn.clone()).await?),
43
14
            authorization_code: Arc::new(AuthorizationCodeModel::new(conn.clone()).await?),
44
14
            access_token: Arc::new(AccessTokenModel::new(conn.clone()).await?),
45
14
            refresh_token: Arc::new(RefreshTokenModel::new(conn.clone()).await?),
46
        })
47
7
    }
48

            
49
    /// Get the raw database connection ([`Database`]).
50
146
    pub fn get_connection(&self) -> &Database {
51
146
        &self.conn
52
146
    }
53
}
54

            
55
#[async_trait]
56
impl super::Model for Model {
57
2
    async fn close(&self) -> Result<(), Box<dyn StdError>> {
58
2
        Ok(())
59
4
    }
60

            
61
1050
    fn user(&self) -> &dyn user::UserModel {
62
1050
        self.user.as_ref()
63
1050
    }
64

            
65
1415
    fn client(&self) -> &dyn client::ClientModel {
66
1415
        self.client.as_ref()
67
1415
    }
68

            
69
428
    fn login_session(&self) -> &dyn login_session::LoginSessionModel {
70
428
        self.login_session.as_ref()
71
428
    }
72

            
73
414
    fn authorization_code(&self) -> &dyn authorization_code::AuthorizationCodeModel {
74
414
        self.authorization_code.as_ref()
75
414
    }
76

            
77
413
    fn access_token(&self) -> &dyn access_token::AccessTokenModel {
78
413
        self.access_token.as_ref()
79
413
    }
80

            
81
177
    fn refresh_token(&self) -> &dyn refresh_token::RefreshTokenModel {
82
177
        self.refresh_token.as_ref()
83
177
    }
84
}