Ecto транзакции
{:ok, %{book_id: book_id}} =
Multi.new()
|> Multi.run(:country, fn _repo, _current_state ->
{:ok, %Books.Country{}} = Books.insert_or_get_country("USA", "US")
end)
|> Multi.run(:author, fn _repo, %{country: %{id: country_id}} ->
{:ok, %{id: author_id}} =
Books.insert_or_get_author(
"Дан",
"Симънс",
Date.from_iso8601!("1948-04-04"),
country_id
)
end)
|> Multi.run(:book_id, fn _repo, _current_state ->
{:ok, Books.insert_book!("978-619-152-344-3", "Ужас", @desc, 2013, "български")}
end)
|> Multi.run(:authors_books, fn _repo, %{book_id: book_id, author: %{id: author_id}} ->
case Books.add_authors_to_book(book_id, [author_id]) do
:ok ->
{:ok, nil}
error ->
error
end
end)
|> Books.Repo.transaction()