backbone_catalog/presentation/dto/
attribute_value_dto.rs1use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10use chrono::{DateTime, Utc};
11
12#[cfg(feature = "openapi")]
13#[cfg(feature = "openapi")]
14use utoipa::ToSchema;
15
16use crate::domain::entity::AttributeValue;
17use crate::domain::entity::AuditMetadata;
18use crate::domain::entity::CatalogStatus;
19
20#[derive(Debug, Clone, Deserialize)]
29#[cfg_attr(feature = "openapi", derive(ToSchema))]
30#[serde(rename_all = "camelCase")]
31pub struct CreateAttributeValueDto {
32 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
33 #[serde(alias = "attribute_id")]
34 pub attribute_id: Uuid,
35 #[cfg_attr(feature = "openapi", schema(example = "example"))]
36 pub code: String,
37 #[cfg_attr(feature = "openapi", schema(example = "example"))]
38 pub label: String,
39 #[serde(default, skip_serializing_if = "Option::is_none", alias = "label_en")]
40 pub label_en: Option<String>,
41 #[serde(default, skip_serializing_if = "Option::is_none", alias = "swatch_hex")]
42 pub swatch_hex: Option<String>,
43 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub icon: Option<String>,
45 #[cfg_attr(feature = "openapi", schema(example = 42))]
46 #[serde(alias = "sort_order")]
47 pub sort_order: i32,
48 pub status: CatalogStatus,
49}
50
51#[derive(Debug, Clone, Deserialize)]
60#[cfg_attr(feature = "openapi", derive(ToSchema))]
61#[serde(rename_all = "camelCase")]
62pub struct UpdateAttributeValueDto {
63 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
64 #[serde(alias = "attribute_id")]
65 pub attribute_id: Uuid,
66 #[cfg_attr(feature = "openapi", schema(example = "example"))]
67 pub code: String,
68 #[cfg_attr(feature = "openapi", schema(example = "example"))]
69 pub label: String,
70 #[serde(default, skip_serializing_if = "Option::is_none", alias = "label_en")]
71 pub label_en: Option<String>,
72 #[serde(default, skip_serializing_if = "Option::is_none", alias = "swatch_hex")]
73 pub swatch_hex: Option<String>,
74 #[serde(default, skip_serializing_if = "Option::is_none")]
75 pub icon: Option<String>,
76 #[cfg_attr(feature = "openapi", schema(example = 42))]
77 #[serde(alias = "sort_order")]
78 pub sort_order: i32,
79 pub status: CatalogStatus,
80}
81
82#[derive(Debug, Clone, Default, Deserialize)]
91#[cfg_attr(feature = "openapi", derive(ToSchema))]
92#[serde(rename_all = "camelCase")]
93pub struct PatchAttributeValueDto {
94 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
95 #[serde(skip_serializing_if = "Option::is_none", alias = "attribute_id")]
96 pub attribute_id: Option<Uuid>,
97 #[cfg_attr(feature = "openapi", schema(example = "example"))]
98 #[serde(skip_serializing_if = "Option::is_none")]
99 pub code: Option<String>,
100 #[cfg_attr(feature = "openapi", schema(example = "example"))]
101 #[serde(skip_serializing_if = "Option::is_none")]
102 pub label: Option<String>,
103 #[serde(skip_serializing_if = "Option::is_none", alias = "label_en")]
104 pub label_en: Option<String>,
105 #[serde(skip_serializing_if = "Option::is_none", alias = "swatch_hex")]
106 pub swatch_hex: Option<String>,
107 #[serde(skip_serializing_if = "Option::is_none")]
108 pub icon: Option<String>,
109 #[cfg_attr(feature = "openapi", schema(example = 42))]
110 #[serde(skip_serializing_if = "Option::is_none", alias = "sort_order")]
111 pub sort_order: Option<i32>,
112 #[serde(skip_serializing_if = "Option::is_none")]
113 pub status: Option<CatalogStatus>,
114}
115
116impl PatchAttributeValueDto {
117 pub fn has_changes(&self) -> bool {
119 self.attribute_id.is_some() || self.code.is_some() || self.label.is_some() || self.label_en.is_some() || self.swatch_hex.is_some() || self.icon.is_some() || self.sort_order.is_some() || self.status.is_some()
120 }
121}
122
123#[derive(Debug, Clone, Serialize)]
132#[cfg_attr(feature = "openapi", derive(ToSchema))]
133#[serde(rename_all = "camelCase")]
134pub struct AttributeValueResponseDto {
135 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
136 pub id: Uuid,
137 #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
138 pub attribute_id: Uuid,
139 #[cfg_attr(feature = "openapi", schema(example = "example"))]
140 pub code: String,
141 #[cfg_attr(feature = "openapi", schema(example = "example"))]
142 pub label: String,
143 pub label_en: Option<String>,
144 pub swatch_hex: Option<String>,
145 pub icon: Option<String>,
146 #[cfg_attr(feature = "openapi", schema(example = 42))]
147 pub sort_order: i32,
148 pub status: CatalogStatus,
149 pub metadata: AuditMetadata,
150}
151
152#[derive(Debug, Clone, Serialize)]
161#[cfg_attr(feature = "openapi", derive(ToSchema))]
162#[serde(rename_all = "camelCase")]
163pub struct AttributeValueListResponseDto {
164 pub items: Vec<AttributeValueResponseDto>,
166 pub total: u64,
168 pub page: u32,
170 pub per_page: u32,
172 pub total_pages: u32,
174 pub has_next: bool,
176 pub has_prev: bool,
178}
179
180impl AttributeValueListResponseDto {
181 pub fn new(items: Vec<AttributeValueResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
183 let total_pages = if per_page > 0 {
184 ((total as f64) / (per_page as f64)).ceil() as u32
185 } else {
186 0
187 };
188 Self {
189 items,
190 total,
191 page,
192 per_page,
193 total_pages,
194 has_next: page < total_pages,
195 has_prev: page > 1,
196 }
197 }
198}
199
200#[derive(Debug, Clone, Serialize)]
202#[cfg_attr(feature = "openapi", derive(ToSchema))]
203#[serde(rename_all = "camelCase")]
204pub struct AttributeValueSummaryDto {
205 pub id: Uuid,
206 pub attribute_id: Uuid,
207 pub code: String,
208 pub label: String,
209 pub created_at: Option<DateTime<Utc>>,
210}
211
212impl From<AttributeValue> for AttributeValueResponseDto {
217 fn from(entity: AttributeValue) -> Self {
218 Self {
219 id: entity.id,
220 attribute_id: entity.attribute_id,
221 code: entity.code,
222 label: entity.label,
223 label_en: entity.label_en,
224 swatch_hex: entity.swatch_hex,
225 icon: entity.icon,
226 sort_order: entity.sort_order,
227 status: entity.status,
228 metadata: entity.metadata,
229 }
230 }
231}
232
233impl From<AttributeValue> for AttributeValueSummaryDto {
234 fn from(entity: AttributeValue) -> Self {
235 let created_at = backbone_core::PersistentEntity::created_at(&entity);
236 Self {
237 id: entity.id,
238 attribute_id: entity.attribute_id,
239 code: entity.code,
240 label: entity.label,
241 created_at,
242 }
243 }
244}
245
246impl From<CreateAttributeValueDto> for AttributeValue {
247 fn from(dto: CreateAttributeValueDto) -> Self {
248 Self {
249 id: Uuid::new_v4(),
250 attribute_id: dto.attribute_id,
251 code: dto.code,
252 label: dto.label,
253 label_en: dto.label_en,
254 swatch_hex: dto.swatch_hex,
255 icon: dto.icon,
256 sort_order: dto.sort_order,
257 status: dto.status,
258 metadata: AuditMetadata::default(),
259 }
260 }
261}
262
263impl From<&AttributeValue> for AttributeValueResponseDto {
264 fn from(entity: &AttributeValue) -> Self {
265 Self {
266 id: entity.id.clone(),
267 attribute_id: entity.attribute_id.clone(),
268 code: entity.code.clone(),
269 label: entity.label.clone(),
270 label_en: entity.label_en.clone(),
271 swatch_hex: entity.swatch_hex.clone(),
272 icon: entity.icon.clone(),
273 sort_order: entity.sort_order.clone(),
274 status: entity.status.clone(),
275 metadata: entity.metadata.clone(),
276 }
277 }
278}
279
280impl backbone_core::FromCreateDto<CreateAttributeValueDto> for AttributeValue {
281 fn from_create_dto(dto: CreateAttributeValueDto) -> backbone_core::ServiceResult<Self> {
282 Ok(AttributeValue::from(dto))
283 }
284}
285
286impl backbone_core::ApplyUpdateDto<UpdateAttributeValueDto> for AttributeValue {
287 fn apply_update(mut self, dto: UpdateAttributeValueDto) -> backbone_core::ServiceResult<Self> {
288 self.attribute_id = dto.attribute_id;
289 self.code = dto.code;
290 self.label = dto.label;
291 self.label_en = dto.label_en;
292 self.swatch_hex = dto.swatch_hex;
293 self.icon = dto.icon;
294 self.sort_order = dto.sort_order;
295 self.status = dto.status;
296 Ok(self)
297 }
298}
299
300