backbone-bucket 0.4.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! FileLock 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::FileLock;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::LockStatus;

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

/// Request DTO for creating a new FileLock
///
/// 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 CreateFileLockDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "file_id")]
    pub file_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "user_id")]
    pub user_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "locked_at")]
    pub locked_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "expires_at")]
    pub expires_at: DateTime<Utc>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "refreshed_at")]
    pub refreshed_at: Option<DateTime<Utc>>,
    pub status: LockStatus,
}

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

/// Request DTO for full update of a FileLock
///
/// 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 UpdateFileLockDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "file_id")]
    pub file_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "user_id")]
    pub user_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "locked_at")]
    pub locked_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(alias = "expires_at")]
    pub expires_at: DateTime<Utc>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "refreshed_at")]
    pub refreshed_at: Option<DateTime<Utc>>,
    pub status: LockStatus,
}

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

/// Request DTO for partial update of a FileLock
///
/// 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 PatchFileLockDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "file_id")]
    pub file_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "user_id")]
    pub user_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "locked_at")]
    pub locked_at: Option<DateTime<Utc>>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "expires_at")]
    pub expires_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "refreshed_at")]
    pub refreshed_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<LockStatus>,
}

impl PatchFileLockDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.file_id.is_some() || self.user_id.is_some() || self.locked_at.is_some() || self.expires_at.is_some() || self.refreshed_at.is_some() || self.status.is_some()
    }
}

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

/// Response DTO for FileLock 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 FileLockResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub file_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub user_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub locked_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub expires_at: DateTime<Utc>,
    pub refreshed_at: Option<DateTime<Utc>>,
    pub status: LockStatus,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for FileLock 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 FileLockListResponseDto {
    /// List of items
    pub items: Vec<FileLockResponseDto>,
    /// 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 FileLockListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<FileLockResponseDto>, 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 FileLockSummaryDto {
    pub id: Uuid,
    pub file_id: Uuid,
    pub user_id: Uuid,
    pub locked_at: DateTime<Utc>,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<FileLock> for FileLockResponseDto {
    fn from(entity: FileLock) -> Self {
        Self {
            id: entity.id,
            file_id: entity.file_id,
            user_id: entity.user_id,
            locked_at: entity.locked_at,
            expires_at: entity.expires_at,
            refreshed_at: entity.refreshed_at,
            status: entity.status,
            metadata: entity.metadata,
        }
    }
}

impl From<FileLock> for FileLockSummaryDto {
    fn from(entity: FileLock) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            file_id: entity.file_id,
            user_id: entity.user_id,
            locked_at: entity.locked_at,
            created_at,
        }
    }
}

impl From<CreateFileLockDto> for FileLock {
    fn from(dto: CreateFileLockDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            file_id: dto.file_id,
            user_id: dto.user_id,
            locked_at: dto.locked_at,
            expires_at: dto.expires_at,
            refreshed_at: dto.refreshed_at,
            status: dto.status,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&FileLock> for FileLockResponseDto {
    fn from(entity: &FileLock) -> Self {
        Self {
            id: entity.id.clone(),
            file_id: entity.file_id.clone(),
            user_id: entity.user_id.clone(),
            locked_at: entity.locked_at.clone(),
            expires_at: entity.expires_at.clone(),
            refreshed_at: entity.refreshed_at.clone(),
            status: entity.status.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateFileLockDto> for FileLock {
    fn apply_update(mut self, dto: UpdateFileLockDto) -> backbone_core::ServiceResult<Self> {
        self.file_id = dto.file_id;
        self.user_id = dto.user_id;
        self.locked_at = dto.locked_at;
        self.expires_at = dto.expires_at;
        self.refreshed_at = dto.refreshed_at;
        self.status = dto.status;
        Ok(self)
    }
}

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

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