framework_cqrs_lib/cqrs/infra/api_key/
component.rs

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
use crate::cqrs::core::api_key::services::api_key_service::ApiKeyService;
use crate::cqrs::core::api_key::services::impl_api_key_service::ImplApiKeyService;
use crate::cqrs::infra::api_key::dao::MongoApiKeyDAO;
use crate::cqrs::infra::api_key::dbo::ApiKeyDbo;
use crate::cqrs::infra::api_key::mongo_repository::MongoApiKeyRepository;
use crate::cqrs::infra::repositories::mongo_entity_repository::MongoEntityRepository;
use std::sync::Arc;

pub struct ApiKeyComponent {
    pub service: Arc<dyn ApiKeyService>,
}

impl ApiKeyComponent {
    pub async fn new(dbname: &str, aggregat_name: &str) -> Self {
        let api_key_dao = Arc::new(
            MongoApiKeyDAO::new(dbname, &format!("{aggregat_name}_api_key")).await
        );
        let api_key_repository: Arc<MongoEntityRepository<ApiKeyDbo>> = Arc::new(
            MongoApiKeyRepository {
                dao: api_key_dao.clone()
            }
        );
        let api_key_service: Arc<dyn ApiKeyService> = Arc::new(ImplApiKeyService { repo: api_key_repository.clone() });

        Self {
            service: api_key_service
        }
    }
}