defmodule PotionIngredients.CQRS.Handler do alias Plug.Conn def handle(output, presenter, conn) do output |> case do {:ok, status, body} -> handle_success(conn, presenter, status, body) {:error, status, reason} -> handle_error(conn, presenter, status, reason) end end defp handle_success(conn, presenter, status, body) when is_atom(status) and (is_map(body) or is_list(body)) do conn |> Conn.put_resp_header("Content-Type", "application/json") |> Conn.resp( status, Poison.encode!(presenter.render("index.json", Casefy.camel_case(body))) ) |> Conn.halt() end defp handle_error(conn, presenter, status, reason) when is_atom(status) and is_map(reason) do conn |> Conn.put_resp_header("Content-Type", "application/json") |> Conn.resp( status, Poison.encode!(presenter.render("error.json", Casefy.camel_case(reason))) ) |> Conn.halt() end end