use std::sync::Arc;
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;
use super::UseCase;
use crate::domain::entity::Bucket;
use crate::infrastructure::persistence::BucketRepository;
#[derive(Debug, Clone)]
pub struct UpdateBucketInput {
pub id: Uuid,
pub name: Option<String>,
pub slug: Option<String>,
pub description: Option<String>,
pub owner_id: Option<Uuid>,
pub bucket_type: Option<BucketType>,
pub status: Option<BucketStatus>,
pub storage_backend: Option<StorageBackend>,
pub root_path: Option<String>,
pub file_count: Option<i32>,
pub total_size_bytes: Option<i64>,
pub max_file_size: Option<i64>,
pub allowed_mime_types: Option<Vec<String>>,
pub auto_delete_after_days: Option<i32>,
pub enable_cdn: Option<bool>,
pub enable_versioning: Option<bool>,
pub enable_deduplication: Option<bool>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct UpdateBucketOutput {
pub bucket: Bucket,
}
pub struct UpdateBucketUseCase {
repository: Arc<BucketRepository>,
}
impl UpdateBucketUseCase {
pub fn new(repository: Arc<BucketRepository>) -> Self {
Self { repository }
}
}
#[async_trait]
impl UseCase for UpdateBucketUseCase {
type Input = UpdateBucketInput;
type Output = UpdateBucketOutput;
async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
let mut entity = self.repository.find_by_id(input.id).await?
.ok_or_else(|| anyhow!("Bucket not found"))?;
if let Some(val) = input.name {
entity.name = val;
}
if let Some(val) = input.slug {
entity.slug = val;
}
if input.description.is_some() {
entity.description = input.description;
}
if let Some(val) = input.owner_id {
entity.owner_id = val;
}
if let Some(val) = input.bucket_type {
entity.bucket_type = val;
}
if let Some(val) = input.status {
entity.status = val;
}
if let Some(val) = input.storage_backend {
entity.storage_backend = val;
}
if let Some(val) = input.root_path {
entity.root_path = val;
}
if let Some(val) = input.file_count {
entity.file_count = val;
}
if let Some(val) = input.total_size_bytes {
entity.total_size_bytes = val;
}
if input.max_file_size.is_some() {
entity.max_file_size = input.max_file_size;
}
if let Some(val) = input.allowed_mime_types {
entity.allowed_mime_types = val;
}
if input.auto_delete_after_days.is_some() {
entity.auto_delete_after_days = input.auto_delete_after_days;
}
if let Some(val) = input.enable_cdn {
entity.enable_cdn = val;
}
if let Some(val) = input.enable_versioning {
entity.enable_versioning = val;
}
if let Some(val) = input.enable_deduplication {
entity.enable_deduplication = val;
}
if let Some(val) = input.metadata {
entity.metadata = val;
}
let updated = self.repository.update(entity).await?;
Ok(UpdateBucketOutput { bucket: updated })
}
}