Skip to content

Instantly share code, notes, and snippets.

@mgiacomini
Last active April 2, 2020 18:36
Show Gist options
  • Save mgiacomini/039ff7e1677c7b50312cd4e2bfb0b18d to your computer and use it in GitHub Desktop.
Save mgiacomini/039ff7e1677c7b50312cd4e2bfb0b18d to your computer and use it in GitHub Desktop.
Domain command
defmodule CustomerManagement.Origination.Commands.CreateProposalCommand do
use Ecto.Schema
import Ecto.Changeset
alias MyApp.Repo
alias Ecto.Multi
alias CustomerManagement.Origination.Proposal
alias CustomerManagement.Origination.Events.ProposalCreatedEvent
@primary_key false
embedded_schema do
has_one :proposal, Proposal
field :realty_type, :string
field :realty_value, Money.Ecto.Amount.Type
end
def execute(%{} = attrs) do
Multi.new
|> Multi.insert(:proposal, user_changeset(attrs))
|> Multi.insert(:user_registred_event, UserRegistredEvent.new(attrs))
end
def user_changeset(%{} = attrs) do
%User{}
|> cast(attrs, [:email, :password])
|> validate_required([:email, :password])
end
end
defmodule MyApp.Accounts.Commands.RegisterUserCommand do
import Ecto.Changeset
alias MyApp.Repo
alias Ecto.Multi
alias MyApp.Accounts.User
alias MyApp.Accounts.Events.UserRegistredEvent
def execute(%{} = attrs) do
Multi.new
|> Multi.insert(:user, user_changeset(attrs))
|> Multi.insert(:user_registred_event, UserRegistredEvent.new(attrs))
end
def user_changeset(%{} = attrs) do
%User{}
|> cast(attrs, [:email, :password])
|> validate_required([:email, :password])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment