Skip to content

Instantly share code, notes, and snippets.

@mazz
Last active November 9, 2019 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazz/bf3e0cdfdd682273f65b5c0ed7b5492a to your computer and use it in GitHub Desktop.
Save mazz/bf3e0cdfdd682273f65b5c0ed7b5492a to your computer and use it in GitHub Desktop.
how to make a list of maps/structs
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
[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
@mazz
Copy link
Author

mazz commented Nov 9, 2019

Solution

  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{basename: basename}, %{
      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} ->
      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()
  end

https://elixirforum.com/t/how-to-make-a-list-of-maps-structs-ecto-multi/26686

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment