backbone-bucket 0.2.0

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

use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;

use super::UseCase;
use crate::domain::entity::UploadSession;
use crate::infrastructure::persistence::UploadSessionRepository;

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

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

/// Use case for creating a new UploadSession
pub struct CreateUploadSessionUseCase {
    repository: Arc<UploadSessionRepository>,
}

impl CreateUploadSessionUseCase {
    /// Create a new use case instance
    pub fn new(repository: Arc<UploadSessionRepository>) -> Self {
        Self { repository }
    }
}

#[async_trait]
impl UseCase for CreateUploadSessionUseCase {
    type Input = CreateUploadSessionInput;
    type Output = CreateUploadSessionOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        // Create entity from input
        let entity = UploadSession {
            id: None,
            bucket_id: input.bucket_id,
            user_id: input.user_id,
            path: input.path,
            filename: input.filename,
            mime_type: input.mime_type,
            file_size: input.file_size,
            chunk_size: input.chunk_size,
            total_chunks: input.total_chunks,
            uploaded_chunks: input.uploaded_chunks,
            status: input.status,
            storage_backend: input.storage_backend,
            completed_parts: input.completed_parts,
            part_etags: input.part_etags,
            expires_at: input.expires_at,
            metadata: input.metadata,
        };

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

        // Persist entity
        let created = self.repository.create(entity).await?;

        Ok(CreateUploadSessionOutput { upload_session: created })
    }
}