use crate::db::Pool;
use std::sync::Arc;
use super::{FileService, MessageService, PlanService, ProjectService, SessionService};
#[derive(Clone)]
pub struct ServiceContext {
pub pool: Arc<Pool>,
}
impl ServiceContext {
pub fn new(pool: Pool) -> Self {
Self {
pool: Arc::new(pool),
}
}
pub fn pool(&self) -> Pool {
(*self.pool).clone()
}
}
pub struct ServiceManager {
context: ServiceContext,
session_service: SessionService,
message_service: MessageService,
file_service: FileService,
plan_service: PlanService,
project_service: ProjectService,
}
impl ServiceManager {
pub fn new(pool: Pool) -> Self {
let context = ServiceContext::new(pool);
Self {
session_service: SessionService::new(context.clone()),
message_service: MessageService::new(context.clone()),
file_service: FileService::new(context.clone()),
plan_service: PlanService::new(context.clone()),
project_service: ProjectService::new(context.clone()),
context,
}
}
pub fn sessions(&self) -> &SessionService {
&self.session_service
}
pub fn messages(&self) -> &MessageService {
&self.message_service
}
pub fn files(&self) -> &FileService {
&self.file_service
}
pub fn plans(&self) -> &PlanService {
&self.plan_service
}
pub fn projects(&self) -> &ProjectService {
&self.project_service
}
pub fn context(&self) -> &ServiceContext {
&self.context
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::db::{Pool, PoolExt};
async fn create_test_pool() -> Pool {
use crate::db::Database;
let db = Database::connect_in_memory().await.unwrap();
db.run_migrations().await.unwrap();
db.pool().clone()
}
#[tokio::test]
async fn test_service_context_creation() {
let pool = create_test_pool().await;
let context = ServiceContext::new(pool);
assert!(context.pool().is_connected());
}
#[tokio::test]
async fn test_service_manager_creation() {
let pool = create_test_pool().await;
let manager = ServiceManager::new(pool);
let _sessions = manager.sessions();
let _messages = manager.messages();
let _files = manager.files();
let _projects = manager.projects();
let _context = manager.context();
}
}