I'm trying to insert an invoice struct along with its associated invoice items. I'm able to insert the invoice data, and call an anonymous function to validate, cast, and insert each item. Since insert/2 does not produce a return, how can I get the invoice_id for the items while still being able to roll back the entire transaction if one item fails validation or insertion?
I've put the code in my own repo, here it is:
def insertassoc(params) do
Repo.transaction(fn ->
i = Invoice.changeset(params["data"], :create)
if i.valid? do
Repo.insert(i)
else
Repo.rollback(i.errors)
end
insert_include = fn k ->
c = InvoiceItem.changeset(k, :create)
if c.valid? do
Repo.insert(c)
else
Repo.rollback(c.errors)
end
end
for include <- params["includes"] do
insert_include.(Map.merge(include, %{"invoice_id" => ????}))
end
end)
end
and here is how I use it from my controller:
def create(conn, params) do
case InvoiceRepo.insertassoc(params) do
{:ok, x} ->
json conn, Map.merge(params, %{"message" => "OK"})
{:error, x} ->
json conn |> put_status(400), Map.merge(params, %{"message"
=> "Error"})
end
end
There aren't many up to date examples out there with Ecto, so sorry if these are noob questions ;-). Anyone have an idea? I tried putting the invoice insert in a private function, and using a case block to determine whether the main transaction should roll back, but I couldn't figure out how to get the invoice id back from that either.