eventuali_core/
aggregate.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4pub type AggregateId = String;
5pub type AggregateVersion = i64;
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct Aggregate {
9    pub id: AggregateId,
10    pub version: AggregateVersion,
11    pub aggregate_type: String,
12}
13
14impl Aggregate {
15    pub fn new(id: AggregateId, aggregate_type: String) -> Self {
16        Self {
17            id,
18            version: 0,
19            aggregate_type,
20        }
21    }
22
23    pub fn new_with_uuid(aggregate_type: String) -> Self {
24        Self::new(Uuid::new_v4().to_string(), aggregate_type)
25    }
26
27    pub fn increment_version(&mut self) {
28        self.version += 1;
29    }
30
31    pub fn is_new(&self) -> bool {
32        self.version == 0
33    }
34}
35
36#[derive(Debug, Clone)]
37pub struct AggregateSnapshot {
38    pub aggregate_id: AggregateId,
39    pub aggregate_type: String,
40    pub version: AggregateVersion,
41    pub data: serde_json::Value,
42    pub timestamp: chrono::DateTime<chrono::Utc>,
43}
44
45impl AggregateSnapshot {
46    pub fn new(
47        aggregate_id: AggregateId,
48        aggregate_type: String,
49        version: AggregateVersion,
50        data: serde_json::Value,
51    ) -> Self {
52        Self {
53            aggregate_id,
54            aggregate_type,
55            version,
56            data,
57            timestamp: chrono::Utc::now(),
58        }
59    }
60}