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::UserQuota;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::QuotaStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateUserQuotaDto {
#[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 = 42))]
#[serde(alias = "limit_bytes")]
pub limit_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "used_bytes")]
pub used_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "file_count")]
pub file_count: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_file_size")]
pub max_file_size: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_file_count")]
pub max_file_count: Option<i32>,
#[cfg_attr(feature = "validation", validate(length(max = 50)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub tier: String,
#[serde(alias = "quota_status")]
pub quota_status: QuotaStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "warning_threshold_percent")]
pub warning_threshold_percent: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_warning_sent_at")]
pub last_warning_sent_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "peak_usage_bytes")]
pub peak_usage_bytes: i64,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "peak_usage_at")]
pub peak_usage_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateUserQuotaDto {
#[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 = 42))]
#[serde(alias = "limit_bytes")]
pub limit_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "used_bytes")]
pub used_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "file_count")]
pub file_count: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_file_size")]
pub max_file_size: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "max_file_count")]
pub max_file_count: Option<i32>,
#[cfg_attr(feature = "validation", validate(length(max = 50)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub tier: String,
#[serde(alias = "quota_status")]
pub quota_status: QuotaStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "warning_threshold_percent")]
pub warning_threshold_percent: i32,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "last_warning_sent_at")]
pub last_warning_sent_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(alias = "peak_usage_bytes")]
pub peak_usage_bytes: i64,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "peak_usage_at")]
pub peak_usage_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchUserQuotaDto {
#[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 = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "limit_bytes")]
pub limit_bytes: Option<i64>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "used_bytes")]
pub used_bytes: Option<i64>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "file_count")]
pub file_count: Option<i32>,
#[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 = "max_file_count")]
pub max_file_count: Option<i32>,
#[cfg_attr(feature = "validation", validate(length(max = 50)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub tier: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "quota_status")]
pub quota_status: Option<QuotaStatus>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "warning_threshold_percent")]
pub warning_threshold_percent: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none", alias = "last_warning_sent_at")]
pub last_warning_sent_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
#[serde(skip_serializing_if = "Option::is_none", alias = "peak_usage_bytes")]
pub peak_usage_bytes: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none", alias = "peak_usage_at")]
pub peak_usage_at: Option<DateTime<Utc>>,
}
impl PatchUserQuotaDto {
pub fn has_changes(&self) -> bool {
self.user_id.is_some() || self.limit_bytes.is_some() || self.used_bytes.is_some() || self.file_count.is_some() || self.max_file_size.is_some() || self.max_file_count.is_some() || self.tier.is_some() || self.quota_status.is_some() || self.warning_threshold_percent.is_some() || self.last_warning_sent_at.is_some() || self.peak_usage_bytes.is_some() || self.peak_usage_at.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UserQuotaResponseDto {
#[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 user_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub limit_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub used_bytes: i64,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub file_count: i32,
pub max_file_size: Option<i64>,
pub max_file_count: Option<i32>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub tier: String,
pub quota_status: QuotaStatus,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub warning_threshold_percent: i32,
pub last_warning_sent_at: Option<DateTime<Utc>>,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub peak_usage_bytes: i64,
pub peak_usage_at: Option<DateTime<Utc>>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct UserQuotaListResponseDto {
pub items: Vec<UserQuotaResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl UserQuotaListResponseDto {
pub fn new(items: Vec<UserQuotaResponseDto>, 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 UserQuotaSummaryDto {
pub id: Uuid,
pub user_id: Uuid,
pub limit_bytes: i64,
pub used_bytes: i64,
pub created_at: Option<DateTime<Utc>>,
}
impl From<UserQuota> for UserQuotaResponseDto {
fn from(entity: UserQuota) -> Self {
Self {
id: entity.id,
user_id: entity.user_id,
limit_bytes: entity.limit_bytes,
used_bytes: entity.used_bytes,
file_count: entity.file_count,
max_file_size: entity.max_file_size,
max_file_count: entity.max_file_count,
tier: entity.tier,
quota_status: entity.quota_status,
warning_threshold_percent: entity.warning_threshold_percent,
last_warning_sent_at: entity.last_warning_sent_at,
peak_usage_bytes: entity.peak_usage_bytes,
peak_usage_at: entity.peak_usage_at,
metadata: entity.metadata,
}
}
}
impl From<UserQuota> for UserQuotaSummaryDto {
fn from(entity: UserQuota) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
user_id: entity.user_id,
limit_bytes: entity.limit_bytes,
used_bytes: entity.used_bytes,
created_at,
}
}
}
impl From<CreateUserQuotaDto> for UserQuota {
fn from(dto: CreateUserQuotaDto) -> Self {
Self {
id: Uuid::new_v4(),
user_id: dto.user_id,
limit_bytes: dto.limit_bytes,
used_bytes: dto.used_bytes,
file_count: dto.file_count,
max_file_size: dto.max_file_size,
max_file_count: dto.max_file_count,
tier: dto.tier,
quota_status: dto.quota_status,
warning_threshold_percent: dto.warning_threshold_percent,
last_warning_sent_at: dto.last_warning_sent_at,
peak_usage_bytes: dto.peak_usage_bytes,
peak_usage_at: dto.peak_usage_at,
metadata: AuditMetadata::default(),
}
}
}
impl From<&UserQuota> for UserQuotaResponseDto {
fn from(entity: &UserQuota) -> Self {
Self {
id: entity.id.clone(),
user_id: entity.user_id.clone(),
limit_bytes: entity.limit_bytes.clone(),
used_bytes: entity.used_bytes.clone(),
file_count: entity.file_count.clone(),
max_file_size: entity.max_file_size.clone(),
max_file_count: entity.max_file_count.clone(),
tier: entity.tier.clone(),
quota_status: entity.quota_status.clone(),
warning_threshold_percent: entity.warning_threshold_percent.clone(),
last_warning_sent_at: entity.last_warning_sent_at.clone(),
peak_usage_bytes: entity.peak_usage_bytes.clone(),
peak_usage_at: entity.peak_usage_at.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateUserQuotaDto> for UserQuota {
fn from_create_dto(dto: CreateUserQuotaDto) -> backbone_core::ServiceResult<Self> {
Ok(UserQuota::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateUserQuotaDto> for UserQuota {
fn apply_update(mut self, dto: UpdateUserQuotaDto) -> backbone_core::ServiceResult<Self> {
self.user_id = dto.user_id;
self.limit_bytes = dto.limit_bytes;
self.used_bytes = dto.used_bytes;
self.file_count = dto.file_count;
self.max_file_size = dto.max_file_size;
self.max_file_count = dto.max_file_count;
self.tier = dto.tier;
self.quota_status = dto.quota_status;
self.warning_threshold_percent = dto.warning_threshold_percent;
self.last_warning_sent_at = dto.last_warning_sent_at;
self.peak_usage_bytes = dto.peak_usage_bytes;
self.peak_usage_at = dto.peak_usage_at;
Ok(self)
}
}