use std::sync::Arc; use libertas_core::{ config::{AppConfig, DatabaseConfig, DatabaseType}, error::{CoreError, CoreResult}, repositories::UserRepository, }; use sqlx::{Pool, Postgres, Sqlite}; use crate::repositories::user_repository::{PostgresUserRepository, SqliteUserRepository}; #[derive(Clone)] pub enum DatabasePool { Postgres(Pool), Sqlite(Pool), } pub async fn build_database_pool(db_config: &DatabaseConfig) -> CoreResult { match db_config.db_type { DatabaseType::Postgres => { let pool = sqlx::postgres::PgPoolOptions::new() .max_connections(50) .connect(&db_config.url) .await .map_err(|e| CoreError::Database(e.to_string()))?; Ok(DatabasePool::Postgres(pool)) } DatabaseType::Sqlite => { let pool = sqlx::sqlite::SqlitePoolOptions::new() .max_connections(2) .connect(&db_config.url) .await .map_err(|e| CoreError::Database(e.to_string()))?; Ok(DatabasePool::Sqlite(pool)) } } } pub async fn build_user_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new(PostgresUserRepository::new(pg_pool))), DatabasePool::Sqlite(sqlite_pool) => Ok(Arc::new(SqliteUserRepository::new(sqlite_pool))), } } pub async fn build_media_repository( config: &AppConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::media_repository::PostgresMediaRepository::new(pg_pool, config), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite media repository not implemented".to_string(), )), } } pub async fn build_album_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::album_repository::PostgresAlbumRepository::new(pg_pool), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite album repository not implemented".to_string(), )), } } pub async fn build_album_share_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::album_share_repository::PostgresAlbumShareRepository::new(pg_pool), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite album share repository not implemented".to_string(), )), } } pub async fn build_media_metadata_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::media_metadata_repository::PostgresMediaMetadataRepository::new( pg_pool, ), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite media metadata repository not implemented".to_string(), )), } } pub async fn build_tag_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::tag_repository::PostgresTagRepository::new(pg_pool), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite tag repository not implemented".to_string(), )), } } pub async fn build_person_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::person_repository::PostgresPersonRepository::new(pg_pool), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite person repository not implemented".to_string(), )), } } pub async fn build_face_region_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::face_region_repository::PostgresFaceRegionRepository::new(pg_pool), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite face region repository not implemented".to_string(), )), } } pub async fn build_person_share_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::person_share_repository::PostgresPersonShareRepository::new( pg_pool, ), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite person share repository not implemented".to_string(), )), } } pub async fn build_media_import_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::media_import_repository::PostgresMediaImportRepository::new( pg_pool, ), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite media import repository not implemented".to_string(), )), } } pub async fn build_face_embedding_repository( _db_config: &DatabaseConfig, pool: DatabasePool, ) -> CoreResult> { match pool { DatabasePool::Postgres(pg_pool) => Ok(Arc::new( crate::repositories::face_embedding_repository::PostgresFaceEmbeddingRepository::new( pg_pool, ), )), DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database( "Sqlite face embedding repository not implemented".to_string(), )), } }