event_driven_core/
outbox.rs1use crate::{prelude::BaseError, unit_of_work::Executor};
2use async_trait::async_trait;
3use chrono::{DateTime, Utc};
4use std::sync::Arc;
5use tokio::sync::RwLock;
6use uuid::Uuid;
7
8#[derive(Debug, Clone)]
9pub struct OutBox {
10 pub id: Uuid,
11 pub aggregate_id: String,
12 pub topic: String,
13 pub state: String,
14 pub processed: bool,
15 pub create_dt: DateTime<Utc>,
16}
17
18impl OutBox {
19 pub fn new(aggregate_id: String, topic: String, state: String) -> Self {
20 Self {
21 id: Uuid::new_v4(),
22 aggregate_id,
23 topic,
24 state,
25 processed: false,
26 create_dt: Default::default(),
27 }
28 }
29
30 pub fn tag_processed(&mut self) {
31 self.processed = true
32 }
33
34 pub fn id(&self) -> Uuid {
35 self.id
36 }
37 pub fn aggregate_id(&self) -> &str {
38 &self.aggregate_id
39 }
40 pub fn topic(&self) -> &str {
41 &self.topic
42 }
43 pub fn state(&self) -> &str {
44 &self.state
45 }
46 pub fn processed(&self) -> bool {
47 self.processed
48 }
49 pub fn create_dt(&self) -> DateTime<Utc> {
50 self.create_dt
51 }
52}
53
54#[async_trait]
55pub trait IOutBox<E: Executor> {
56 async fn add(executor: Arc<RwLock<E>>, outboxes: Vec<OutBox>) -> Result<(), BaseError>;
57 async fn get() -> Result<Vec<Box<Self>>, BaseError>;
58 async fn update(&self, executor: Arc<RwLock<E>>) -> Result<(), BaseError>;
59}