I have an API that allows users to update and insert items with a single request. It works by supplying a list of JSON objects to an endpoint that will update items where the primary key exists, and insert new items where the primary key doesn’t exist. I have this working with:
def create_machines(attrs) do
x =
attrs
|> Enum.map(fn x -> get_changeset(x) end)
|> Enum.map(fn x -> Repo.insert_or_update(x) end)
end
defp get_changeset(params) do
case Repo.get_by(Machine, name: params["name"]) do
nil ->
# Insert a new machine
Machine.changeset(%Machine{}, params)
machine ->
# Update the machine data
Machine.changeset(machine, params)
end
end
I get back a list of {ok: Machine}
items. What I would like to have though is a list of updated machines and a list of new machines so that the returned JSON is something like
{
"new":
[
{"name": "machine1", ...}, {...}
],
"updated":
[
{"name": "machine2", ....}, .{...}
]
}
I’m just not sure how best to approach generating this result properly. I’ve tried inserting or updating the changeset in the get_changeset
function so that I can return something like {:update, machine}
and {:create, machine}
but then I got stuck with trying to convert
[{:update, machine1}, {:update, machine2}, {:created, machine3}]
Into the format above for display.
Any advice on what the best approach is for this? thanks