Files
k-photos/libertas_infra/src/factory.rs

105 lines
3.5 KiB
Rust

use std::sync::Arc;
use libertas_core::{
config::{Config, 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<Postgres>),
Sqlite(Pool<Sqlite>),
}
pub async fn build_database_pool(db_config: &DatabaseConfig) -> CoreResult<DatabasePool> {
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<Arc<dyn UserRepository>> {
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: &Config,
pool: DatabasePool,
) -> CoreResult<Arc<dyn libertas_core::repositories::MediaRepository>> {
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<Arc<dyn libertas_core::repositories::AlbumRepository>> {
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<Arc<dyn libertas_core::repositories::AlbumShareRepository>> {
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<Arc<dyn libertas_core::repositories::MediaMetadataRepository>> {
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(),
)),
}
}