backbone-bucket 0.4.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! ContentHash DTOs (Data Transfer Objects)
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! DTOs provide a clean separation between domain entities and API
//! representations, with validation and OpenAPI documentation support.

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};

#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

#[cfg(feature = "validation")]
use validator::Validate;

use crate::domain::entity::ContentHash;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::StorageBackend;

// =============================================================================
// Create DTO
// =============================================================================

/// Request DTO for creating a new ContentHash
///
/// Used in POST requests to create new entities.
/// Excludes auto-generated fields (id, created_at, updated_at, deleted_at).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateContentHashDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub hash: String,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "size_bytes")]
    pub size_bytes: i64,
    #[cfg_attr(feature = "validation", validate(length(max = 1024)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "storage_key")]
    pub storage_key: String,
    #[serde(alias = "storage_backend")]
    pub storage_backend: StorageBackend,
    #[cfg_attr(feature = "validation", validate(range(min = 1)))]
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "reference_count")]
    pub reference_count: i32,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "first_uploaded_at")]
    pub first_uploaded_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "last_referenced_at")]
    pub last_referenced_at: DateTime<Utc>,
    #[cfg_attr(feature = "validation", validate(length(max = 128)))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fingerprint: Option<String>,
}

// =============================================================================
// Update DTO
// =============================================================================

/// Request DTO for full update of a ContentHash
///
/// Used in PUT requests for full entity replacement.
/// All fields are required (except auto-generated ones).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateContentHashDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub hash: String,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "size_bytes")]
    pub size_bytes: i64,
    #[cfg_attr(feature = "validation", validate(length(max = 1024)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "storage_key")]
    pub storage_key: String,
    #[serde(alias = "storage_backend")]
    pub storage_backend: StorageBackend,
    #[cfg_attr(feature = "validation", validate(range(min = 1)))]
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(alias = "reference_count")]
    pub reference_count: i32,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "first_uploaded_at")]
    pub first_uploaded_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "last_referenced_at")]
    pub last_referenced_at: DateTime<Utc>,
    #[cfg_attr(feature = "validation", validate(length(max = 128)))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fingerprint: Option<String>,
}

// =============================================================================
// Patch DTO
// =============================================================================

/// Request DTO for partial update of a ContentHash
///
/// Used in PATCH requests for partial updates.
/// All fields are optional - only provided fields will be updated.
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchContentHashDto {
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hash: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "size_bytes")]
    pub size_bytes: Option<i64>,
    #[cfg_attr(feature = "validation", validate(length(max = 1024)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "storage_key")]
    pub storage_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "storage_backend")]
    pub storage_backend: Option<StorageBackend>,
    #[cfg_attr(feature = "validation", validate(range(min = 1)))]
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "reference_count")]
    pub reference_count: Option<i32>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "first_uploaded_at")]
    pub first_uploaded_at: Option<DateTime<Utc>>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "last_referenced_at")]
    pub last_referenced_at: Option<DateTime<Utc>>,
    #[cfg_attr(feature = "validation", validate(length(max = 128)))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fingerprint: Option<String>,
}

impl PatchContentHashDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.hash.is_some() || self.size_bytes.is_some() || self.storage_key.is_some() || self.storage_backend.is_some() || self.reference_count.is_some() || self.first_uploaded_at.is_some() || self.last_referenced_at.is_some() || self.fingerprint.is_some()
    }
}

// =============================================================================
// Response DTO
// =============================================================================

/// Response DTO for ContentHash entity
///
/// Used in API responses for single entity.
/// Includes all fields including metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ContentHashResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub hash: String,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub size_bytes: i64,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub storage_key: String,
    pub storage_backend: StorageBackend,
    #[cfg_attr(feature = "openapi", schema(example = 42))]
    pub reference_count: i32,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub first_uploaded_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub last_referenced_at: DateTime<Utc>,
    pub fingerprint: Option<String>,
    pub metadata: AuditMetadata,
}

// =============================================================================
// List Response DTO
// =============================================================================

