-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod order_materialized_view; | ||
pub mod order_restaurant_aggregate; | ||
pub mod restaurant_materialized_view; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use crate::domain::api::OrderEvent; | ||
use crate::domain::order_view::{OrderView, OrderViewState}; | ||
use crate::framework::application::materialized_view::MaterializedView; | ||
use crate::infrastructure::order_view_state_repository::OrderViewStateRepository; | ||
|
||
/// A convenient type alias for the order materialized view. | ||
pub type OrderMeterializedView<'a> = | ||
MaterializedView<Option<OrderViewState>, OrderEvent, OrderViewStateRepository, OrderView<'a>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod order_restaurant_event_repository; | ||
pub mod order_view_state_repository; | ||
pub mod restaurant_view_state_repository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use crate::domain::api::OrderEvent; | ||
use crate::domain::order_view::OrderViewState; | ||
use crate::framework::domain::api::Identifier; | ||
use crate::framework::infrastructure::errors::ErrorMessage; | ||
use crate::framework::infrastructure::to_payload; | ||
use crate::framework::infrastructure::view_state_repository::ViewStateRepository; | ||
use pgrx::{IntoDatum, JsonB, PgBuiltInOids, Spi}; | ||
|
||
/// OrderViewStateRepository struct | ||
/// View state repository is always very specific to the domain. There is no default implementation in the `ViewStateRepository` trait. | ||
pub struct OrderViewStateRepository {} | ||
|
||
/// OrderViewStateRepository - struct implementation | ||
impl OrderViewStateRepository { | ||
/// Create a new OrderViewStateRepository | ||
pub fn new() -> Self { | ||
OrderViewStateRepository {} | ||
} | ||
} | ||
|
||
/// Implementation of the view state repository for the order `view` state. | ||
impl ViewStateRepository<OrderEvent, Option<OrderViewState>> for OrderViewStateRepository { | ||
/// Fetches current state, based on the event. | ||
fn fetch_state( | ||
&self, | ||
event: &OrderEvent, | ||
) -> Result<Option<Option<OrderViewState>>, ErrorMessage> { | ||
let query = "SELECT data FROM orders WHERE id = $1"; | ||
Spi::connect(|client| { | ||
let mut results = Vec::new(); | ||
let tup_table = client | ||
.select( | ||
query, | ||
None, | ||
Some(vec![( | ||
PgBuiltInOids::UUIDOID.oid(), | ||
event.identifier().to_string().into_datum(), | ||
)]), | ||
) | ||
.map_err(|err| ErrorMessage { | ||
message: "Failed to fetch the order: ".to_string() + &err.to_string(), | ||
})?; | ||
for row in tup_table { | ||
let data = row["data"].value::<JsonB>().map_err(|err| ErrorMessage { | ||
message: "Failed to fetch the order/payload (map `data` to `JsonB`): ".to_string() + &err.to_string(), | ||
})?.ok_or(ErrorMessage { | ||
message: "Failed to fetch order data/payload (map `data` to `JsonB`): No data/payload found".to_string(), | ||
})?; | ||
|
||
results.push(to_payload::<OrderViewState>(data)?); | ||
} | ||
Ok(Some(results.into_iter().last())) | ||
}) | ||
} | ||
/// Saves the new state. | ||
fn save(&self, state: &Option<OrderViewState>) -> Result<Option<OrderViewState>, ErrorMessage> { | ||
let state = state.as_ref().ok_or(ErrorMessage { | ||
message: "Failed to save the order: state is empty".to_string(), | ||
})?; | ||
let data = serde_json::to_value(state).map_err(|err| ErrorMessage { | ||
message: "Failed to serialize the order: ".to_string() + &err.to_string(), | ||
})?; | ||
|
||
Spi::connect(|mut client| { | ||
client | ||
.update( | ||
"INSERT INTO orders (id, data) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET data = $2 RETURNING data", | ||
None, | ||
Some(vec![ | ||
( | ||
PgBuiltInOids::UUIDOID.oid(), | ||
state.identifier.to_string().into_datum(), | ||
), | ||
( | ||
PgBuiltInOids::JSONBOID.oid(), | ||
JsonB(data).into_datum(), | ||
), | ||
]), | ||
)? | ||
.first() | ||
.get_one::<JsonB>().map(|o|{ o.map( |it| to_payload(it).unwrap() )}) | ||
}) | ||
.map(Some) | ||
.map_err(|err| ErrorMessage { | ||
message: "Failed to save the order: ".to_string() + &err.to_string(), | ||
}) | ||
.map(|state| state.unwrap()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters