2025-03-19 00:44:27 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
use axum::{
|
|
|
|
|
extract::{State, Json},
|
|
|
|
|
response::IntoResponse,
|
|
|
|
|
http::StatusCode,
|
|
|
|
|
};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
use crate::application::services::batch_operations::{
|
|
|
|
|
BatchOperationService, BatchResult, BatchStats
|
|
|
|
|
};
|
|
|
|
|
use crate::application::dtos::file_dto::FileDto;
|
|
|
|
|
use crate::application::dtos::folder_dto::FolderDto;
|
|
|
|
|
use crate::interfaces::api::handlers::ApiResult;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Shared state for the batch handler
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct BatchHandlerState {
|
|
|
|
|
pub batch_service: Arc<BatchOperationService>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// DTO for batch file operation requests
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct BatchFileOperationRequest {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// IDs of the files to process
|
2025-03-19 00:44:27 +01:00
|
|
|
pub file_ids: Vec<String>,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Target folder ID (optional)
|
2025-03-19 00:44:27 +01:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub target_folder_id: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// DTO for batch folder operation requests
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct BatchFolderOperationRequest {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// IDs of the folders to process
|
2025-03-19 00:44:27 +01:00
|
|
|
pub folder_ids: Vec<String>,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Whether the operation should be recursive
|
2025-03-19 00:44:27 +01:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub recursive: bool,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Target folder ID (optional)
|
2025-03-19 00:44:27 +01:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub target_folder_id: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// DTO for batch folder creation requests
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct BatchCreateFoldersRequest {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Details of the folders to create
|
2025-03-19 00:44:27 +01:00
|
|
|
pub folders: Vec<CreateFolderDetail>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Detail for folder creation
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
pub struct CreateFolderDetail {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Folder name
|
2025-03-19 00:44:27 +01:00
|
|
|
pub name: String,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Parent folder ID (optional)
|
2025-03-19 00:44:27 +01:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub parent_id: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// DTO for batch operation results
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
|
pub struct BatchOperationResponse<T> {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Successfully processed entities
|
2025-03-19 00:44:27 +01:00
|
|
|
pub successful: Vec<T>,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Failed operations with their error messages
|
2025-03-19 00:44:27 +01:00
|
|
|
pub failed: Vec<FailedOperation>,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Operation statistics
|
2025-03-19 00:44:27 +01:00
|
|
|
pub stats: BatchOperationStats,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Failed operation in a batch
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
|
pub struct FailedOperation {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Identifier of the entity that failed
|
2025-03-19 00:44:27 +01:00
|
|
|
pub id: String,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Error message
|
2025-03-19 00:44:27 +01:00
|
|
|
pub error: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Statistics for a batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
|
pub struct BatchOperationStats {
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Total number of operations
|
2025-03-19 00:44:27 +01:00
|
|
|
pub total: usize,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Number of successful operations
|
2025-03-19 00:44:27 +01:00
|
|
|
pub successful: usize,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Number of failed operations
|
2025-03-19 00:44:27 +01:00
|
|
|
pub failed: usize,
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Total execution time in milliseconds
|
2025-03-19 00:44:27 +01:00
|
|
|
pub execution_time_ms: u128,
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Converts domain BatchStats to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
impl From<BatchStats> for BatchOperationStats {
|
|
|
|
|
fn from(stats: BatchStats) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
total: stats.total,
|
|
|
|
|
successful: stats.successful,
|
|
|
|
|
failed: stats.failed,
|
|
|
|
|
execution_time_ms: stats.execution_time_ms,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Converts domain BatchResult<T> to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
impl<T, U> From<BatchResult<T>> for BatchOperationResponse<U>
|
|
|
|
|
where
|
|
|
|
|
U: From<T>,
|
|
|
|
|
{
|
|
|
|
|
fn from(result: BatchResult<T>) -> Self {
|
|
|
|
|
let successful = result.successful.into_iter().map(U::from).collect();
|
|
|
|
|
|
|
|
|
|
let failed = result.failed.into_iter()
|
|
|
|
|
.map(|(id, error)| FailedOperation { id, error })
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
successful,
|
|
|
|
|
failed,
|
|
|
|
|
stats: result.stats.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for moving multiple files in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn move_files_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchFileOperationRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are files to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.file_ids.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No file IDs provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.move_files(request.file_ids, request.target_folder_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert result to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
let response: BatchOperationResponse<FileDto> = result.into();
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::OK // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for copying multiple files in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn copy_files_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchFileOperationRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are files to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.file_ids.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No file IDs provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.copy_files(request.file_ids, request.target_folder_id)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert result to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
let response: BatchOperationResponse<FileDto> = result.into();
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::OK // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for deleting multiple files in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn delete_files_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchFileOperationRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are files to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.file_ids.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No file IDs provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.delete_files(request.file_ids)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Create custom response for string IDs
|
2025-03-19 00:44:27 +01:00
|
|
|
let response = BatchOperationResponse {
|
|
|
|
|
successful: result.successful,
|
|
|
|
|
failed: result.failed.into_iter()
|
|
|
|
|
.map(|(id, error)| FailedOperation { id, error })
|
|
|
|
|
.collect(),
|
|
|
|
|
stats: result.stats.into(),
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::OK // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for deleting multiple folders in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn delete_folders_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchFolderOperationRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are folders to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.folder_ids.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No folder IDs provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.delete_folders(request.folder_ids, request.recursive)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Create custom response for string IDs
|
2025-03-19 00:44:27 +01:00
|
|
|
let response = BatchOperationResponse {
|
|
|
|
|
successful: result.successful,
|
|
|
|
|
failed: result.failed.into_iter()
|
|
|
|
|
.map(|(id, error)| FailedOperation { id, error })
|
|
|
|
|
.collect(),
|
|
|
|
|
stats: result.stats.into(),
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::OK // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for creating multiple folders in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn create_folders_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchCreateFoldersRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are folders to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.folders.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No folders provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Transform the format for the service
|
2025-03-19 00:44:27 +01:00
|
|
|
let folders = request.folders
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|detail| (detail.name, detail.parent_id))
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.create_folders(folders)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert result to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
let response: BatchOperationResponse<FolderDto> = result.into();
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::CREATED // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for getting multiple files in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn get_files_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchFileOperationRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are files to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.file_ids.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No file IDs provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.get_multiple_files(request.file_ids)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert result to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
let response: BatchOperationResponse<FileDto> = result.into();
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::OK // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
/// Handler for getting multiple folders in batch
|
2025-03-19 00:44:27 +01:00
|
|
|
pub async fn get_folders_batch(
|
|
|
|
|
State(state): State<BatchHandlerState>,
|
|
|
|
|
Json(request): Json<BatchFolderOperationRequest>,
|
|
|
|
|
) -> ApiResult<impl IntoResponse> {
|
2026-02-12 09:41:25 +01:00
|
|
|
// Verify there are folders to process
|
2025-03-19 00:44:27 +01:00
|
|
|
if request.folder_ids.is_empty() {
|
|
|
|
|
return Ok((
|
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
|
Json(serde_json::json!({
|
|
|
|
|
"error": "No folder IDs provided"
|
|
|
|
|
}))
|
|
|
|
|
).into_response());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Execute batch operation
|
2025-03-19 00:44:27 +01:00
|
|
|
let result = state.batch_service
|
|
|
|
|
.get_multiple_folders(request.folder_ids)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Convert result to DTO
|
2025-03-19 00:44:27 +01:00
|
|
|
let response: BatchOperationResponse<FolderDto> = result.into();
|
|
|
|
|
|
2026-02-12 09:41:25 +01:00
|
|
|
// Determine status code based on results
|
2025-03-19 00:44:27 +01:00
|
|
|
let status_code = if response.stats.failed > 0 {
|
|
|
|
|
if response.stats.successful > 0 {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::PARTIAL_CONTENT // Some operations successful, others failed
|
2025-03-19 00:44:27 +01:00
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::BAD_REQUEST // All failed
|
2025-03-19 00:44:27 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-02-12 09:41:25 +01:00
|
|
|
StatusCode::OK // All successful
|
2025-03-19 00:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok((status_code, Json(response)).into_response())
|
|
|
|
|
}
|