Skip to content

Instantly share code, notes, and snippets.

@imetallica
Last active July 18, 2017 19:51
Show Gist options
  • Save imetallica/d959f2c5f3cbe8df2410a7dbc188994f to your computer and use it in GitHub Desktop.
Save imetallica/d959f2c5f3cbe8df2410a7dbc188994f to your computer and use it in GitHub Desktop.
defmodule App.AccountCtx do
alias __MODULE__.User
alias Ecto.Multi
def create_account(params) do
user_changeset = User.changeset(%User{}, params)
Multi.new()
|> Multi.insert(:create_account_on_acc_ctx, user_changeset)
|> Multi.run(:send_email, &App.Emailer.send_mail/1) # Logic to send e-mail
end
end
defmodule App.SchoolsCtx do
alias __MODULE__.User
alias Ecto.Multi
def create_account(params) do
user_changeset = User.changeset(%User{}, params)
Multi.insert(Multi.new(), :create_account_on_sch_ctx, user_changeset)
end
end
defmodule App.SomeResolverModule do
alias Ecto.Multi
alias App.{Repo, SchoolsCtx, AccountsCtx}
def resolve_acc_creation(params, _) do
schools = SchoolsCtx.create_user(params)
accounts = AccountsCtx.create_user(params)
all_operations = Multi.append(schools, accounts)
Repo.transaction(all_operations)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment