Last active
November 9, 2019 23:53
-
-
Save mazz/bf3e0cdfdd682273f65b5c0ed7b5492a to your computer and use it in GitHub Desktop.
how to make a list of maps/structs
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
def add_playlist( | |
ordinal, | |
basename, | |
small_thumbnail_path, | |
med_thumbnail_path, | |
large_thumbnail_path, | |
banner_path, | |
media_category, | |
localized_titles, | |
channel_id | |
) do | |
changeset = Playlist.changeset(%Playlist{}, %{ | |
ordinal: ordinal, | |
basename: basename, | |
small_thumbnail_path: small_thumbnail_path, | |
med_thumbnail_path: med_thumbnail_path, | |
large_thumbnail_path: large_thumbnail_path, | |
banner_path: banner_path, | |
media_category: media_category, | |
channel_id: channel_id, | |
uuid: Ecto.UUID.generate() | |
}) | |
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) | |
|> Multi.run(:add_localized_titles, fn _repo, %{item_without_hash_id: playlist} -> | |
Logger.debug("localized_titles #{inspect(%{attributes: localized_titles})}") | |
maps = Enum.map(localized_titles, fn(title) -> | |
Logger.debug("title #{inspect(%{attributes: title})}") | |
map = Enum.each(title, fn {k, v} -> | |
IO.puts "#{k} --> #{v}" | |
# return a PlaylistTitle struct with the playlist_id from above | |
%PlaylistTitle{language_id: k, localizedname: v, uuid: Ecto.UUID.generate(), playlist_id: playlist.id} | |
# %{lang: k, value: v} | |
end) | |
end) | |
Logger.debug("maps #{inspect(%{attributes: maps})}") | |
# insert all the new PlaylistTitle | |
# {count, _} = Repo.insert_all(PlaylistTitle, maps) | |
end) | |
|> Repo.transaction() | |
end |
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
[debug] QUERY OK db=2.7ms | |
INSERT INTO "playlists" ("channel_id","media_category","multilanguage","ordinal","uuid","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING "id" [1, 0, false, 99, <<102, 126, 60, 219, 143, 23, 76, 178, 153, 254, 248, 45, 210, 252, 176, 170>>, ~U[2019-11-09 15:58:28Z], ~U[2019-11-09 15:58:28Z]] | |
[debug] QUERY OK db=1.0ms | |
UPDATE "playlists" SET "hash_id" = $1, "updated_at" = $2 WHERE "id" = $3 ["adyy", ~U[2019-11-09 15:58:28Z], 123] | |
en --> The English Title of a Playlist | |
[debug] localized_titles %{attributes: [%{"en" => "The English Title of a Playlist"}, %{"fr" => "Le French Title of a Playlist"}]} | |
[debug] title %{attributes: %{"en" => "The English Title of a Playlist"}} | |
[debug] title %{attributes: %{"fr" => "Le French Title of a Playlist"}} | |
fr --> Le French Title of a Playlist | |
[debug] maps %{attributes: [:ok, :ok]} | |
[debug] QUERY OK db=0.3ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution
https://elixirforum.com/t/how-to-make-a-list-of-maps-structs-ecto-multi/26686