use std::sync::Arc;
use sea_orm::DatabaseConnection;
use crate::search::SearchEngine;
use crate::matching::{ProbabilisticMatcher, PersonMatcher};
use crate::config::Config;
use crate::db::{PersonRepository, SeaOrmPersonRepository, AuditLogRepository};
use crate::streaming::{EventProducer, InMemoryEventPublisher};
#[derive(Clone)]
pub struct AppState {
pub db: DatabaseConnection,
pub person_repository: Arc<dyn PersonRepository>,
pub event_publisher: Arc<dyn EventProducer>,
pub audit_log: Arc<AuditLogRepository>,
pub search_engine: Arc<SearchEngine>,
pub matcher: Arc<dyn PersonMatcher>,
pub config: Arc<Config>,
}
impl AppState {
pub fn new(
db: DatabaseConnection,
search_engine: SearchEngine,
matcher: ProbabilisticMatcher,
config: Config,
) -> Self {
let event_publisher = Arc::new(InMemoryEventPublisher::new()) as Arc<dyn EventProducer>;
let audit_log = Arc::new(AuditLogRepository::new(db.clone()));
let person_repository = Arc::new(
SeaOrmPersonRepository::new(db.clone())
.with_event_publisher(event_publisher.clone())
.with_audit_log(audit_log.clone())
) as Arc<dyn PersonRepository>;
let person_matcher = Arc::new(matcher) as Arc<dyn PersonMatcher>;
Self {
db,
person_repository,
event_publisher,
audit_log,
search_engine: Arc::new(search_engine),
matcher: person_matcher,
config: Arc::new(config),
}
}
}