backbone_catalog/domain/entity/
attribute.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use uuid::Uuid;
5
6use super::AttributeType;
7use super::CatalogStatus;
8use super::AuditMetadata;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(transparent)]
13pub struct AttributeId(pub Uuid);
14
15impl AttributeId {
16 pub fn new(id: Uuid) -> Self { Self(id) }
17 pub fn generate() -> Self { Self(Uuid::new_v4()) }
18 pub fn into_inner(self) -> Uuid { self.0 }
19}
20
21impl std::fmt::Display for AttributeId {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 write!(f, "{}", self.0)
24 }
25}
26
27impl std::str::FromStr for AttributeId {
28 type Err = uuid::Error;
29 fn from_str(s: &str) -> Result<Self, Self::Err> {
30 Ok(Self(Uuid::parse_str(s)?))
31 }
32}
33
34impl From<Uuid> for AttributeId {
35 fn from(id: Uuid) -> Self { Self(id) }
36}
37
38impl From<AttributeId> for Uuid {
39 fn from(id: AttributeId) -> Self { id.0 }
40}
41
42impl AsRef<Uuid> for AttributeId {
43 fn as_ref(&self) -> &Uuid { &self.0 }
44}
45
46impl std::ops::Deref for AttributeId {
47 type Target = Uuid;
48 fn deref(&self) -> &Self::Target { &self.0 }
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
52pub struct Attribute {
53 pub id: Uuid,
54 pub code: String,
55 pub name: String,
56 pub attribute_type: AttributeType,
57 pub status: CatalogStatus,
58 #[serde(default)]
59 #[sqlx(json)]
60 pub metadata: AuditMetadata,
61}
62
63impl Attribute {
64 pub fn builder() -> AttributeBuilder {
66 <AttributeBuilder as Default>::default()
67 }
68
69 pub fn new(code: String, name: String, attribute_type: AttributeType, status: CatalogStatus) -> Self {
71 Self {
72 id: Uuid::new_v4(),
73 code,
74 name,
75 attribute_type,
76 status,
77 metadata: AuditMetadata::default(),
78 }
79 }
80
81 pub fn id(&self) -> &Uuid {
83 &self.id
84 }
85
86 pub fn typed_id(&self) -> AttributeId {
88 AttributeId(self.id)
89 }
90
91 pub fn created_at(&self) -> Option<&DateTime<Utc>> {
93 self.metadata.created_at.as_ref()
94 }
95
96 pub fn updated_at(&self) -> Option<&DateTime<Utc>> {
98 self.metadata.updated_at.as_ref()
99 }
100
101 pub fn is_deleted(&self) -> bool {
103 self.metadata.deleted_at.is_some()
104 }
105
106 pub fn is_active(&self) -> bool {
108 self.metadata.deleted_at.is_none()
109 }
110
111 pub fn deleted_at(&self) -> Option<&DateTime<Utc>> {
113 self.metadata.deleted_at.as_ref()
114 }
115
116 pub fn created_by(&self) -> Option<&Uuid> {
118 self.metadata.created_by.as_ref()
119 }
120
121 pub fn updated_by(&self) -> Option<&Uuid> {
123 self.metadata.updated_by.as_ref()
124 }
125
126 pub fn deleted_by(&self) -> Option<&Uuid> {
128 self.metadata.deleted_by.as_ref()
129 }
130
131 pub fn status(&self) -> &CatalogStatus {
133 &self.status
134 }
135
136
137 pub fn apply_patch(&mut self, fields: std::collections::HashMap<String, serde_json::Value>) {
143 for (key, value) in fields {
144 match key.as_str() {
145 "code" => {
146 if let Ok(v) = serde_json::from_value(value) { self.code = v; }
147 }
148 "name" => {
149 if let Ok(v) = serde_json::from_value(value) { self.name = v; }
150 }
151 "attribute_type" => {
152 if let Ok(v) = serde_json::from_value(value) { self.attribute_type = v; }
153 }
154 "status" => {
155 if let Ok(v) = serde_json::from_value(value) { self.status = v; }
156 }
157 _ => {} }
159 }
160 }
161
162 }
165
166impl super::Entity for Attribute {
167 type Id = Uuid;
168
169 fn entity_id(&self) -> &Self::Id {
170 &self.id
171 }
172
173 fn entity_type() -> &'static str {
174 "Attribute"
175 }
176}
177
178impl backbone_core::PersistentEntity for Attribute {
179 fn entity_id(&self) -> String {
180 self.id.to_string()
181 }
182 fn set_entity_id(&mut self, id: String) {
183 if let Ok(uuid) = uuid::Uuid::parse_str(&id) {
184 self.id = uuid;
185 }
186 }
187 fn created_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
188 self.metadata.created_at
189 }
190 fn set_created_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
191 self.metadata.created_at = Some(ts);
192 }
193 fn updated_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
194 self.metadata.updated_at
195 }
196 fn set_updated_at(&mut self, ts: chrono::DateTime<chrono::Utc>) {
197 self.metadata.updated_at = Some(ts);
198 }
199 fn deleted_at(&self) -> Option<chrono::DateTime<chrono::Utc>> {
200 self.metadata.deleted_at
201 }
202 fn set_deleted_at(&mut self, ts: Option<chrono::DateTime<chrono::Utc>>) {
203 self.metadata.deleted_at = ts;
204 }
205}
206
207impl backbone_orm::EntityRepoMeta for Attribute {
208 fn column_types() -> std::collections::HashMap<String, String> {
209 let mut m = std::collections::HashMap::new();
210 m.insert("id".to_string(), "uuid".to_string());
211 m.insert("attribute_type".to_string(), "attribute_type".to_string());
212 m.insert("status".to_string(), "catalog_status".to_string());
213 m
214 }
215 fn search_fields() -> &'static [&'static str] {
216 &["code", "name"]
217 }
218}
219
220#[derive(Debug, Clone, Default)]
225pub struct AttributeBuilder {
226 code: Option<String>,
227 name: Option<String>,
228 attribute_type: Option<AttributeType>,
229 status: Option<CatalogStatus>,
230}
231
232impl AttributeBuilder {
233 pub fn code(mut self, value: String) -> Self {
235 self.code = Some(value);
236 self
237 }
238
239 pub fn name(mut self, value: String) -> Self {
241 self.name = Some(value);
242 self
243 }
244
245 pub fn attribute_type(mut self, value: AttributeType) -> Self {
247 self.attribute_type = Some(value);
248 self
249 }
250
251 pub fn status(mut self, value: CatalogStatus) -> Self {
253 self.status = Some(value);
254 self
255 }
256
257 pub fn build(self) -> Result<Attribute, String> {
261 let code = self.code.ok_or_else(|| "code is required".to_string())?;
262 let name = self.name.ok_or_else(|| "name is required".to_string())?;
263
264 Ok(Attribute {
265 id: Uuid::new_v4(),
266 code,
267 name,
268 attribute_type: self.attribute_type.unwrap_or_default(),
269 status: self.status.unwrap_or_default(),
270 metadata: AuditMetadata::default(),
271 })
272 }
273}