backbone-catalog 0.6.23

Canonical product/service identity: Item, Item Group, UOM (Indonesia-first)
Documentation
//! ItemVariant 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};
use rust_decimal::Decimal;

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

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

use crate::domain::entity::ItemVariant;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::CatalogStatus;

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

/// Request DTO for creating a new ItemVariant
///
/// 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 CreateItemVariantDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "item_id")]
    pub item_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub sku: String,
    #[cfg_attr(feature = "validation", validate(length(max = 200)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "variant_label")]
    pub variant_label: String,
    pub options: serde_json::Value,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub barcode: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "is_default")]
    pub is_default: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "weight_per_unit")]
    pub weight_per_unit: Option<Decimal>,
    pub status: CatalogStatus,
}

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

/// Request DTO for full update of a ItemVariant
///
/// 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 UpdateItemVariantDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(alias = "item_id")]
    pub item_id: Uuid,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub sku: String,
    #[cfg_attr(feature = "validation", validate(length(max = 200)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(alias = "variant_label")]
    pub variant_label: String,
    pub options: serde_json::Value,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub barcode: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "is_default")]
    pub is_default: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "weight_per_unit")]
    pub weight_per_unit: Option<Decimal>,
    pub status: CatalogStatus,
}

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

/// Request DTO for partial update of a ItemVariant
///
/// 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 PatchItemVariantDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "item_id")]
    pub item_id: Option<Uuid>,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sku: Option<String>,
    #[cfg_attr(feature = "validation", validate(length(max = 200)))]
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "variant_label")]
    pub variant_label: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<serde_json::Value>,
    #[cfg_attr(feature = "validation", validate(length(max = 60)))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub barcode: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "is_default")]
    pub is_default: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "weight_per_unit")]
    pub weight_per_unit: Option<Decimal>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<CatalogStatus>,
}

impl PatchItemVariantDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.item_id.is_some() || self.sku.is_some() || self.variant_label.is_some() || self.options.is_some() || self.barcode.is_some() || self.is_default.is_some() || self.weight_per_unit.is_some() || self.status.is_some()
    }
}

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

/// Response DTO for ItemVariant 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 ItemVariantResponseDto {
    #[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 item_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub sku: String,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub variant_label: String,
    pub options: serde_json::Value,
    pub barcode: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub is_default: bool,
    pub weight_per_unit: Option<Decimal>,
    pub status: CatalogStatus,
    pub metadata: AuditMetadata,
}

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

/// Paginated list response for ItemVariant 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 ItemVariantListResponseDto {
    /// List of items
    pub items: Vec<ItemVariantResponseDto>,
    /// 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 ItemVariantListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<ItemVariantResponseDto>, 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 ItemVariantSummaryDto {
    pub id: Uuid,
    pub item_id: Uuid,
    pub sku: String,
    pub variant_label: String,
    pub created_at: Option<DateTime<Utc>>,
}

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

impl From<ItemVariant> for ItemVariantResponseDto {
    fn from(entity: ItemVariant) -> Self {
        Self {
            id: entity.id,
            item_id: entity.item_id,
            sku: entity.sku,
            variant_label: entity.variant_label,
            options: entity.options,
            barcode: entity.barcode,
            is_default: entity.is_default,
            weight_per_unit: entity.weight_per_unit,
            status: entity.status,
            metadata: entity.metadata,
        }
    }
}

impl From<ItemVariant> for ItemVariantSummaryDto {
    fn from(entity: ItemVariant) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            item_id: entity.item_id,
            sku: entity.sku,
            variant_label: entity.variant_label,
            created_at,
        }
    }
}

impl From<CreateItemVariantDto> for ItemVariant {
    fn from(dto: CreateItemVariantDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            item_id: dto.item_id,
            sku: dto.sku,
            variant_label: dto.variant_label,
            options: dto.options,
            barcode: dto.barcode,
            is_default: dto.is_default,
            weight_per_unit: dto.weight_per_unit,
            status: dto.status,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&ItemVariant> for ItemVariantResponseDto {
    fn from(entity: &ItemVariant) -> Self {
        Self {
            id: entity.id.clone(),
            item_id: entity.item_id.clone(),
            sku: entity.sku.clone(),
            variant_label: entity.variant_label.clone(),
            options: entity.options.clone(),
            barcode: entity.barcode.clone(),
            is_default: entity.is_default.clone(),
            weight_per_unit: entity.weight_per_unit.clone(),
            status: entity.status.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

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

impl backbone_core::ApplyUpdateDto<UpdateItemVariantDto> for ItemVariant {
    fn apply_update(mut self, dto: UpdateItemVariantDto) -> backbone_core::ServiceResult<Self> {
        self.item_id = dto.item_id;
        self.sku = dto.sku;
        self.variant_label = dto.variant_label;
        self.options = dto.options;
        self.barcode = dto.barcode;
        self.is_default = dto.is_default;
        self.weight_per_unit = dto.weight_per_unit;
        self.status = dto.status;
        Ok(self)
    }
}

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

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