Aicacia.Handler behaviour (Aicacia Handler v0.1.1) View Source
defmodule TestError do
defexception [:message]
end
defmodule TestHandler do
use Aicacia.Handler
schema "" do
field(:name, :string)
field(:age, :integer, default: 18)
field(:fail, :boolean, default: false)
end
def changeset(%{} = attrs) do
%TestHandler{}
|> cast(attrs, [:name, :age, :fail])
|> validate_required([:name])
end
def handle(%{} = command) do
if command.fail do
{:error, %TestError{message: "fail"}}
else
{:ok, %{name: command.name, age: command.age}}
end
end
end
%{name: "Bob", age: 18} = TestHandler.new!(%{name: "Bob"}) |> TestHandler.handle!()
command = TestHandler.new!(%{name: "Bob", fail: true})
assert_raise(TestError, fn ->
TestHandler.handle!(command)
end)
{:error, _changeset} = TestHandler.new(%{})
Link to this section Summary
Callbacks
ecto changeset function https://hexdocs.pm/ecto/Ecto.Changeset.html
handles the command created by new/1
or new!/1
runs the handle raising on error
creates a new changeset either returning the params or the changeset with the errors
creates a new changeset either returning the params or raising the changeset
covert this schema struct to a Map.t()
Link to this section Callbacks
Specs
changeset(attrs :: Map.t()) :: Ecto.Changeset.t()
ecto changeset function https://hexdocs.pm/ecto/Ecto.Changeset.html
Specs
handles the command created by new/1
or new!/1
Specs
handle!(command :: Map.t()) :: term()
runs the handle raising on error
Specs
new(attrs :: Map.t()) :: {:ok, Map.t()} | {:error, Ecto.Changeset.t()}
creates a new changeset either returning the params or the changeset with the errors
Specs
new!(attrs :: Map.t()) :: Map.t()
creates a new changeset either returning the params or raising the changeset
Specs
to_map(struct :: Map.t()) :: Map.t()
covert this schema struct to a Map.t()