backbone_catalog/domain/entity/
mod.rs1pub mod attribute;
6pub mod attribute_value;
7pub mod brand;
8pub mod item;
9pub mod item_group;
10pub mod item_variant;
11pub mod uom;
12pub mod uom_conversion;
13pub mod attribute_type;
14pub mod item_type;
15pub mod catalog_status;
16pub mod uom_type;
17
18pub use attribute::Attribute;
20pub use attribute::AttributeBuilder;
21pub use attribute::AttributeId;
22pub use attribute_value::AttributeValue;
23pub use attribute_value::AttributeValueBuilder;
24pub use attribute_value::AttributeValueId;
25pub use brand::Brand;
26pub use brand::BrandBuilder;
27pub use brand::BrandId;
28pub use item::Item;
29pub use item::ItemBuilder;
30pub use item::ItemId;
31pub use item_group::ItemGroup;
32pub use item_group::ItemGroupBuilder;
33pub use item_group::ItemGroupId;
34pub use item_variant::ItemVariant;
35pub use item_variant::ItemVariantBuilder;
36pub use item_variant::ItemVariantId;
37pub use uom::Uom;
38pub use uom::UomBuilder;
39pub use uom::UomId;
40pub use uom_conversion::UomConversion;
41pub use uom_conversion::UomConversionBuilder;
42pub use uom_conversion::UomConversionId;
43pub use attribute_type::AttributeType;
44pub use item_type::ItemType;
45pub use catalog_status::CatalogStatus;
46pub use uom_type::UomType;
47
48use std::fmt::Debug;
53
54pub trait Entity: Debug + Clone {
59 type Id: Debug + Clone + PartialEq;
61
62 fn entity_id(&self) -> &Self::Id;
64
65 fn entity_type() -> &'static str;
67}
68
69use chrono::{DateTime, Utc};
74use serde::{Deserialize, Serialize};
75use uuid::Uuid;
76#[cfg(feature = "openapi")]
77use utoipa::ToSchema;
78
79#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
84#[cfg_attr(feature = "openapi", derive(ToSchema))]
85pub struct AuditMetadata {
86 #[serde(skip_serializing_if = "Option::is_none")]
88 pub created_at: Option<DateTime<Utc>>,
89
90 #[serde(skip_serializing_if = "Option::is_none")]
92 pub updated_at: Option<DateTime<Utc>>,
93
94 #[serde(skip_serializing_if = "Option::is_none")]
96 pub deleted_at: Option<DateTime<Utc>>,
97
98 #[serde(skip_serializing_if = "Option::is_none")]
100 pub created_by: Option<Uuid>,
101
102 #[serde(skip_serializing_if = "Option::is_none")]
104 pub updated_by: Option<Uuid>,
105
106 #[serde(skip_serializing_if = "Option::is_none")]
108 pub deleted_by: Option<Uuid>,
109}
110
111impl AuditMetadata {
112 pub fn new() -> Self {
114 Self {
115 created_at: Some(Utc::now()),
116 ..Default::default()
117 }
118 }
119
120 pub fn with_creator(creator_id: Uuid) -> Self {
122 Self {
123 created_at: Some(Utc::now()),
124 created_by: Some(creator_id),
125 ..Default::default()
126 }
127 }
128
129 pub fn touch(&mut self) {
131 self.updated_at = Some(Utc::now());
132 }
133
134 pub fn touch_by(&mut self, updater_id: Uuid) {
136 self.updated_at = Some(Utc::now());
137 self.updated_by = Some(updater_id);
138 }
139
140 pub fn soft_delete(&mut self) {
142 self.deleted_at = Some(Utc::now());
143 }
144
145 pub fn soft_delete_by(&mut self, deleter_id: Uuid) {
147 self.deleted_at = Some(Utc::now());
148 self.deleted_by = Some(deleter_id);
149 }
150
151 pub fn restore(&mut self) {
153 self.deleted_at = None;
154 self.deleted_by = None;
155 }
156
157 pub fn is_deleted(&self) -> bool {
159 self.deleted_at.is_some()
160 }
161
162 pub fn is_active(&self) -> bool {
164 self.deleted_at.is_none()
165 }
166}