backbone-bucket 0.2.1

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Update StoredFile use case
//!
//! Generated by backbone-schema. Do not edit manually.

use std::sync::Arc;

use anyhow::{Result, anyhow};
use async_trait::async_trait;
use uuid::Uuid;

use super::UseCase;
use crate::domain::entity::StoredFile;
use crate::infrastructure::persistence::StoredFileRepository;

/// Input for updating a StoredFile
#[derive(Debug, Clone)]
pub struct UpdateStoredFileInput {
    pub id: Uuid,
    pub bucket_id: Option<Uuid>,
    pub owner_id: Option<Uuid>,
    pub path: Option<String>,
    pub original_name: Option<String>,
    pub size_bytes: Option<i64>,
    pub mime_type: Option<String>,
    pub checksum: Option<String>,
    pub is_compressed: Option<bool>,
    pub original_size: Option<i64>,
    pub compression_algorithm: Option<String>,
    pub is_scanned: Option<bool>,
    pub scan_result: Option<serde_json::Value>,
    pub threat_level: Option<ThreatLevel>,
    pub has_thumbnail: Option<bool>,
    pub thumbnail_path: Option<String>,
    pub has_video_thumbnail: Option<bool>,
    pub has_document_preview: Option<bool>,
    pub processing_status: Option<ProcessingStatus>,
    pub content_hash_id: Option<Uuid>,
    pub cdn_url: Option<String>,
    pub cdn_url_expires_at: Option<DateTime<Utc>>,
    pub status: Option<FileStatus>,
    pub storage_key: Option<String>,
    pub version: Option<i32>,
    pub previous_version_id: Option<Uuid>,
    pub download_count: Option<i32>,
    pub last_accessed_at: Option<DateTime<Utc>>,
    pub metadata: Option<serde_json::Value>,
}

/// Output after updating a StoredFile
#[derive(Debug, Clone)]
pub struct UpdateStoredFileOutput {
    pub stored_file: StoredFile,
}

/// Use case for updating an existing StoredFile
pub struct UpdateStoredFileUseCase {
    repository: Arc<StoredFileRepository>,
}

impl UpdateStoredFileUseCase {
    pub fn new(repository: Arc<StoredFileRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateStoredFileUseCase {
    type Input = UpdateStoredFileInput;
    type Output = UpdateStoredFileOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        // Fetch existing entity
        let mut entity = self.repository.find_by_id(input.id).await?
            .ok_or_else(|| anyhow!("StoredFile not found"))?;

        // Apply updates
        if let Some(val) = input.bucket_id {
            entity.bucket_id = val;
        }
        if let Some(val) = input.owner_id {
            entity.owner_id = val;
        }
        if let Some(val) = input.path {
            entity.path = val;
        }
        if let Some(val) = input.original_name {
            entity.original_name = val;
        }
        if let Some(val) = input.size_bytes {
            entity.size_bytes = val;
        }
        if let Some(val) = input.mime_type {
            entity.mime_type = val;
        }
        if input.checksum.is_some() {
            entity.checksum = input.checksum;
        }
        if let Some(val) = input.is_compressed {
            entity.is_compressed = val;
        }
        if input.original_size.is_some() {
            entity.original_size = input.original_size;
        }
        if input.compression_algorithm.is_some() {
            entity.compression_algorithm = input.compression_algorithm;
        }
        if let Some(val) = input.is_scanned {
            entity.is_scanned = val;
        }
        if input.scan_result.is_some() {
            entity.scan_result = input.scan_result;
        }
        if input.threat_level.is_some() {
            entity.threat_level = input.threat_level;
        }
        if let Some(val) = input.has_thumbnail {
            entity.has_thumbnail = val;
        }
        if input.thumbnail_path.is_some() {
            entity.thumbnail_path = input.thumbnail_path;
        }
        if let Some(val) = input.has_video_thumbnail {
            entity.has_video_thumbnail = val;
        }
        if let Some(val) = input.has_document_preview {
            entity.has_document_preview = val;
        }
        if input.processing_status.is_some() {
            entity.processing_status = input.processing_status;
        }
        if input.content_hash_id.is_some() {
            entity.content_hash_id = input.content_hash_id;
        }
        if input.cdn_url.is_some() {
            entity.cdn_url = input.cdn_url;
        }
        if input.cdn_url_expires_at.is_some() {
            entity.cdn_url_expires_at = input.cdn_url_expires_at;
        }
        if let Some(val) = input.status {
            entity.status = val;
        }
        if let Some(val) = input.storage_key {
            entity.storage_key = val;
        }
        if let Some(val) = input.version {
            entity.version = val;
        }
        if input.previous_version_id.is_some() {
            entity.previous_version_id = input.previous_version_id;
        }
        if let Some(val) = input.download_count {
            entity.download_count = val;
        }
        if input.last_accessed_at.is_some() {
            entity.last_accessed_at = input.last_accessed_at;
        }
        if let Some(val) = input.metadata {
            entity.metadata = val;
        }

        // <<< CUSTOM VALIDATION START >>>
        // Add custom validation logic here
        // <<< CUSTOM VALIDATION END >>>

        // Persist updates
        let updated = self.repository.update(entity).await?;

        Ok(UpdateStoredFileOutput { stored_file: updated })
    }
}