feat: implement media listing with sorting and filtering options
This commit is contained in:
@@ -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