Last active
April 2, 2020 18:36
-
-
Save mgiacomini/039ff7e1677c7b50312cd4e2bfb0b18d to your computer and use it in GitHub Desktop.
Domain command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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