forked from primait/event_sourcing.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
149 lines (121 loc) · 3.8 KB
/
main.rs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use serde::{Deserialize, Serialize};
use sqlx::{PgConnection, Pool, Postgres};
use thiserror::Error;
use esrs::bus::EventBus;
use esrs::handler::{EventHandler, TransactionalEventHandler};
use esrs::manager::AggregateManager;
use esrs::store::postgres::{PgStore, PgStoreBuilder, PgStoreError};
use esrs::store::StoreEvent;
use esrs::Aggregate;
use crate::common::util::new_pool;
#[path = "../common/lib.rs"]
mod common;
//////////////////////////////
// Application entry point
#[tokio::main]
async fn main() {
let pool: Pool<Postgres> = new_pool().await;
let store: PgStore<Book> = PgStoreBuilder::new(pool)
.add_event_handler(BookEventHandler)
.add_transactional_event_handler(BookTransactionalEventHandler)
.add_event_bus(BookEventBus)
.try_build()
.await
.expect("Failed to create PgStore");
let manager: AggregateManager<_> = AggregateManager::new(store);
let _: Result<(), Error> = manager
.handle_command(Default::default(), BookCommand::Buy { num_of_copies: 1 })
.await;
}
//////////////////////////////
// Global error
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Aggregate(#[from] BookError),
#[error(transparent)]
Store(#[from] PgStoreError),
}
//////////////////////////////
// Aggregate
pub struct Book;
impl Aggregate for Book {
const NAME: &'static str = "book";
type State = BookState;
type Command = BookCommand;
type Event = BookEvent;
type Error = BookError;
fn handle_command(state: &Self::State, command: Self::Command) -> Result<Vec<Self::Event>, Self::Error> {
match command {
BookCommand::Buy { num_of_copies } if state.leftover < num_of_copies => Err(BookError::NotEnoughCopies),
BookCommand::Buy { num_of_copies } => Ok(vec![BookEvent::Bought { num_of_copies }]),
BookCommand::Return { num_of_copies } => Ok(vec![BookEvent::Returned { num_of_copies }]),
}
}
fn apply_event(state: Self::State, payload: Self::Event) -> Self::State {
match payload {
BookEvent::Bought { num_of_copies } => BookState {
leftover: state.leftover - num_of_copies,
},
BookEvent::Returned { num_of_copies } => BookState {
leftover: state.leftover + num_of_copies,
},
}
}
}
pub struct BookState {
pub leftover: i32,
}
impl Default for BookState {
fn default() -> Self {
Self { leftover: 10 }
}
}
pub enum BookCommand {
Buy { num_of_copies: i32 },
Return { num_of_copies: i32 },
}
#[derive(Serialize, Deserialize, Clone)]
pub enum BookEvent {
Bought { num_of_copies: i32 },
Returned { num_of_copies: i32 },
}
#[cfg(feature = "upcasting")]
impl esrs::event::Upcaster for BookEvent {}
#[derive(Debug, Error)]
pub enum BookError {
#[error("Not enough copies")]
NotEnoughCopies,
}
//////////////////////////////
// Event handler
pub struct BookEventHandler;
#[async_trait::async_trait]
impl EventHandler<Book> for BookEventHandler {
async fn handle(&self, _event: &StoreEvent<BookEvent>) {
// Implementation here
}
}
//////////////////////////////
// Transactional event handler
pub struct BookTransactionalEventHandler;
#[async_trait::async_trait]
impl TransactionalEventHandler<Book, PgStoreError, PgConnection> for BookTransactionalEventHandler {
async fn handle(
&self,
_event: &StoreEvent<BookEvent>,
_transaction: &mut PgConnection,
) -> Result<(), PgStoreError> {
// Implementation here
Ok(())
}
}
//////////////////////////////
// Event bus
pub struct BookEventBus;
#[async_trait::async_trait]
impl EventBus<Book> for BookEventBus {
async fn publish(&self, _store_event: &StoreEvent<BookEvent>) {
// Implementation here
}
}