feat: implement media listing with sorting and filtering options
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use libertas_core::{
|
||||
config::{DatabaseConfig, DatabaseType},
|
||||
config::{Config, DatabaseConfig, DatabaseType},
|
||||
error::{CoreError, CoreResult},
|
||||
repositories::UserRepository,
|
||||
};
|
||||
@@ -47,12 +47,12 @@ pub async fn build_user_repository(
|
||||
}
|
||||
|
||||
pub async fn build_media_repository(
|
||||
_db_config: &DatabaseConfig,
|
||||
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),
|
||||
crate::repositories::media_repository::PostgresMediaRepository::new(pg_pool, config),
|
||||
)),
|
||||
DatabasePool::Sqlite(_sqlite_pool) => Err(CoreError::Database(
|
||||
"Sqlite media repository not implemented".to_string(),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod factory;
|
||||
pub mod repositories;
|
||||
pub mod db_models;
|
||||
pub mod mappers;
|
||||
pub mod mappers;
|
||||
pub mod query_builder;
|
||||
80
libertas_infra/src/query_builder.rs
Normal file
80
libertas_infra/src/query_builder.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use libertas_core::{error::{CoreError, CoreResult}, schema::{ListMediaOptions, SortOrder}};
|
||||
use sqlx::QueryBuilder as SqlxQueryBuilder;
|
||||
|
||||
pub trait QueryBuilder<T> {
|
||||
fn apply_options_to_query<'a>(
|
||||
&self,
|
||||
query: SqlxQueryBuilder<'a, sqlx::Postgres>,
|
||||
options: &'a T,
|
||||
) -> CoreResult<SqlxQueryBuilder<'a, sqlx::Postgres>>;
|
||||
}
|
||||
|
||||
pub struct MediaQueryBuilder {
|
||||
allowed_sort_columns: Vec<String>,
|
||||
}
|
||||
|
||||
impl MediaQueryBuilder {
|
||||
pub fn new(allowed_sort_columns: Vec<String>) -> Self {
|
||||
Self {
|
||||
allowed_sort_columns,
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_sort_column<'a>(&self, column: &'a str) -> CoreResult<&'a str> {
|
||||
if self.allowed_sort_columns.contains(&column.to_string()) {
|
||||
Ok(column)
|
||||
} else {
|
||||
Err(CoreError::Validation(format!(
|
||||
"Sorting by '{}' is not supported",
|
||||
column
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl QueryBuilder<ListMediaOptions> for MediaQueryBuilder {
|
||||
fn apply_options_to_query<'a>(
|
||||
&self,
|
||||
mut query: SqlxQueryBuilder<'a, sqlx::Postgres>,
|
||||
options: &'a ListMediaOptions,
|
||||
) -> CoreResult<SqlxQueryBuilder<'a, sqlx::Postgres>> {
|
||||
if let Some(filter) = &options.filter {
|
||||
// In the future, you would add logic here:
|
||||
// if let Some(mime) = &filter.mime_type {
|
||||
// query.push(" AND mime_type = ");
|
||||
// query.push_bind(mime);
|
||||
// }
|
||||
}
|
||||
|
||||
if let Some(sort) = &options.sort {
|
||||
let column = self.validate_sort_column(&sort.sort_by)?;
|
||||
|
||||
let direction = match sort.sort_order {
|
||||
SortOrder::Asc => "ASC",
|
||||
SortOrder::Desc => "DESC",
|
||||
};
|
||||
|
||||
let nulls_order = if direction == "ASC" {
|
||||
"NULLS LAST"
|
||||
} else {
|
||||
"NULLS FIRST"
|
||||
};
|
||||
|
||||
let order_by_clause = format!("ORDER BY {} {} {}", column, direction, nulls_order);
|
||||
query.push(order_by_clause);
|
||||
|
||||
} else {
|
||||
query.push("ORDER BY date_taken DESC NULLS FIRST");
|
||||
}
|
||||
|
||||
// --- 3. Apply Pagination (Future-Proofing Stub) ---
|
||||
// if let Some(pagination) = &options.pagination {
|
||||
// query.push(" LIMIT ");
|
||||
// query.push_bind(pagination.limit);
|
||||
// query.push(" OFFSET ");
|
||||
// query.push_bind(pagination.offset);
|
||||
// }
|
||||
|
||||
Ok(query)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,28 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{
|
||||
error::{CoreError, CoreResult},
|
||||
models::Media,
|
||||
repositories::MediaRepository,
|
||||
config::Config, error::{CoreError, CoreResult}, models::Media, repositories::MediaRepository, schema::ListMediaOptions
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::db_models::PostgresMedia;
|
||||
use crate::{db_models::PostgresMedia, query_builder::{MediaQueryBuilder, QueryBuilder}};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PostgresMediaRepository {
|
||||
pool: PgPool,
|
||||
query_builder: Arc<MediaQueryBuilder>,
|
||||
}
|
||||
|
||||
impl PostgresMediaRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
pub fn new(pool: PgPool, config: &Config) -> Self {
|
||||
let allowed_columns = config
|
||||
.allowed_sort_columns
|
||||
.clone()
|
||||
.unwrap_or_else(|| vec!["created_at".to_string()]);
|
||||
|
||||
Self { pool, query_builder: Arc::new(MediaQueryBuilder::new(allowed_columns)) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,22 +87,30 @@ impl MediaRepository for PostgresMediaRepository {
|
||||
Ok(pg_media.map(|m| m.into()))
|
||||
}
|
||||
|
||||
async fn list_by_user(&self, user_id: Uuid) -> CoreResult<Vec<Media>> {
|
||||
let pg_media = sqlx::query_as!(
|
||||
PostgresMedia,
|
||||
async fn list_by_user(&self, user_id: Uuid, options: &ListMediaOptions) -> CoreResult<Vec<Media>> {
|
||||
let mut query = sqlx::QueryBuilder::new(
|
||||
r#"
|
||||
SELECT id, owner_id, storage_path, original_filename, mime_type, hash, created_at,
|
||||
extracted_location, width, height, date_taken
|
||||
FROM media
|
||||
WHERE owner_id = $1
|
||||
WHERE owner_id =
|
||||
"#,
|
||||
user_id
|
||||
)
|
||||
);
|
||||
|
||||
query.push_bind(user_id);
|
||||
|
||||
query = self
|
||||
.query_builder
|
||||
.apply_options_to_query(query, options)?;
|
||||
|
||||
let pg_media = query
|
||||
.build_query_as::<PostgresMedia>()
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(pg_media.into_iter().map(|m| m.into()).collect())
|
||||
|
||||
let media_list = pg_media.into_iter().map(|m| m.into()).collect();
|
||||
Ok(media_list)
|
||||
}
|
||||
|
||||
async fn update_metadata(
|
||||
|
||||
Reference in New Issue
Block a user