Skip to content

Instantly share code, notes, and snippets.

@mazz
Created April 24, 2020 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazz/c12222346708ca0048bee51b3645e3ab to your computer and use it in GitHub Desktop.
Save mazz/c12222346708ca0048bee51b3645e3ab to your computer and use it in GitHub Desktop.
Multi.new()
|> Multi.insert(:item_without_hash_id, changeset)
|> Multi.run(:playlist, fn _repo, %{item_without_hash_id: playlist} ->
playlist
|> Playlist.changeset_generate_hash_id()
|> Repo.update()
end)
# iterate over the localized_titles and add to db
# pass in playlist that was just inserted
|> Multi.run(:add_localized_titles, fn _repo, %{playlist: playlist} ->
maps =
for title <- localized_titles,
_ = Logger.debug("title #{inspect(%{attributes: title})}"),
{k, v} <- title do
IO.puts("#{k} --> #{v}")
Repo.insert(%PlaylistTitle{
language_id: k,
localizedname: v,
uuid: Ecto.UUID.generate(),
playlist_id: playlist.id
})
end
Logger.debug("maps #{inspect(%{attributes: maps})}")
{:ok, maps}
end)
|> Repo.transaction()
|> case do
{:ok, %{playlist: playlist}} ->
{:ok, playlist}
{:error, _, error, _} ->
{:error, error}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment