Hi, I am trying to create a POST endpoint in Phoenix that will receive an array of maps %{} and I need to insert all of that data into a database as a bulk insertion.
It would be receiving something like this:
entries = [
%{name: "Frank", age: 23},
%{name: "John", age: 24}
]
Parameters look like this:
%{"data" => [%{"name" => "Frank", "age" => 23}, %{"name" => "John", "age" => 24}]}
For now I only have created the Repo functions like:
def create_users(attrs) do
User
|> Repo.insert_all(attrs)
end
I’ve searched the entire web and I can’t find any article that explains how the controllers and views should be.
I know this is incorrect because it doesn’t work but it’s more or less what I have right now
def create_many(conn, %{"data" => users_params}) do
case Users.create_users(users_params) do
{:ok, %{errors: _, rows_affected: _}} ->
conn
|> json(%{result: "ok"})
# render(conn, "bulk_creation_result.json", %{some: result})
# {:error, _, changeset, _} ->
# render(conn, MyApp.ChangesetView, "error.json", changeset: changeset)
end
As far as the views I really have no clue
Does someone have done this yet in the past and could show me a correct example?
Thanks in advance