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::FileVersion;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::FileVersionStatus;
use crate::domain::entity::VersionType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateFileVersionDto {
#[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 = 42))]
#[serde(alias = "version_number")]
pub version_number: i32,
#[serde(alias = "version_type")]
pub version_type: VersionType,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "storage_key")]
pub storage_key: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "storage_backend")]
pub storage_backend: String,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "mime_type")]
pub mime_type: String,
#[cfg_attr(feature = "validation", validate(length(max = 32)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "checksum_md5")]
pub checksum_md5: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 64)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "checksum_sha256")]
pub checksum_sha256: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "created_by_id")]
pub created_by_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 500)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "change_summary")]
pub change_summary: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_current")]
pub is_current: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "restored_from_id")]
pub restored_from_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
pub status: FileVersionStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "size_bytes")]
pub size_bytes: i64,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateFileVersionDto {
#[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 = 42))]
#[serde(alias = "version_number")]
pub version_number: i32,
#[serde(alias = "version_type")]
pub version_type: VersionType,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "storage_key")]
pub storage_key: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "storage_backend")]
pub storage_backend: String,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "mime_type")]
pub mime_type: String,
#[cfg_attr(feature = "validation", validate(length(max = 32)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "checksum_md5")]
pub checksum_md5: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 64)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "checksum_sha256")]
pub checksum_sha256: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "created_by_id")]
pub created_by_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 500)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "change_summary")]
pub change_summary: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_current")]
pub is_current: bool,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "restored_from_id")]
pub restored_from_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
pub status: FileVersionStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "size_bytes")]
pub size_bytes: i64,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchFileVersionDto {
#[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 = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "version_number")]
pub version_number: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "version_type")]
pub version_type: Option<VersionType>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "storage_key")]
pub storage_key: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "storage_backend")]
pub storage_backend: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "mime_type")]
pub mime_type: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 32)))]
#[serde(skip_serializing_if = "Option::is_none", alias = "checksum_md5")]
pub checksum_md5: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 64)))]
#[serde(skip_serializing_if = "Option::is_none", alias = "checksum_sha256")]
pub checksum_sha256: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "created_by_id")]
pub created_by_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 500)))]
#[serde(skip_serializing_if = "Option::is_none", alias = "change_summary")]
pub change_summary: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_current")]
pub is_current: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", alias = "restored_from_id")]
pub restored_from_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<FileVersionStatus>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "size_bytes")]
pub size_bytes: Option<i64>,
}
impl PatchFileVersionDto {
pub fn has_changes(&self) -> bool {
self.file_id.is_some() || self.version_number.is_some() || self.version_type.is_some() || self.storage_key.is_some() || self.storage_backend.is_some() || self.name.is_some() || self.mime_type.is_some() || self.checksum_md5.is_some() || self.checksum_sha256.is_some() || self.created_by_id.is_some() || self.change_summary.is_some() || self.is_current.is_some() || self.restored_from_id.is_some() || self.expires_at.is_some() || self.status.is_some() || self.size_bytes.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileVersionResponseDto {
#[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 = 42))]
pub version_number: i32,
pub version_type: VersionType,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub storage_key: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub storage_backend: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub mime_type: String,
pub checksum_md5: Option<String>,
pub checksum_sha256: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub created_by_id: Uuid,
pub change_summary: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_current: bool,
pub restored_from_id: Option<Uuid>,
pub expires_at: Option<DateTime<Utc>>,
pub status: FileVersionStatus,
pub deleted_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub size_bytes: i64,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileVersionListResponseDto {
pub items: Vec<FileVersionResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl FileVersionListResponseDto {
pub fn new(items: Vec<FileVersionResponseDto>, 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,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileVersionSummaryDto {
pub id: Uuid,
pub file_id: Uuid,
pub version_number: i32,
pub version_type: VersionType,
pub created_at: Option<DateTime<Utc>>,
}
impl From<FileVersion> for FileVersionResponseDto {
fn from(entity: FileVersion) -> Self {
Self {
id: entity.id,
file_id: entity.file_id,
version_number: entity.version_number,
version_type: entity.version_type,
storage_key: entity.storage_key,
storage_backend: entity.storage_backend,
name: entity.name,
mime_type: entity.mime_type,
checksum_md5: entity.checksum_md5,
checksum_sha256: entity.checksum_sha256,
created_by_id: entity.created_by_id,
change_summary: entity.change_summary,
is_current: entity.is_current,
restored_from_id: entity.restored_from_id,
expires_at: entity.expires_at,
status: entity.status,
deleted_at: entity.deleted_at,
size_bytes: entity.size_bytes,
metadata: entity.metadata,
}
}
}
impl From<FileVersion> for FileVersionSummaryDto {
fn from(entity: FileVersion) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
file_id: entity.file_id,
version_number: entity.version_number,
version_type: entity.version_type,
created_at,
}
}
}
impl From<CreateFileVersionDto> for FileVersion {
fn from(dto: CreateFileVersionDto) -> Self {
Self {
id: Uuid::new_v4(),
file_id: dto.file_id,
version_number: dto.version_number,
version_type: dto.version_type,
storage_key: dto.storage_key,
storage_backend: dto.storage_backend,
name: dto.name,
mime_type: dto.mime_type,
checksum_md5: dto.checksum_md5,
checksum_sha256: dto.checksum_sha256,
created_by_id: dto.created_by_id,
change_summary: dto.change_summary,
is_current: dto.is_current,
restored_from_id: dto.restored_from_id,
expires_at: dto.expires_at,
status: dto.status,
deleted_at: None,
size_bytes: dto.size_bytes,
metadata: AuditMetadata::default(),
}
}
}
impl From<&FileVersion> for FileVersionResponseDto {
fn from(entity: &FileVersion) -> Self {
Self {
id: entity.id.clone(),
file_id: entity.file_id.clone(),
version_number: entity.version_number.clone(),
version_type: entity.version_type.clone(),
storage_key: entity.storage_key.clone(),
storage_backend: entity.storage_backend.clone(),
name: entity.name.clone(),
mime_type: entity.mime_type.clone(),
checksum_md5: entity.checksum_md5.clone(),
checksum_sha256: entity.checksum_sha256.clone(),
created_by_id: entity.created_by_id.clone(),
change_summary: entity.change_summary.clone(),
is_current: entity.is_current.clone(),
restored_from_id: entity.restored_from_id.clone(),
expires_at: entity.expires_at.clone(),
status: entity.status.clone(),
deleted_at: entity.deleted_at.clone(),
size_bytes: entity.size_bytes.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateFileVersionDto> for FileVersion {
fn from_create_dto(dto: CreateFileVersionDto) -> backbone_core::ServiceResult<Self> {
Ok(FileVersion::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateFileVersionDto> for FileVersion {
fn apply_update(mut self, dto: UpdateFileVersionDto) -> backbone_core::ServiceResult<Self> {
self.file_id = dto.file_id;
self.version_number = dto.version_number;
self.version_type = dto.version_type;
self.storage_key = dto.storage_key;
self.storage_backend = dto.storage_backend;
self.name = dto.name;
self.mime_type = dto.mime_type;
self.checksum_md5 = dto.checksum_md5;
self.checksum_sha256 = dto.checksum_sha256;
self.created_by_id = dto.created_by_id;
self.change_summary = dto.change_summary;
self.is_current = dto.is_current;
self.restored_from_id = dto.restored_from_id;
self.expires_at = dto.expires_at;
self.status = dto.status;
self.size_bytes = dto.size_bytes;
Ok(self)
}
}