feat: enhance album and media management with update and delete functionalities
This commit is contained in:
@@ -7,7 +7,7 @@ use libertas_core::{
|
||||
error::{CoreError, CoreResult},
|
||||
models::Album,
|
||||
repositories::{AlbumRepository, AlbumShareRepository, MediaRepository},
|
||||
schema::{AddMediaToAlbumData, CreateAlbumData, ShareAlbumData},
|
||||
schema::{AddMediaToAlbumData, CreateAlbumData, ShareAlbumData, UpdateAlbumData},
|
||||
services::AlbumService,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
@@ -139,4 +139,70 @@ impl AlbumService for AlbumServiceImpl {
|
||||
.create_or_update_share(data.album_id, data.target_user_id, data.permission)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn update_album(
|
||||
&self,
|
||||
album_id: Uuid,
|
||||
user_id: Uuid,
|
||||
data: UpdateAlbumData<'_>,
|
||||
) -> CoreResult<Album> {
|
||||
let mut album = self
|
||||
.album_repo
|
||||
.find_by_id(album_id)
|
||||
.await?
|
||||
.ok_or(CoreError::NotFound("Album".to_string(), album_id))?;
|
||||
|
||||
let share_permission = self
|
||||
.album_share_repo
|
||||
.get_user_permission(album_id, user_id)
|
||||
.await?;
|
||||
|
||||
if !authz::can_contribute_to_album(user_id, &album, share_permission) {
|
||||
return Err(CoreError::Auth(
|
||||
"User does not have permission to update this album".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(name) = data.name {
|
||||
if name.is_empty() {
|
||||
return Err(CoreError::Validation(
|
||||
"Album name cannot be empty".to_string(),
|
||||
));
|
||||
}
|
||||
album.name = name.to_string();
|
||||
}
|
||||
|
||||
if let Some(description) = data.description {
|
||||
album.description = description.map(|s| s.to_string());
|
||||
}
|
||||
|
||||
if let Some(is_public) = data.is_public {
|
||||
if !authz::is_owner(user_id, &album) && is_public {
|
||||
return Err(CoreError::Auth(
|
||||
"Only the owner can make an album public".to_string(),
|
||||
));
|
||||
}
|
||||
album.is_public = is_public;
|
||||
}
|
||||
|
||||
self.album_repo.update(album.clone()).await?;
|
||||
|
||||
Ok(album)
|
||||
}
|
||||
|
||||
async fn delete_album(&self, album_id: Uuid, user_id: Uuid) -> CoreResult<()> {
|
||||
let album = self
|
||||
.album_repo
|
||||
.find_by_id(album_id)
|
||||
.await?
|
||||
.ok_or(CoreError::NotFound("Album".to_string(), album_id))?;
|
||||
|
||||
if !authz::is_owner(user_id, &album) {
|
||||
return Err(CoreError::Auth(
|
||||
"Only the album owner can delete the album".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
self.album_repo.delete(album_id).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,4 +192,46 @@ impl MediaService for MediaServiceImpl {
|
||||
|
||||
Err(CoreError::Auth("Access denied".to_string()))
|
||||
}
|
||||
|
||||
async fn delete_media(&self, id: Uuid, user_id: Uuid) -> CoreResult<()> {
|
||||
let media = self
|
||||
.repo
|
||||
.find_by_id(id)
|
||||
.await?
|
||||
.ok_or(CoreError::NotFound("Media".to_string(), id))?;
|
||||
|
||||
let user = self
|
||||
.user_repo
|
||||
.find_by_id(user_id)
|
||||
.await?
|
||||
.ok_or(CoreError::NotFound("User".to_string(), user_id))?;
|
||||
|
||||
if !authz::is_owner(user_id, &media) && !authz::is_admin(&user) {
|
||||
return Err(CoreError::Auth("Access denied".to_string()));
|
||||
}
|
||||
|
||||
let full_path = PathBuf::from(&self.config.media_library_path).join(&media.storage_path);
|
||||
self.repo.delete(id).await?;
|
||||
|
||||
let file_size = match fs::metadata(&full_path).await {
|
||||
Ok(metadata) => metadata.len() as i64,
|
||||
Err(_) => 0,
|
||||
};
|
||||
|
||||
if let Err(e) = fs::remove_file(full_path).await {
|
||||
tracing::error!("Failed to delete media file from disk: {}", e);
|
||||
}
|
||||
|
||||
self.user_repo
|
||||
.update_storage_used(user.id, -file_size)
|
||||
.await?;
|
||||
|
||||
let job_payload = json!({ "media_id": id });
|
||||
self.nats_client
|
||||
.publish("media.deleted".to_string(), job_payload.to_string().into())
|
||||
.await
|
||||
.map_err(|e| CoreError::Unknown(format!("Failed to publish NATS job: {}", e)))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user