A model-bound command: a module whose struct is the request and whose handle/2 decides.
defmodule MyApp.Authors.Registration.RegisterAuthor do
@moduledoc "Registers a new author."
use Arc.Command
schema do
field :id, :uuid, key: true, required: true
field :name, :string, required: true, max_length: 100
end
def handle(%__MODULE__{} = command, _ctx) do
{:append, %MyApp.Authors.AuthorRegistered{name: command.name}}
end
endCallbacks
provide/2(optional) - load data after authorization and validation; return:ok,{:ok, map}(merged intoctx.provided) or{:error, results}.validate/2(optional) - cross-field rules; return:okor{:error, results}.handle/2- decide. A command either records what happened or is rejected; it never succeeds as a silent no-op.
What handle/2 returns
{:append, events}- append one event or a list to Chronicle. The event source id is the command'skey: truefield; wrap an event as{event_source_id, event}to target another.{:append, events, response}- append and return a value to the client.{:ok, response}- succeed without recording an event, for example a command that only calls a collaborator. Prefer{:append, ...}for state changes.{:error, results}- reject with a list of (or one)Arc.ValidationResult.
Declarations
authorize/1andallow_anonymous/0- seeArc.Authorizationresponse/1- the type returned to the client, for the generated proxy
Summary
Functions
Allows anonymous callers.
Declares who may run the command. See Arc.Authorization.
Declares the response type, for example response :string or response MyApp.Receipt.
Types
@type result() :: {:append, term()} | {:append, term(), term()} | {:ok, term()} | {:error, Arc.ValidationResult.t() | [Arc.ValidationResult.t()]}
Callbacks
@callback handle(struct(), Arc.Context.t()) :: result()
@callback provide(struct(), Arc.Context.t()) :: :ok | {:ok, map()} | {:error, Arc.ValidationResult.t() | [Arc.ValidationResult.t()]}
@callback validate(struct(), Arc.Context.t()) :: :ok | {:error, Arc.ValidationResult.t() | [Arc.ValidationResult.t()]}
Functions
Allows anonymous callers.
Declares who may run the command. See Arc.Authorization.
Declares the response type, for example response :string or response MyApp.Receipt.