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::FileShare;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::SharePermission;
use crate::domain::entity::ShareStatus;
use crate::domain::entity::ShareType;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateFileShareDto {
#[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 = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "owner_id")]
pub owner_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(min = 32), length(max = 64)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub token: String,
#[serde(alias = "share_type")]
pub share_type: ShareType,
pub permission: SharePermission,
#[serde(alias = "shared_with")]
pub shared_with: Vec<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "password_hash")]
pub password_hash: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_downloads")]
pub max_downloads: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "download_count")]
pub download_count: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
pub status: ShareStatus,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "revoked_at")]
pub revoked_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "revoked_by")]
pub revoked_by: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateFileShareDto {
#[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 = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "owner_id")]
pub owner_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(min = 32), length(max = 64)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub token: String,
#[serde(alias = "share_type")]
pub share_type: ShareType,
pub permission: SharePermission,
#[serde(alias = "shared_with")]
pub shared_with: Vec<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "password_hash")]
pub password_hash: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_downloads")]
pub max_downloads: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "download_count")]
pub download_count: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
pub status: ShareStatus,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "revoked_at")]
pub revoked_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "revoked_by")]
pub revoked_by: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: 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 PatchFileShareDto {
#[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 = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "owner_id")]
pub owner_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(min = 32), length(max = 64)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "share_type")]
pub share_type: Option<ShareType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub permission: Option<SharePermission>,
#[serde(skip_serializing_if = "Option::is_none", alias = "shared_with")]
pub shared_with: Option<Vec<Uuid>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "password_hash")]
pub password_hash: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "max_downloads")]
pub max_downloads: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "download_count")]
pub download_count: Option<i32>,
#[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<ShareStatus>,
#[serde(skip_serializing_if = "Option::is_none", alias = "revoked_at")]
pub revoked_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "revoked_by")]
pub revoked_by: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
impl PatchFileShareDto {
pub fn has_changes(&self) -> bool {
self.file_id.is_some() || self.owner_id.is_some() || self.token.is_some() || self.share_type.is_some() || self.permission.is_some() || self.shared_with.is_some() || self.password_hash.is_some() || self.max_downloads.is_some() || self.download_count.is_some() || self.expires_at.is_some() || self.status.is_some() || self.revoked_at.is_some() || self.revoked_by.is_some() || self.message.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileShareResponseDto {
#[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 = "550e8400-e29b-41d4-a716-446655440000"))]
pub owner_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub token: String,
pub share_type: ShareType,
pub permission: SharePermission,
pub shared_with: Vec<Uuid>,
pub password_hash: Option<String>,
pub max_downloads: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub download_count: i32,
pub expires_at: Option<DateTime<Utc>>,
pub status: ShareStatus,
pub revoked_at: Option<DateTime<Utc>>,
pub revoked_by: Option<Uuid>,
pub message: Option<String>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileShareListResponseDto {
pub items: Vec<FileShareResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl FileShareListResponseDto {
pub fn new(items: Vec<FileShareResponseDto>, 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 FileShareSummaryDto {
pub id: Uuid,
pub file_id: Uuid,
pub owner_id: Uuid,
pub token: String,
pub created_at: Option<DateTime<Utc>>,
}
impl From<FileShare> for FileShareResponseDto {
fn from(entity: FileShare) -> Self {
Self {
id: entity.id,
file_id: entity.file_id,
owner_id: entity.owner_id,
token: entity.token,
share_type: entity.share_type,
permission: entity.permission,
shared_with: entity.shared_with,
password_hash: entity.password_hash,
max_downloads: entity.max_downloads,
download_count: entity.download_count,
expires_at: entity.expires_at,
status: entity.status,
revoked_at: entity.revoked_at,
revoked_by: entity.revoked_by,
message: entity.message,
metadata: entity.metadata,
}
}
}
impl From<FileShare> for FileShareSummaryDto {
fn from(entity: FileShare) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
file_id: entity.file_id,
owner_id: entity.owner_id,
token: entity.token,
created_at,
}
}
}
impl From<CreateFileShareDto> for FileShare {
fn from(dto: CreateFileShareDto) -> Self {
Self {
id: Uuid::new_v4(),
file_id: dto.file_id,
owner_id: dto.owner_id,
token: dto.token,
share_type: dto.share_type,
permission: dto.permission,
shared_with: dto.shared_with,
password_hash: dto.password_hash,
max_downloads: dto.max_downloads,
download_count: dto.download_count,
expires_at: dto.expires_at,
status: dto.status,
revoked_at: dto.revoked_at,
revoked_by: dto.revoked_by,
message: dto.message,
metadata: AuditMetadata::default(),
}
}
}
impl From<&FileShare> for FileShareResponseDto {
fn from(entity: &FileShare) -> Self {
Self {
id: entity.id.clone(),
file_id: entity.file_id.clone(),
owner_id: entity.owner_id.clone(),
token: entity.token.clone(),
share_type: entity.share_type.clone(),
permission: entity.permission.clone(),
shared_with: entity.shared_with.clone(),
password_hash: entity.password_hash.clone(),
max_downloads: entity.max_downloads.clone(),
download_count: entity.download_count.clone(),
expires_at: entity.expires_at.clone(),
status: entity.status.clone(),
revoked_at: entity.revoked_at.clone(),
revoked_by: entity.revoked_by.clone(),
message: entity.message.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateFileShareDto> for FileShare {
fn from_create_dto(dto: CreateFileShareDto) -> backbone_core::ServiceResult<Self> {
Ok(FileShare::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateFileShareDto> for FileShare {
fn apply_update(mut self, dto: UpdateFileShareDto) -> backbone_core::ServiceResult<Self> {
self.file_id = dto.file_id;
self.owner_id = dto.owner_id;
self.token = dto.token;
self.share_type = dto.share_type;
self.permission = dto.permission;
self.shared_with = dto.shared_with;
self.password_hash = dto.password_hash;
self.max_downloads = dto.max_downloads;
self.download_count = dto.download_count;
self.expires_at = dto.expires_at;
self.status = dto.status;
self.revoked_at = dto.revoked_at;
self.revoked_by = dto.revoked_by;
self.message = dto.message;
Ok(self)
}
}