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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use ;
use Debug;
use crate::;
/// A `Query` is a read element in a CQRS system. As events are
/// emitted multiple downstream queries are updated to reflect the
/// current state of the system. A query may also be referred to as a
/// 'view', the concepts are identical but 'query' is used here to
/// conform with CQRS nomenclature.
///
/// Queries are generally serialized for persistence, usually in a
/// standard database, but a query could also utilize messaging
/// platform or other asynchronous, eventually-consistent systems.
/// # Examples
/// ```rust
/// use serde::{
/// Deserialize,
/// Serialize,
/// };
/// use std::fmt::Debug;
///
/// use cqrs_es2::{
/// example_impl::{
/// Customer,
/// CustomerEvent,CustomerCommand
/// },
/// EventContext,
/// IEventConsumer,
/// IQuery,
/// };
///
/// #[derive(
/// Debug,
/// PartialEq,
/// Default,
/// Clone,
/// Serialize,
/// Deserialize
/// )]
/// pub struct CustomerContactQuery {
/// pub name: String,
/// pub email: String,
/// pub latest_address: String,
/// }
///
/// impl IQuery<CustomerCommand, CustomerEvent> for CustomerContactQuery {
/// fn query_type() -> &'static str {
/// "customer_contact_query"
/// }
/// }
///
/// impl IEventConsumer<CustomerCommand, CustomerEvent> for CustomerContactQuery {
/// fn update(
/// &mut self,
/// event: &EventContext<CustomerCommand, CustomerEvent>,
/// ) {
/// match &event.payload {
/// CustomerEvent::NameAdded(payload) => {
/// self.name = payload.changed_name.clone();
/// },
/// CustomerEvent::EmailUpdated(payload) => {
/// self.email = payload.new_email.clone();
/// },
/// CustomerEvent::AddressUpdated(payload) => {
/// self.latest_address = payload.new_address.clone();
/// },
/// }
/// }
/// }
/// ```