1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::collections::HashMap;
use crate::aggregate::{Aggregate, AggregateError};
use crate::event::{EventEnvelope};
pub trait EventStore<A, AC>
where A: Aggregate,
AC: AggregateContext<A>
{
fn load(&self, aggregate_id: &str) -> Vec<EventEnvelope<A>>;
fn load_aggregate(&self, aggregate_id: &str) -> AC;
fn commit(&self, events: Vec<A::Event>, context: AC, metadata: HashMap<String, String>) -> Result<Vec<EventEnvelope<A>>, AggregateError>;
fn wrap_events(&self, aggregate_id: &str, current_sequence: usize, resultant_events: Vec<A::Event>, base_metadata: HashMap<String, String>) -> Vec<EventEnvelope<A>> {
let mut sequence = current_sequence;
let mut wrapped_events: Vec<EventEnvelope<A>> = Vec::new();
for payload in resultant_events {
sequence += 1;
let aggregate_type = A::aggregate_type().to_string();
let aggregate_id: String = aggregate_id.to_string();
let sequence = sequence;
let metadata = base_metadata.clone();
wrapped_events.push(EventEnvelope::new_with_metadata(
aggregate_id,
sequence,
aggregate_type,
payload,
metadata,
));
}
wrapped_events
}
}
pub trait AggregateContext<A>
where A: Aggregate {
fn aggregate(&self) -> &A;
}