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::MailMessageStar;
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 CreateMailMessageStarDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "partner_id")]
pub partner_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "message_id")]
pub message_id: Uuid,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateMailMessageStarDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "partner_id")]
pub partner_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "message_id")]
pub message_id: Uuid,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchMailMessageStarDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "partner_id")]
pub partner_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "message_id")]
pub message_id: Option<Uuid>,
}
impl PatchMailMessageStarDto {
pub fn has_changes(&self) -> bool {
self.partner_id.is_some() || self.message_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailMessageStarResponseDto {
#[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 partner_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub message_id: Uuid,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailMessageStarListResponseDto {
pub items: Vec<MailMessageStarResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl MailMessageStarListResponseDto {
pub fn new(items: Vec<MailMessageStarResponseDto>, 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 MailMessageStarSummaryDto {
pub id: Uuid,
pub partner_id: Uuid,
pub message_id: Uuid,
pub created_at: Option<DateTime<Utc>>,
}
impl From<MailMessageStar> for MailMessageStarResponseDto {
fn from(entity: MailMessageStar) -> Self {
Self {
id: entity.id,
partner_id: entity.partner_id,
message_id: entity.message_id,
metadata: entity.metadata,
}
}
}
impl From<MailMessageStar> for MailMessageStarSummaryDto {
fn from(entity: MailMessageStar) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
partner_id: entity.partner_id,
message_id: entity.message_id,
created_at,
}
}
}
impl From<CreateMailMessageStarDto> for MailMessageStar {
fn from(dto: CreateMailMessageStarDto) -> Self {
Self {
id: Uuid::new_v4(),
partner_id: dto.partner_id,
message_id: dto.message_id,
metadata: AuditMetadata::default(),
}
}
}
impl From<&MailMessageStar> for MailMessageStarResponseDto {
fn from(entity: &MailMessageStar) -> Self {
Self {
id: entity.id.clone(),
partner_id: entity.partner_id.clone(),
message_id: entity.message_id.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateMailMessageStarDto> for MailMessageStar {
fn from_create_dto(dto: CreateMailMessageStarDto) -> backbone_core::ServiceResult<Self> {
Ok(MailMessageStar::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateMailMessageStarDto> for MailMessageStar {
fn apply_update(mut self, dto: UpdateMailMessageStarDto) -> backbone_core::ServiceResult<Self> {
self.partner_id = dto.partner_id;
self.message_id = dto.message_id;
Ok(self)
}
}