feat: expand workspace to include libertas_infra and libertas_worker
feat(libertas_api): add dependency on libertas_infra and async-nats refactor(libertas_api): consolidate config loading and add broker_url refactor(libertas_api): integrate NATS client into app state and services feat(libertas_core): introduce config module for database and server settings fix(libertas_core): enhance error handling with detailed messages feat(libertas_infra): create infrastructure layer with database repositories feat(libertas_infra): implement Postgres repositories for media and albums feat(libertas_worker): add worker service to process media jobs via NATS
This commit is contained in:
93
libertas_infra/src/repositories/media_repository.rs
Normal file
93
libertas_infra/src/repositories/media_repository.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{
|
||||
error::{CoreError, CoreResult},
|
||||
models::Media,
|
||||
repositories::MediaRepository,
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PostgresMediaRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresMediaRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl MediaRepository for PostgresMediaRepository {
|
||||
async fn create(&self, media: &Media) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO media (id, owner_id, storage_path, original_filename, mime_type, hash, created_at, width, height)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
"#,
|
||||
media.id,
|
||||
media.owner_id,
|
||||
media.storage_path,
|
||||
media.original_filename,
|
||||
media.mime_type,
|
||||
media.hash,
|
||||
media.created_at,
|
||||
media.width,
|
||||
media.height
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn find_by_hash(&self, hash: &str) -> CoreResult<Option<Media>> {
|
||||
sqlx::query_as!(
|
||||
Media,
|
||||
r#"
|
||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||
extracted_location, width, height
|
||||
FROM media
|
||||
WHERE hash = $1
|
||||
"#,
|
||||
hash
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))
|
||||
}
|
||||
|
||||
async fn find_by_id(&self, id: Uuid) -> CoreResult<Option<Media>> {
|
||||
sqlx::query_as!(
|
||||
Media,
|
||||
r#"
|
||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||
extracted_location, width, height
|
||||
FROM media
|
||||
WHERE id = $1
|
||||
"#,
|
||||
id
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))
|
||||
}
|
||||
|
||||
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Media>> {
|
||||
sqlx::query_as!(
|
||||
Media,
|
||||
r#"
|
||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||
extracted_location, width, height
|
||||
FROM media
|
||||
WHERE owner_id = $1
|
||||
"#,
|
||||
user_id
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user