2025-03-19 19:52:12 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
|
|
use crate::application::dtos::file_dto::FileDto;
|
|
|
|
|
use crate::application::ports::file_ports::FileManagementUseCase;
|
|
|
|
|
use crate::application::ports::storage_ports::FileWritePort;
|
|
|
|
|
use crate::common::errors::DomainError;
|
|
|
|
|
|
2025-03-30 14:17:09 +00:00
|
|
|
/// Service for file management operations
|
2025-03-19 19:52:12 +01:00
|
|
|
pub struct FileManagementService {
|
|
|
|
|
file_repository: Arc<dyn FileWritePort>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FileManagementService {
|
2025-03-30 14:17:09 +00:00
|
|
|
/// Creates a new file management service
|
2025-03-19 19:52:12 +01:00
|
|
|
pub fn new(file_repository: Arc<dyn FileWritePort>) -> Self {
|
|
|
|
|
Self { file_repository }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl FileManagementUseCase for FileManagementService {
|
|
|
|
|
async fn move_file(&self, file_id: &str, folder_id: Option<String>) -> Result<FileDto, DomainError> {
|
2025-03-30 14:17:09 +00:00
|
|
|
tracing::info!("Moving file with ID: {} to folder: {:?}", file_id, folder_id);
|
2025-03-19 19:52:12 +01:00
|
|
|
|
|
|
|
|
let moved_file = self.file_repository.move_file(file_id, folder_id).await
|
|
|
|
|
.map_err(|e| {
|
2025-03-30 14:17:09 +00:00
|
|
|
tracing::error!("Error moving file (ID: {}): {}", file_id, e);
|
2025-03-19 19:52:12 +01:00
|
|
|
e
|
|
|
|
|
})?;
|
|
|
|
|
|
2025-03-30 14:17:09 +00:00
|
|
|
tracing::info!("File moved successfully: {} (ID: {}) to folder: {:?}",
|
2025-03-19 19:52:12 +01:00
|
|
|
moved_file.name(), moved_file.id(), moved_file.folder_id());
|
|
|
|
|
|
|
|
|
|
Ok(FileDto::from(moved_file))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn delete_file(&self, id: &str) -> Result<(), DomainError> {
|
|
|
|
|
self.file_repository.delete_file(id).await
|
|
|
|
|
}
|
|
|
|
|
}
|