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