The book implements handle_command/2
for :sub
as follows:
def handle_command(%{value: val}, %{cmd: :sub, value: v}) do
%{event_type: :value_subtracted, max(@min_state_value, val - v)
end
The value in the created event is the result expected after subtraction, i.e., it is not the subtrahend. It can be reimplemented as follows instead:
def handle_command(%{value: val}, %{cmd: :sub, value: v) do
subtrahend = if(val >= v, do: v, else: val)
%{event_type: :value_subtracted, value: subtrahend}
end