Skip to content

Instantly share code, notes, and snippets.

@cybrox
Created March 20, 2019 15:35
Show Gist options
  • Save cybrox/aaf5f81c55f580417772f7bd9948ffa6 to your computer and use it in GitHub Desktop.
Save cybrox/aaf5f81c55f580417772f7bd9948ffa6 to your computer and use it in GitHub Desktop.
phx-ete5.ex
post "/db/factory" do
# When piped through a generic Phoenix JSON API pipeline, using a route
# like this allows you to call your factory via your test API easily.
with {:ok, schema} <- Map.fetch(conn.body_params, "schema"),
{:ok, attrs} <- Map.fetch(conn.body_params, "attributes") do
db_schema = String.to_atom(schema)
db_attrs = Enum.map(attrs, fn {k, v} -> {String.to_atom(k), v} end)
db_entry = Factory.insert(db_schema, db_attrs)
send_resp(conn, 200, Poison.encode!(%{id: db_entry.id}))
else
_ -> send_resp(conn, 401, "schema or attributes missing")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment