Created
February 22, 2025 15:45
-
-
Save fredgrott/ca74901df9e56201fd46967e2fbd139f to your computer and use it in GitHub Desktop.
cqrs aggregate
This file contains hidden or 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
class AccountAggregate extends Aggregate<Account> { | |
final String name = 'account'; | |
@override | |
Account initializeModel() => Account(); | |
@override | |
Future<void> apply(Account model, DomainEvent event) async { | |
if (event is AccountCreatedEvent) { | |
model.id = event.id; | |
model.owner = event.owner; | |
model.amount = 0.0; | |
} else if (event is DepositPerformedEvent) { | |
model.amount += event.amount; | |
} else if (event is WithdrawalPerformedEvent) { | |
model.amount -= event.amount; | |
} else { | |
throw Exception("Unknown event!"); | |
} | |
} | |
@override | |
Future<void> handleCommand( | |
Command cmd, Account model, CommandOutput out) async { | |
if (cmd is CreateAccountCmd) { | |
if (model.id != null) { | |
out.setError("Model with id ${cmd.modelId} already exists!"); | |
return; | |
} | |
out.addEvent(AccountCreatedEvent(id: cmd.modelId, owner: cmd.owner)); | |
} else if (cmd is DepositCmd) { | |
out.addEvent(DepositPerformedEvent(id: cmd.modelId, amount: cmd.amount)); | |
} else if (cmd is WithdrawCmd) { | |
if (model.amount < cmd.amount) { | |
out.setError("Not enough balance!"); | |
return; | |
} | |
out.addEvent( | |
WithdrawalPerformedEvent(id: cmd.modelId, amount: cmd.amount)); | |
return; | |
} else { | |
throw UnsupportedError(cmd.runtimeType.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment