View Source OnePiece.Commanded.CommandHandler (OnePiece.Commanded v0.19.1)

Defines a module as a "Command Handler". For more information about commands, please read the following:

Summary

Functions

Link to this macro

__using__(opts \\ [])

View Source (macro)
@spec __using__(opts :: []) :: any()

Convert the module into a Commanded.Commands.Handler.

Use Commanded.Aggregate.Multi to generate multiple events from a single command. This can be useful when you want to emit multiple events that depend upon the aggregate state being updated.

Usage

defmodule MyCommandHandler do
  use OnePiece.Commanded.CommandHandler

  @impl Commanded.Commands.Handler
  def handle(account, command) do
    account
    |> Multi.new()
    |> Multi.execute(&withdraw_money(&1, command.amount))
    |> Multi.execute(&check_balance/1)
  end

  defp withdraw_money(account, amount) do
    # ...
  end

  defp check_balance(account, amount) do
    # ...
  end
end