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::Thumbnail;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::ThumbnailSize;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateThumbnailDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "file_id")]
pub file_id: Uuid,
pub size: ThumbnailSize,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub width: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub height: i32,
#[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 = "openapi", schema(example = "example"))]
#[serde(alias = "mime_type")]
pub mime_type: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub format: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub quality: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "size_bytes")]
pub size_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "generated_at")]
pub generated_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "generation_time_ms")]
pub generation_time_ms: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "source_version")]
pub source_version: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "cdn_url")]
pub cdn_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "cache_expires_at")]
pub cache_expires_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_stale")]
pub is_stale: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateThumbnailDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "file_id")]
pub file_id: Uuid,
pub size: ThumbnailSize,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub width: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub height: i32,
#[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 = "openapi", schema(example = "example"))]
#[serde(alias = "mime_type")]
pub mime_type: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub format: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub quality: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "size_bytes")]
pub size_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "generated_at")]
pub generated_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "generation_time_ms")]
pub generation_time_ms: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "source_version")]
pub source_version: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "cdn_url")]
pub cdn_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "cache_expires_at")]
pub cache_expires_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "is_stale")]
pub is_stale: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchThumbnailDto {
#[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>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<ThumbnailSize>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none")]
pub width: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none")]
pub height: Option<i32>,
#[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 = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "mime_type")]
pub mime_type: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none")]
pub quality: Option<i32>,
#[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 = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "generated_at")]
pub generated_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "generation_time_ms")]
pub generation_time_ms: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "source_version")]
pub source_version: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "cdn_url")]
pub cdn_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "cache_expires_at")]
pub cache_expires_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "is_stale")]
pub is_stale: Option<bool>,
}
impl PatchThumbnailDto {
pub fn has_changes(&self) -> bool {
self.file_id.is_some() || self.size.is_some() || self.width.is_some() || self.height.is_some() || self.storage_key.is_some() || self.storage_backend.is_some() || self.mime_type.is_some() || self.format.is_some() || self.quality.is_some() || self.size_bytes.is_some() || self.generated_at.is_some() || self.generation_time_ms.is_some() || self.source_version.is_some() || self.cdn_url.is_some() || self.cache_expires_at.is_some() || self.is_stale.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ThumbnailResponseDto {
#[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,
pub size: ThumbnailSize,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub width: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub height: i32,
#[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 mime_type: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub format: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub quality: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub size_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub generated_at: DateTime<Utc>,
pub generation_time_ms: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub source_version: i32,
pub cdn_url: Option<String>,
pub cache_expires_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub is_stale: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ThumbnailListResponseDto {
pub items: Vec<ThumbnailResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl ThumbnailListResponseDto {
pub fn new(items: Vec<ThumbnailResponseDto>, 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 ThumbnailSummaryDto {
pub id: Uuid,
pub file_id: Uuid,
pub size: ThumbnailSize,
pub width: i32,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Thumbnail> for ThumbnailResponseDto {
fn from(entity: Thumbnail) -> Self {
Self {
id: entity.id,
file_id: entity.file_id,
size: entity.size,
width: entity.width,
height: entity.height,
storage_key: entity.storage_key,
storage_backend: entity.storage_backend,
mime_type: entity.mime_type,
format: entity.format,
quality: entity.quality,
size_bytes: entity.size_bytes,
generated_at: entity.generated_at,
generation_time_ms: entity.generation_time_ms,
source_version: entity.source_version,
cdn_url: entity.cdn_url,
cache_expires_at: entity.cache_expires_at,
is_stale: entity.is_stale,
metadata: entity.metadata,
}
}
}
impl From<Thumbnail> for ThumbnailSummaryDto {
fn from(entity: Thumbnail) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
file_id: entity.file_id,
size: entity.size,
width: entity.width,
created_at,
}
}
}
impl From<CreateThumbnailDto> for Thumbnail {
fn from(dto: CreateThumbnailDto) -> Self {
Self {
id: Uuid::new_v4(),
file_id: dto.file_id,
size: dto.size,
width: dto.width,
height: dto.height,
storage_key: dto.storage_key,
storage_backend: dto.storage_backend,
mime_type: dto.mime_type,
format: dto.format,
quality: dto.quality,
size_bytes: dto.size_bytes,
generated_at: dto.generated_at,
generation_time_ms: dto.generation_time_ms,
source_version: dto.source_version,
cdn_url: dto.cdn_url,
cache_expires_at: dto.cache_expires_at,
is_stale: dto.is_stale,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Thumbnail> for ThumbnailResponseDto {
fn from(entity: &Thumbnail) -> Self {
Self {
id: entity.id.clone(),
file_id: entity.file_id.clone(),
size: entity.size.clone(),
width: entity.width.clone(),
height: entity.height.clone(),
storage_key: entity.storage_key.clone(),
storage_backend: entity.storage_backend.clone(),
mime_type: entity.mime_type.clone(),
format: entity.format.clone(),
quality: entity.quality.clone(),
size_bytes: entity.size_bytes.clone(),
generated_at: entity.generated_at.clone(),
generation_time_ms: entity.generation_time_ms.clone(),
source_version: entity.source_version.clone(),
cdn_url: entity.cdn_url.clone(),
cache_expires_at: entity.cache_expires_at.clone(),
is_stale: entity.is_stale.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateThumbnailDto> for Thumbnail {
fn from_create_dto(dto: CreateThumbnailDto) -> backbone_core::ServiceResult<Self> {
Ok(Thumbnail::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateThumbnailDto> for Thumbnail {
fn apply_update(mut self, dto: UpdateThumbnailDto) -> backbone_core::ServiceResult<Self> {
self.file_id = dto.file_id;
self.size = dto.size;
self.width = dto.width;
self.height = dto.height;
self.storage_key = dto.storage_key;
self.storage_backend = dto.storage_backend;
self.mime_type = dto.mime_type;
self.format = dto.format;
self.quality = dto.quality;
self.size_bytes = dto.size_bytes;
self.generated_at = dto.generated_at;
self.generation_time_ms = dto.generation_time_ms;
self.source_version = dto.source_version;
self.cdn_url = dto.cdn_url;
self.cache_expires_at = dto.cache_expires_at;
self.is_stale = dto.is_stale;
Ok(self)
}
}