backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Update UploadSession 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::UploadSession;
use crate::infrastructure::persistence::UploadSessionRepository;

/// Input for updating a UploadSession
#[derive(Debug, Clone)]
pub struct UpdateUploadSessionInput {
    pub id: Uuid,
    pub bucket_id: Option<Uuid>,
    pub user_id: Option<Uuid>,
    pub path: Option<String>,
    pub filename: Option<String>,
    pub mime_type: Option<String>,
    pub file_size: Option<i64>,
    pub chunk_size: Option<i32>,
    pub total_chunks: Option<i32>,
    pub uploaded_chunks: Option<i32>,
    pub status: Option<UploadStatus>,
    pub storage_backend: Option<StorageBackend>,
    pub completed_parts: Option<Vec<i32>>,
    pub part_etags: Option<serde_json::Value>,
    pub expires_at: Option<DateTime<Utc>>,
    pub metadata: Option<serde_json::Value>,
}

/// Output after updating a UploadSession
#[derive(Debug, Clone)]
pub struct UpdateUploadSessionOutput {
    pub upload_session: UploadSession,
}

/// Use case for updating an existing UploadSession
pub struct UpdateUploadSessionUseCase {
    repository: Arc<UploadSessionRepository>,
}

impl UpdateUploadSessionUseCase {
    pub fn new(repository: Arc<UploadSessionRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for UpdateUploadSessionUseCase {
    type Input = UpdateUploadSessionInput;
    type Output = UpdateUploadSessionOutput;

    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!("UploadSession not found"))?;

        // Apply updates
        if let Some(val) = input.bucket_id {
            entity.bucket_id = val;
        }
        if let Some(val) = input.user_id {
            entity.user_id = val;
        }
        if let Some(val) = input.path {
            entity.path = val;
        }
        if let Some(val) = input.filename {
            entity.filename = val;
        }
        if input.mime_type.is_some() {
            entity.mime_type = input.mime_type;
        }
        if let Some(val) = input.file_size {
            entity.file_size = val;
        }
        if let Some(val) = input.chunk_size {
            entity.chunk_size = val;
        }
        if let Some(val) = input.total_chunks {
            entity.total_chunks = val;
        }
        if let Some(val) = input.uploaded_chunks {
            entity.uploaded_chunks = 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.completed_parts {
            entity.completed_parts = val;
        }
        if input.part_etags.is_some() {
            entity.part_etags = input.part_etags;
        }
        if let Some(val) = input.expires_at {
            entity.expires_at = val;
        }
        if input.metadata.is_some() {
            entity.metadata = input.metadata;
        }

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

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

        Ok(UpdateUploadSessionOutput { upload_session: updated })
    }
}