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::PhoneBlacklist;
use crate::domain::entity::AuditMetadata;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreatePhoneBlacklistDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub number: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePhoneBlacklistDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub number: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchPhoneBlacklistDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub number: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = true))]
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
}
impl PatchPhoneBlacklistDto {
pub fn has_changes(&self) -> bool {
self.number.is_some() || self.active.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PhoneBlacklistResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub number: String,
#[cfg_attr(feature = "openapi", schema(example = true))]
pub active: bool,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PhoneBlacklistListResponseDto {
pub items: Vec<PhoneBlacklistResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PhoneBlacklistListResponseDto {
pub fn new(items: Vec<PhoneBlacklistResponseDto>, 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 PhoneBlacklistSummaryDto {
pub id: Uuid,
pub number: String,
pub active: bool,
pub created_at: Option<DateTime<Utc>>,
}
impl From<PhoneBlacklist> for PhoneBlacklistResponseDto {
fn from(entity: PhoneBlacklist) -> Self {
Self {
id: entity.id,
number: entity.number,
active: entity.active,
metadata: entity.metadata,
}
}
}
impl From<PhoneBlacklist> for PhoneBlacklistSummaryDto {
fn from(entity: PhoneBlacklist) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
number: entity.number,
active: entity.active,
created_at,
}
}
}
impl From<CreatePhoneBlacklistDto> for PhoneBlacklist {
fn from(dto: CreatePhoneBlacklistDto) -> Self {
Self {
id: Uuid::new_v4(),
number: dto.number,
active: dto.active,
metadata: AuditMetadata::default(),
}
}
}
impl From<&PhoneBlacklist> for PhoneBlacklistResponseDto {
fn from(entity: &PhoneBlacklist) -> Self {
Self {
id: entity.id.clone(),
number: entity.number.clone(),
active: entity.active.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreatePhoneBlacklistDto> for PhoneBlacklist {
fn from_create_dto(dto: CreatePhoneBlacklistDto) -> backbone_core::ServiceResult<Self> {
Ok(PhoneBlacklist::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdatePhoneBlacklistDto> for PhoneBlacklist {
fn apply_update(mut self, dto: UpdatePhoneBlacklistDto) -> backbone_core::ServiceResult<Self> {
self.number = dto.number;
self.active = dto.active;
Ok(self)
}
}