/// Paginated list response for ContentHash entities
///
/// Used in API responses for list endpoints.
/// Includes pagination metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ContentHashListResponseDto {
    /// List of items
    pub items: Vec<ContentHashResponseDto>,
    /// Total number of items (across all pages)
    pub total: u64,
    /// Current page number (1-indexed)
    pub page: u32,
    /// Number of items per page
    pub per_page: u32,
    /// Total number of pages
    pub total_pages: u32,
    /// Whether there is a next page
    pub has_next: bool,
    /// Whether there is a previous page
    pub has_prev: bool,
}

impl ContentHashListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<ContentHashResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
        let total_pages = if per_page > 0 {
            ((total as f64) / (per_page as f64)).ceil() as u32
        } else {
            0
        };
        Self {
            items,
            total,
            page,
            per_page,
            total_pages,
            has_next: page < total_pages,
            has_prev: page > 1,
        }
    }
}

/// Summary DTO for list views (compact version)
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ContentHashSummaryDto {
    pub id: Uuid,
    pub hash: String,
    pub size_bytes: i64,
    pub storage_key: String,
    pub created_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Conversions
// =============================================================================

impl From<ContentHash> for ContentHashResponseDto {
    fn from(entity: ContentHash) -> Self {
        Self {
            id: entity.id,
            hash: entity.hash,
            size_bytes: entity.size_bytes,
            storage_key: entity.storage_key,
            storage_backend: entity.storage_backend,
            reference_count: entity.reference_count,
            first_uploaded_at: entity.first_uploaded_at,
            last_referenced_at: entity.last_referenced_at,
            fingerprint: entity.fingerprint,
            metadata: entity.metadata,
        }
    }
}

impl From<ContentHash> for ContentHashSummaryDto {
    fn from(entity: ContentHash) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            hash: entity.hash,
            size_bytes: entity.size_bytes,
            storage_key: entity.storage_key,
            created_at,
        }
    }
}

impl From<CreateContentHashDto> for ContentHash {
    fn from(dto: CreateContentHashDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            hash: dto.hash,
            size_bytes: dto.size_bytes,
            storage_key: dto.storage_key,
            storage_backend: dto.storage_backend,
            reference_count: dto.reference_count,
            first_uploaded_at: dto.first_uploaded_at,
            last_referenced_at: dto.last_referenced_at,
            fingerprint: dto.fingerprint,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&ContentHash> for ContentHashResponseDto {
    fn from(entity: &ContentHash) -> Self {
        Self {
            id: entity.id.clone(),
            hash: entity.hash.clone(),
            size_bytes: entity.size_bytes.clone(),
            storage_key: entity.storage_key.clone(),
            storage_backend: entity.storage_backend.clone(),
            reference_count: entity.reference_count.clone(),
            first_uploaded_at: entity.first_uploaded_at.clone(),
            last_referenced_at: entity.last_referenced_at.clone(),
            fingerprint: entity.fingerprint.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

impl backbone_core::FromCreateDto<CreateContentHashDto> for ContentHash {
    fn from_create_dto(dto: CreateContentHashDto) -> backbone_core::ServiceResult<Self> {
        Ok(ContentHash::from(dto))
    }
}

impl backbone_core::ApplyUpdateDto<UpdateContentHashDto> for ContentHash {
    fn apply_update(mut self, dto: UpdateContentHashDto) -> backbone_core::ServiceResult<Self> {
        self.hash = dto.hash;
        self.size_bytes = dto.size_bytes;
        self.storage_key = dto.storage_key;
        self.storage_backend = dto.storage_backend;
        self.reference_count = dto.reference_count;
        self.first_uploaded_at = dto.first_uploaded_at;
        self.last_referenced_at = dto.last_referenced_at;
        self.fingerprint = dto.fingerprint;
        Ok(self)
    }
}

// =============================================================================
// Custom DTOs
// =============================================================================

// <<< CUSTOM DTOs
// Add custom DTOs specific to ContentHash here.
// This section will be preserved during regeneration.
// >>> END CUSTOM DTOs