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::Bank;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::BankStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateBankDto {
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "validation", validate(length(max = 11)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "swift_bic")]
pub swift_bic: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 2)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub country: String,
pub status: BankStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateBankDto {
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
#[cfg_attr(feature = "validation", validate(length(max = 11)))]
#[serde(default, skip_serializing_if = "Option::is_none", alias = "swift_bic")]
pub swift_bic: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 2)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub country: String,
pub status: BankStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchBankDto {
#[cfg_attr(feature = "validation", validate(length(max = 140)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 11)))]
#[serde(skip_serializing_if = "Option::is_none", alias = "swift_bic")]
pub swift_bic: Option<String>,
#[cfg_attr(feature = "validation", validate(length(max = 2)))]
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub country: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<BankStatus>,
}
impl PatchBankDto {
pub fn has_changes(&self) -> bool {
self.name.is_some() || self.swift_bic.is_some() || self.country.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub name: String,
pub swift_bic: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub country: String,
pub status: BankStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct BankListResponseDto {
pub items: Vec<BankResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl BankListResponseDto {
pub fn new(items: Vec<BankResponseDto>, 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 BankSummaryDto {
pub id: Uuid,
pub name: String,
pub swift_bic: Option<String>,
pub country: String,
pub created_at: Option<DateTime<Utc>>,
}
impl From<Bank> for BankResponseDto {
fn from(entity: Bank) -> Self {
Self {
id: entity.id,
name: entity.name,
swift_bic: entity.swift_bic,
country: entity.country,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<Bank> for BankSummaryDto {
fn from(entity: Bank) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
name: entity.name,
swift_bic: entity.swift_bic,
country: entity.country,
created_at,
}
}
}
impl From<CreateBankDto> for Bank {
fn from(dto: CreateBankDto) -> Self {
Self {
id: Uuid::new_v4(),
name: dto.name,
swift_bic: dto.swift_bic,
country: dto.country,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&Bank> for BankResponseDto {
fn from(entity: &Bank) -> Self {
Self {
id: entity.id.clone(),
name: entity.name.clone(),
swift_bic: entity.swift_bic.clone(),
country: entity.country.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateBankDto> for Bank {
fn from_create_dto(dto: CreateBankDto) -> backbone_core::ServiceResult<Self> {
Ok(Bank::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateBankDto> for Bank {
fn apply_update(mut self, dto: UpdateBankDto) -> backbone_core::ServiceResult<Self> {
self.name = dto.name;
self.swift_bic = dto.swift_bic;
self.country = dto.country;
self.status = dto.status;
Ok(self)
}
}