feat: Add public album routes and enhance authorization checks for media and albums
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{
|
||||
error::{CoreError, CoreResult},
|
||||
models::Album,
|
||||
models::{Album, Media},
|
||||
repositories::AlbumRepository,
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::db_models::PostgresAlbum;
|
||||
use crate::db_models::{PostgresAlbum, PostgresMedia};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PostgresAlbumRepository {
|
||||
@@ -72,7 +72,7 @@ impl AlbumRepository for PostgresAlbumRepository {
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
|
||||
Ok(pg_albums.into_iter().map(|a| a.into()).collect())
|
||||
}
|
||||
|
||||
@@ -127,4 +127,43 @@ impl AlbumRepository for PostgresAlbumRepository {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_media_by_album_id(&self, album_id: Uuid) -> CoreResult<Vec<Media>> {
|
||||
let pg_media = sqlx::query_as!(
|
||||
PostgresMedia,
|
||||
r#"
|
||||
SELECT m.id, m.owner_id, m.storage_path, m.original_filename, m.mime_type,
|
||||
m.hash, m.created_at, m.thumbnail_path
|
||||
FROM media m
|
||||
JOIN album_media am ON m.id = am.media_id
|
||||
WHERE am.album_id = $1
|
||||
"#,
|
||||
album_id
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
let media_list = pg_media.into_iter().map(|m| m.into()).collect();
|
||||
Ok(media_list)
|
||||
}
|
||||
|
||||
async fn is_media_in_public_album(&self, media_id: Uuid) -> CoreResult<bool> {
|
||||
let result = sqlx::query!(
|
||||
r#"
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM album_media am
|
||||
JOIN albums a ON am.album_id = a.id
|
||||
WHERE am.media_id = $1 AND a.is_public = TRUE
|
||||
) as "exists!"
|
||||
"#,
|
||||
media_id
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(result.exists)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user