Skip to content

Commit

Permalink
AggregateManager handle command returns updated state (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelo-rendina-prima authored Jun 10, 2024
1 parent ba6f98d commit aa5499a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/readme/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() {
.expect("Failed to create PgStore");

let manager: AggregateManager<_> = AggregateManager::new(store);
let _: Result<Result<(), BookError>, PgStoreError> = manager
let _: Result<Result<BookState, BookError>, PgStoreError> = manager
.handle_command(Default::default(), BookCommand::Buy { num_of_copies: 1 })
.await;
}
Expand Down
8 changes: 6 additions & 2 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ 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.
pub async fn handle_command(
&self,
mut aggregate_state: AggregateState<<E::Aggregate as Aggregate>::State>,
command: <E::Aggregate as Aggregate>::Command,
) -> Result<Result<(), <E::Aggregate as Aggregate>::Error>, E::Error> {
) -> Result<Result<<E::Aggregate as Aggregate>::State, <E::Aggregate as Aggregate>::Error>, E::Error> {
match <E::Aggregate as Aggregate>::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, <E::Aggregate as Aggregate>::apply_event)
.into_inner())),
Err(operational_error) => Err(operational_error),
},
}
Expand Down

0 comments on commit aa5499a

Please sign in to comment.