backbone-bucket 0.2.0

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

/// Input for creating a ContentHash
#[derive(Debug, Clone)]
pub struct CreateContentHashInput {
    pub hash: String,
    pub size_bytes: i64,
    pub storage_key: String,
    pub storage_backend: StorageBackend,
    pub reference_count: i32,
    pub first_uploaded_at: DateTime<Utc>,
    pub last_referenced_at: DateTime<Utc>,
    pub fingerprint: Option<String>,
    pub metadata: Option<serde_json::Value>,
}

/// Output after creating a ContentHash
#[derive(Debug, Clone)]
pub struct CreateContentHashOutput {
    pub content_hash: ContentHash,
}

/// Use case for creating a new ContentHash
pub struct CreateContentHashUseCase {
    repository: Arc<ContentHashRepository>,
}

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

#[async_trait]
impl UseCase for CreateContentHashUseCase {
    type Input = CreateContentHashInput;
    type Output = CreateContentHashOutput;

    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
        // Create entity from input
        let entity = ContentHash {
            id: None,
            hash: input.hash,
            size_bytes: input.size_bytes,
            storage_key: input.storage_key,
            storage_backend: input.storage_backend,
            reference_count: input.reference_count,
            first_uploaded_at: input.first_uploaded_at,
            last_referenced_at: input.last_referenced_at,
            fingerprint: input.fingerprint,
            metadata: input.metadata,
        };

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

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

        Ok(CreateContentHashOutput { content_hash: created })
    }
}