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::Bucket;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::BucketStatus;
use crate::domain::entity::BucketType;
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 CreateBucketDto {
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub slug: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "owner_id")]
pub owner_id: Uuid,
#[serde(alias = "bucket_type")]
pub bucket_type: BucketType,
pub status: BucketStatus,
#[serde(alias = "storage_backend")]
pub storage_backend: StorageBackend,
#[cfg_attr(feature = "validation", validate(length(max = 1024)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "root_path")]
pub root_path: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "file_count")]
pub file_count: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "total_size_bytes")]
pub total_size_bytes: i64,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_file_size")]
pub max_file_size: Option<i64>,
#[serde(alias = "allowed_mime_types")]
pub allowed_mime_types: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "auto_delete_after_days")]
pub auto_delete_after_days: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "enable_cdn")]
pub enable_cdn: bool,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "enable_versioning")]
pub enable_versioning: bool,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "enable_deduplication")]
pub enable_deduplication: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateBucketDto {
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub slug: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "owner_id")]
pub owner_id: Uuid,
#[serde(alias = "bucket_type")]
pub bucket_type: BucketType,
pub status: BucketStatus,
#[serde(alias = "storage_backend")]
pub storage_backend: StorageBackend,
#[cfg_attr(feature = "validation", validate(length(max = 1024)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "root_path")]
pub root_path: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "file_count")]
pub file_count: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "total_size_bytes")]
pub total_size_bytes: i64,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_file_size")]
pub max_file_size: Option<i64>,
#[serde(alias = "allowed_mime_types")]
pub allowed_mime_types: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "auto_delete_after_days")]
pub auto_delete_after_days: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "enable_cdn")]
pub enable_cdn: bool,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "enable_versioning")]
pub enable_versioning: bool,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(alias = "enable_deduplication")]
pub enable_deduplication: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchBucketDto {
#[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 = "validation", validate(length(max = 255)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub slug: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "owner_id")]
pub owner_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "bucket_type")]
pub bucket_type: Option<BucketType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<BucketStatus>,
#[serde(skip_serializing_if = "Option::is_none", alias = "storage_backend")]
pub storage_backend: Option<StorageBackend>,
#[cfg_attr(feature = "validation", validate(length(max = 1024)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "root_path")]
pub root_path: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "file_count")]
pub file_count: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "total_size_bytes")]
pub total_size_bytes: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none", alias = "max_file_size")]
pub max_file_size: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none", alias = "allowed_mime_types")]
pub allowed_mime_types: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "auto_delete_after_days")]
pub auto_delete_after_days: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "enable_cdn")]
pub enable_cdn: Option<bool>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "enable_versioning")]
pub enable_versioning: Option<bool>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none", alias = "enable_deduplication")]
pub enable_deduplication: Option<bool>,
}
impl PatchBucketDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.slug.is_some() || self.description.is_some() || self.owner_id.is_some() || self.bucket_type.is_some() || self.status.is_some() || self.storage_backend.is_some() || self.root_path.is_some() || self.file_count.is_some() || self.total_size_bytes.is_some() || self.max_file_size.is_some() || self.allowed_mime_types.is_some() || self.auto_delete_after_days.is_some() || self.enable_cdn.is_some() || self.enable_versioning.is_some() || self.enable_deduplication.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BucketResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub slug: String,
pub description: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub owner_id: Uuid,
pub bucket_type: BucketType,
pub status: BucketStatus,
pub storage_backend: StorageBackend,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub root_path: String,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub file_count: i32,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub total_size_bytes: i64,
pub max_file_size: Option<i64>,
pub allowed_mime_types: Vec<String>,
pub auto_delete_after_days: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub enable_cdn: bool,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub enable_versioning: bool,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub enable_deduplication: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BucketListResponseDto {
pub items: Vec<BucketResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl BucketListResponseDto {
pub fn new(items: Vec<BucketResponseDto>, 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 BucketSummaryDto {
pub id: Uuid,
pub name: String,
pub slug: String,
pub description: Option<String>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Bucket> for BucketResponseDto {
fn from(entity: Bucket) -> Self {
Self {
id: entity.id,
name: entity.name,
slug: entity.slug,
description: entity.description,
owner_id: entity.owner_id,
bucket_type: entity.bucket_type,
status: entity.status,
storage_backend: entity.storage_backend,
root_path: entity.root_path,
file_count: entity.file_count,
total_size_bytes: entity.total_size_bytes,
max_file_size: entity.max_file_size,
allowed_mime_types: entity.allowed_mime_types,
auto_delete_after_days: entity.auto_delete_after_days,
enable_cdn: entity.enable_cdn,
enable_versioning: entity.enable_versioning,
enable_deduplication: entity.enable_deduplication,
metadata: entity.metadata,
}
}
}
impl From<Bucket> for BucketSummaryDto {
fn from(entity: Bucket) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
slug: entity.slug,
description: entity.description,
created_at,
}
}
}
impl From<CreateBucketDto> for Bucket {
fn from(dto: CreateBucketDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
slug: dto.slug,
description: dto.description,
owner_id: dto.owner_id,
bucket_type: dto.bucket_type,
status: dto.status,
storage_backend: dto.storage_backend,
root_path: dto.root_path,
file_count: dto.file_count,
total_size_bytes: dto.total_size_bytes,
max_file_size: dto.max_file_size,
allowed_mime_types: dto.allowed_mime_types,
auto_delete_after_days: dto.auto_delete_after_days,
enable_cdn: dto.enable_cdn,
enable_versioning: dto.enable_versioning,
enable_deduplication: dto.enable_deduplication,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Bucket> for BucketResponseDto {
fn from(entity: &Bucket) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
slug: entity.slug.clone(),
description: entity.description.clone(),
owner_id: entity.owner_id.clone(),
bucket_type: entity.bucket_type.clone(),
status: entity.status.clone(),
storage_backend: entity.storage_backend.clone(),
root_path: entity.root_path.clone(),
file_count: entity.file_count.clone(),
total_size_bytes: entity.total_size_bytes.clone(),
max_file_size: entity.max_file_size.clone(),
allowed_mime_types: entity.allowed_mime_types.clone(),
auto_delete_after_days: entity.auto_delete_after_days.clone(),
enable_cdn: entity.enable_cdn.clone(),
enable_versioning: entity.enable_versioning.clone(),
enable_deduplication: entity.enable_deduplication.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateBucketDto> for Bucket {
fn from_create_dto(dto: CreateBucketDto) -> backbone_core::ServiceResult<Self> {
Ok(Bucket::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateBucketDto> for Bucket {
fn apply_update(mut self, dto: UpdateBucketDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.slug = dto.slug;
self.description = dto.description;
self.owner_id = dto.owner_id;
self.bucket_type = dto.bucket_type;
self.status = dto.status;
self.storage_backend = dto.storage_backend;
self.root_path = dto.root_path;
self.file_count = dto.file_count;
self.total_size_bytes = dto.total_size_bytes;
self.max_file_size = dto.max_file_size;
self.allowed_mime_types = dto.allowed_mime_types;
self.auto_delete_after_days = dto.auto_delete_after_days;
self.enable_cdn = dto.enable_cdn;
self.enable_versioning = dto.enable_versioning;
self.enable_deduplication = dto.enable_deduplication;
Ok(self)
}
}