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::Slot;
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 CreateSlotDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "event_id")]
pub event_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 200)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "date_begin")]
pub date_begin: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "date_end")]
pub date_end: 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 UpdateSlotDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "event_id")]
pub event_id: Uuid,
#[cfg_attr(feature = "validation", validate(length(max = 200)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(alias = "date_begin")]
pub date_begin: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "date_end")]
pub date_end: 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 PatchSlotDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "event_id")]
pub event_id: Option<Uuid>,
#[cfg_attr(feature = "validation", validate(length(max = 200)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "date_begin")]
pub date_begin: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", alias = "date_end")]
pub date_end: Option<DateTime<Utc>>,
}
impl PatchSlotDto {
pub fn has_changes(&self) -> bool {
self.event_id.is_some() || self.name.is_some() || self.date_begin.is_some() || self.date_end.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SlotResponseDto {
#[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 event_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
pub date_begin: DateTime<Utc>,
pub date_end: Option<DateTime<Utc>>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct SlotListResponseDto {
pub items: Vec<SlotResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl SlotListResponseDto {
pub fn new(items: Vec<SlotResponseDto>, 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 SlotSummaryDto {
pub id: Uuid,
pub event_id: Uuid,
pub name: String,
pub date_begin: DateTime<Utc>,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Slot> for SlotResponseDto {
fn from(entity: Slot) -> Self {
Self {
id: entity.id,
event_id: entity.event_id,
name: entity.name,
date_begin: entity.date_begin,
date_end: entity.date_end,
metadata: entity.metadata,
}
}
}
impl From<Slot> for SlotSummaryDto {
fn from(entity: Slot) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
event_id: entity.event_id,
name: entity.name,
date_begin: entity.date_begin,
created_at,
}
}
}
impl From<CreateSlotDto> for Slot {
fn from(dto: CreateSlotDto) -> Self {
Self {
id: Uuid::new_v4(),
event_id: dto.event_id,
name: dto.name,
date_begin: dto.date_begin,
date_end: dto.date_end,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Slot> for SlotResponseDto {
fn from(entity: &Slot) -> Self {
Self {
id: entity.id.clone(),
event_id: entity.event_id.clone(),
name: entity.name.clone(),
date_begin: entity.date_begin.clone(),
date_end: entity.date_end.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateSlotDto> for Slot {
fn from_create_dto(dto: CreateSlotDto) -> backbone_core::ServiceResult<Self> {
Ok(Slot::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateSlotDto> for Slot {
fn apply_update(mut self, dto: UpdateSlotDto) -> backbone_core::ServiceResult<Self> {
self.event_id = dto.event_id;
self.name = dto.name;
self.date_begin = dto.date_begin;
self.date_end = dto.date_end;
Ok(self)
}
}