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::FileLock;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::LockStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateFileLockDto {
#[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 = "user_id")]
pub user_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "locked_at")]
pub locked_at: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "expires_at")]
pub expires_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "refreshed_at")]
pub refreshed_at: Option<DateTime<Utc>>,
pub status: LockStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateFileLockDto {
#[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 = "user_id")]
pub user_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "locked_at")]
pub locked_at: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "expires_at")]
pub expires_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "refreshed_at")]
pub refreshed_at: Option<DateTime<Utc>>,
pub status: LockStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchFileLockDto {
#[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 = "user_id")]
pub user_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "locked_at")]
pub locked_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "expires_at")]
pub expires_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "refreshed_at")]
pub refreshed_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<LockStatus>,
}
impl PatchFileLockDto {
pub fn has_changes(&self) -> bool {
self.file_id.is_some() || self.user_id.is_some() || self.locked_at.is_some() || self.expires_at.is_some() || self.refreshed_at.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileLockResponseDto {
#[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 user_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub locked_at: DateTime<Utc>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub expires_at: DateTime<Utc>,
pub refreshed_at: Option<DateTime<Utc>>,
pub status: LockStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FileLockListResponseDto {
pub items: Vec<FileLockResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl FileLockListResponseDto {
pub fn new(items: Vec<FileLockResponseDto>, 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 FileLockSummaryDto {
pub id: Uuid,
pub file_id: Uuid,
pub user_id: Uuid,
pub locked_at: DateTime<Utc>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<FileLock> for FileLockResponseDto {
fn from(entity: FileLock) -> Self {
Self {
id: entity.id,
file_id: entity.file_id,
user_id: entity.user_id,
locked_at: entity.locked_at,
expires_at: entity.expires_at,
refreshed_at: entity.refreshed_at,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<FileLock> for FileLockSummaryDto {
fn from(entity: FileLock) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
file_id: entity.file_id,
user_id: entity.user_id,
locked_at: entity.locked_at,
created_at,
}
}
}
impl From<CreateFileLockDto> for FileLock {
fn from(dto: CreateFileLockDto) -> Self {
Self {
id: Uuid::new_v4(),
file_id: dto.file_id,
user_id: dto.user_id,
locked_at: dto.locked_at,
expires_at: dto.expires_at,
refreshed_at: dto.refreshed_at,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&FileLock> for FileLockResponseDto {
fn from(entity: &FileLock) -> Self {
Self {
id: entity.id.clone(),
file_id: entity.file_id.clone(),
user_id: entity.user_id.clone(),
locked_at: entity.locked_at.clone(),
expires_at: entity.expires_at.clone(),
refreshed_at: entity.refreshed_at.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateFileLockDto> for FileLock {
fn from_create_dto(dto: CreateFileLockDto) -> backbone_core::ServiceResult<Self> {
Ok(FileLock::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateFileLockDto> for FileLock {
fn apply_update(mut self, dto: UpdateFileLockDto) -> backbone_core::ServiceResult<Self> {
self.file_id = dto.file_id;
self.user_id = dto.user_id;
self.locked_at = dto.locked_at;
self.expires_at = dto.expires_at;
self.refreshed_at = dto.refreshed_at;
self.status = dto.status;
Ok(self)
}
}