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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::future::Future;

use crate::model::auth_account::{AuthAccountData, AuthAccountModel, AuthAccountStatus};
use crate::model::token::{TokenData, TokenModel};
use c3p0::*;
use lightspeed_core::error::LsError;

pub mod pg;

pub trait AuthRepositoryManager<Id: IdType>: Clone + Send + Sync {
    type Tx: Send + Sync;
    type C3P0: C3p0Pool<Tx = Self::Tx>;
    type AuthAccountRepo: AuthAccountRepository<Id, Tx = Self::Tx>;
    type TokenRepo: TokenRepository<Id, Tx = Self::Tx>;

    fn c3p0(&self) -> &Self::C3P0;
    fn start(&self) -> impl Future<Output = Result<(), LsError>> + Send;
    fn auth_account_repo(&self) -> Self::AuthAccountRepo;
    fn token_repo(&self) -> Self::TokenRepo;
}

pub trait AuthAccountRepository<Id: IdType>: Clone + Send + Sync {
    type Tx: Send + Sync;

    fn fetch_all_by_status(
        &self,
        tx: &mut Self::Tx,
        status: AuthAccountStatus,
        start_user_id: &Id,
        limit: u32,
    ) -> impl Future<Output = Result<Vec<AuthAccountModel<Id>>, LsError>> + Send;

    fn fetch_by_id(
        &self,
        tx: &mut Self::Tx,
        user_id: &Id,
    ) -> impl Future<Output = Result<AuthAccountModel<Id>, LsError>> + Send;

    fn fetch_by_username(
        &self,
        tx: &mut Self::Tx,
        username: &str,
    ) -> impl Future<Output = Result<AuthAccountModel<Id>, LsError>> + Send;

    fn fetch_by_username_optional(
        &self,
        tx: &mut Self::Tx,
        username: &str,
    ) -> impl Future<Output = Result<Option<AuthAccountModel<Id>>, LsError>> + Send;

    fn fetch_by_email_optional(
        &self,
        tx: &mut Self::Tx,
        email: &str,
    ) -> impl Future<Output = Result<Option<AuthAccountModel<Id>>, LsError>> + Send;

    fn save(
        &self,
        tx: &mut Self::Tx,
        model: NewModel<AuthAccountData>,
    ) -> impl Future<Output = Result<AuthAccountModel<Id>, LsError>> + Send;

    fn update(
        &self,
        tx: &mut Self::Tx,
        model: Model<Id, AuthAccountData>,
    ) -> impl Future<Output = Result<AuthAccountModel<Id>, LsError>> + Send;

    fn delete(
        &self,
        tx: &mut Self::Tx,
        model: AuthAccountModel<Id>,
    ) -> impl Future<Output = Result<AuthAccountModel<Id>, LsError>> + Send;

    fn delete_by_id(&self, tx: &mut Self::Tx, user_id: &Id) -> impl Future<Output = Result<u64, LsError>> + Send;
}

pub trait TokenRepository<Id: IdType>: Clone + Send + Sync {
    type Tx: Send + Sync;

    fn fetch_by_token(
        &self,
        tx: &mut Self::Tx,
        token_string: &str,
    ) -> impl Future<Output = Result<TokenModel<Id>, LsError>> + Send;

    fn fetch_by_username(
        &self,
        tx: &mut Self::Tx,
        username: &str,
    ) -> impl Future<Output = Result<Vec<TokenModel<Id>>, LsError>> + Send;

    fn save(
        &self,
        tx: &mut Self::Tx,
        model: NewModel<TokenData>,
    ) -> impl Future<Output = Result<TokenModel<Id>, LsError>> + Send;

    fn delete(
        &self,
        tx: &mut Self::Tx,
        model: TokenModel<Id>,
    ) -> impl Future<Output = Result<TokenModel<Id>, LsError>> + Send;
}