Skip to content

Instantly share code, notes, and snippets.

@mmmries
Last active April 20, 2020 13:12
Show Gist options
  • Save mmmries/19b587851c117464749c0317a3112c02 to your computer and use it in GitHub Desktop.
Save mmmries/19b587851c117464749c0317a3112c02 to your computer and use it in GitHub Desktop.
Kick off job after saving changes
defmodule UserController do
def create(params) do
changeset = params
|> cast([:name, :email])
|> validate_required([:email])
|> validate_other_stuff()
WithSideEffects.create(changeset)
end
end
defmodule WithSideEffects do
def create(changeset) do
case Repo.insert(changeset) do
{:ok, record} ->
async_publish_job(record, :create)
{:error, changeset} ->
{:error, changeset}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment