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;
#[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>,
}
#[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>,
}
#[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 {
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()
}
}
#[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,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ContentHashListResponseDto {
pub items: Vec<ContentHashResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl ContentHashListResponseDto {
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,
}
}
}
#[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>>,
}
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)
}
}