Last active
December 10, 2016 01:52
-
-
Save travisjeffery/d31d047cb1a561a9c0aa to your computer and use it in GitHub Desktop.
Example of using Elixir's pattern matching to simplify and clean up some code
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 Stash.AccountController do | |
| use Stash.Web, :controller | |
| def create(conn, %{"accounts" => accounts}) do | |
| case Aggregation.add_all_accounts([ | |
| institution_id: accounts["institution_id"], | |
| customer_id: customer_id(conn.assigns.current_user), | |
| credentials: accounts["credentials"] | |
| ]) do | |
| {:mfa, %{"questions" => questions, "mfa_session" => mfa_session}} -> | |
| render conn, "mfa.html", questions: questions, mfa_session: mfa_session, institution_id: accounts["institution_id"] | |
| {:ok, %{"accounts" => accounts}} -> | |
| update_user_with_accounts(conn, accounts) | |
| end | |
| end | |
| def create(conn, %{"mfa" => mfa}) do | |
| case Aggregation.add_all_accounts_with_mfa([ | |
| customer_id: customer_id(conn.assigns.current_user), | |
| institution_id: mfa["institution_id"], | |
| mfa_session: mfa["mfa_session"], | |
| mfa_challenges: mfa["questions"] | |
| ]) do | |
| {:mfa, %{"questions" => questions, "mfa_session" => mfa_session}} -> | |
| render conn, "mfa.html", questions: questions, mfa_session: mfa_session, institution_id: mfa["institution_id"] | |
| {:ok, %{"accounts" => accounts}} -> | |
| update_user_with_accounts(conn, accounts) | |
| end | |
| end | |
| defp update_user_with_accounts(conn, accounts) do | |
| # ... | |
| 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 Stash.AccountController do | |
| use Stash.Web, :controller | |
| def create(conn, %{"accounts" => accounts}) do | |
| Aggregation.add_all_accounts([ | |
| institution_id: accounts["institution_id"], | |
| customer_id: customer_id(conn.assigns.current_user), | |
| credentials: accounts["credentials"] | |
| ]) |> handle_accounts(accounts["institution_id"], conn) | |
| end | |
| def create(conn, %{"mfa" => mfa}) do | |
| Aggregation.add_all_accounts_with_mfa([ | |
| customer_id: customer_id(conn.assigns.current_user), | |
| institution_id: mfa["institution_id"], | |
| mfa_session: mfa["mfa_session"], | |
| mfa_challenges: mfa["questions"] | |
| ]) |> handle_accounts(mfa["institution_id"], conn) | |
| end | |
| defp handle_accounts({:ok, %{"accounts" => accounts}}, _institution_id, conn), do: update_user_with_accounts(conn, accounts) | |
| defp handle_accounts({:mfa, %{"questions" => questions, "mfa_session" => mfa_session}}, institution_id, conn) do | |
| render conn, "mfa.html", questions: questions, mfa_session: mfa_session, institution_id: institution_id | |
| end | |
| defp update_user_with_accounts(conn, accounts) do | |
| # ... | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment