Last active
July 18, 2017 19:51
-
-
Save imetallica/d959f2c5f3cbe8df2410a7dbc188994f to your computer and use it in GitHub Desktop.
This file contains 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 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