feat: implement album sharing functionality with permissions management
This commit is contained in:
@@ -73,3 +73,17 @@ pub async fn build_album_repository(
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
87
libertas_infra/src/repositories/album_share_repository.rs
Normal file
87
libertas_infra/src/repositories/album_share_repository.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use async_trait::async_trait;
|
||||
use libertas_core::{
|
||||
error::{CoreError, CoreResult},
|
||||
models::AlbumPermission,
|
||||
repositories::AlbumShareRepository,
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PostgresAlbumShareRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PostgresAlbumShareRepository {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AlbumShareRepository for PostgresAlbumShareRepository {
|
||||
async fn create_or_update_share(
|
||||
&self,
|
||||
album_id: Uuid,
|
||||
user_id: Uuid,
|
||||
permission: AlbumPermission,
|
||||
) -> CoreResult<()> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO album_shares (album_id, user_id, permission)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (album_id, user_id)
|
||||
DO UPDATE SET permission = $3
|
||||
"#,
|
||||
album_id,
|
||||
user_id,
|
||||
permission as AlbumPermission,
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_user_permission(
|
||||
&self,
|
||||
album_id: Uuid,
|
||||
user_id: Uuid,
|
||||
) -> CoreResult<Option<AlbumPermission>> {
|
||||
let result = sqlx::query!(
|
||||
r#"
|
||||
SELECT permission as "permission: AlbumPermission"
|
||||
FROM album_shares
|
||||
WHERE album_id = $1 AND user_id = $2
|
||||
"#,
|
||||
album_id,
|
||||
user_id
|
||||
)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(result.map(|row| row.permission))
|
||||
}
|
||||
|
||||
async fn is_media_in_shared_album(&self, media_id: Uuid, user_id: Uuid) -> CoreResult<bool> {
|
||||
let result = sqlx::query!(
|
||||
r#"
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM album_media am
|
||||
JOIN album_shares ash ON am.album_id = ash.album_id
|
||||
WHERE am.media_id = $1 AND ash.user_id = $2
|
||||
)
|
||||
"#,
|
||||
media_id,
|
||||
user_id
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(|e| CoreError::Database(e.to_string()))?;
|
||||
|
||||
Ok(result.exists.unwrap_or(false))
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod album_repository;
|
||||
pub mod album_share_repository;
|
||||
pub mod media_repository;
|
||||
pub mod user_repository;
|
||||
|
||||
Reference in New Issue
Block a user