use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::{AuditMetadata, Example, ExampleStatus};
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateExampleDto {
#[cfg_attr(feature = "validation", validate(length(max = 200)))]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default)]
pub status: ExampleStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateExampleDto {
#[cfg_attr(feature = "validation", validate(length(max = 200)))]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub status: ExampleStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchExampleDto {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<ExampleStatus>,
}
impl PatchExampleDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.description.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExampleResponseDto {
pub id: Uuid,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub status: ExampleStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExampleListResponseDto {
pub items: Vec<ExampleResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl ExampleListResponseDto {
pub fn new(items: Vec<ExampleResponseDto>, 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)]
#[serde(rename_all = "camelCase")]
pub struct ExampleSummaryDto {
pub id: Uuid,
pub name: String,
pub status: ExampleStatus,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Example> for ExampleResponseDto {
fn from(entity: Example) -> Self {
Self {
id: entity.id,
name: entity.name,
description: entity.description,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<&Example> for ExampleResponseDto {
fn from(entity: &Example) -> Self {
Self {
id: entity.id,
name: entity.name.clone(),
description: entity.description.clone(),
status: entity.status,
metadata: entity.metadata.clone(),
}
}
}
impl From<Example> for ExampleSummaryDto {
fn from(entity: Example) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
status: entity.status,
created_at,
}
}
}
impl From<CreateExampleDto> for Example {
fn from(dto: CreateExampleDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
description: dto.description,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl backbone_core::FromCreateDto<CreateExampleDto> for Example {
fn from_create_dto(dto: CreateExampleDto) -> backbone_core::ServiceResult<Self> {
Ok(Example::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateExampleDto> for Example {
fn apply_update(mut self, dto: UpdateExampleDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.description = dto.description;
self.status = dto.status;
Ok(self)
}
}