From aa5499ab4cf6640668556ab25a32d2778f106973 Mon Sep 17 00:00:00 2001 From: angelo-rendina-prima <95231944+angelo-rendina-prima@users.noreply.github.com> Date: Mon, 10 Jun 2024 08:41:22 +0100 Subject: [PATCH] AggregateManager handle command returns updated state (#196) --- examples/readme/main.rs | 2 +- src/manager.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/readme/main.rs b/examples/readme/main.rs index f923d01..4596186 100644 --- a/examples/readme/main.rs +++ b/examples/readme/main.rs @@ -30,7 +30,7 @@ async fn main() { .expect("Failed to create PgStore"); let manager: AggregateManager<_> = AggregateManager::new(store); - let _: Result, PgStoreError> = manager + let _: Result, PgStoreError> = manager .handle_command(Default::default(), BookCommand::Buy { num_of_copies: 1 }) .await; } diff --git a/src/manager.rs b/src/manager.rs index 4c9bd92..b6fb50c 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -34,6 +34,8 @@ where /// /// The store transactionally persists the events - recording them in the aggregate instance's history. /// + /// On success, the updated aggregate state is returned. + /// /// Returns two layers of errors: /// - `Err(_)` if the aggregate handled the command but the outcome failed to be recorded; /// - `Ok(Err(_))` if the aggregate denied the command. @@ -41,11 +43,13 @@ where &self, mut aggregate_state: AggregateState<::State>, command: ::Command, - ) -> Result::Error>, E::Error> { + ) -> Result::State, ::Error>, E::Error> { match ::handle_command(aggregate_state.inner(), command) { Err(domain_error) => Ok(Err(domain_error)), Ok(events) => match self.event_store.persist(&mut aggregate_state, events).await { - Ok(_) => Ok(Ok(())), + Ok(store_events) => Ok(Ok(aggregate_state + .apply_store_events(store_events, ::apply_event) + .into_inner())), Err(operational_error) => Err(operational_error), }, }