Created
April 24, 2020 20:52
-
-
Save mazz/c12222346708ca0048bee51b3645e3ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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