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