diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index f1d3cb0c..ad7b511b 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -5,6 +5,7 @@ defmodule MusicLibrary.Records do import Ecto.Query, warn: false + alias MusicLibrary.Artists alias MusicLibrary.Records.{ArtistRecord, Cover, Record, SearchParser} alias MusicLibrary.{BackgroundRepo, Repo, Worker} @@ -258,6 +259,18 @@ defmodule MusicLibrary.Records do end def create_record(attrs \\ %{}) do + with {:ok, record} <- do_create_record(attrs) do + record + |> Record.artist_ids() + |> Enum.each(fn artist_id -> + Artists.fetch_artist_info_async(artist_id) + end) + + {:ok, record} + end + end + + defp do_create_record(attrs) do %Record{} |> Record.changeset(attrs) |> Repo.insert() diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index 3b5f4619..78edc0dd 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -34,6 +34,10 @@ defmodule MusicLibrary.Records.Record do Enum.map_join(record.artists, ", ", fn artist -> artist.name end) end + def artist_ids(record) do + Enum.map(record.artists, fn artist -> artist.musicbrainz_id end) + end + def formats, do: @formats def types, do: @types diff --git a/lib/music_library/worker/fetch_artist_info.ex b/lib/music_library/worker/fetch_artist_info.ex index 417fd8fe..f6270a6c 100644 --- a/lib/music_library/worker/fetch_artist_info.ex +++ b/lib/music_library/worker/fetch_artist_info.ex @@ -3,7 +3,8 @@ defmodule MusicLibrary.Worker.FetchArtistInfo do @impl Oban.Worker def perform(%Oban.Job{args: %{"id" => artist_id}}) do - with {:ok, _artist_info} <- MusicLibrary.Artists.fetch_artist_info(artist_id) do + with {:ok, _artist_info} <- MusicLibrary.Artists.fetch_artist_info(artist_id), + {:ok, _artist_info} <- MusicLibrary.Artists.fetch_image(artist_id) do :ok end end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 76a99297..9dcfa719 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -38,8 +38,15 @@ defmodule MusicLibrary.DataCase do Sets up the sandbox based on the test tags. """ def setup_sandbox(tags) do - pid = Sandbox.start_owner!(MusicLibrary.Repo, shared: not tags[:async]) - on_exit(fn -> Sandbox.stop_owner(pid) end) + repo_pid = Sandbox.start_owner!(MusicLibrary.Repo, shared: not tags[:async]) + + background_repo_pid = + Sandbox.start_owner!(MusicLibrary.BackgroundRepo, shared: not tags[:async]) + + on_exit(fn -> + Sandbox.stop_owner(repo_pid) + Sandbox.stop_owner(background_repo_pid) + end) end @doc """ diff --git a/test/test_helper.exs b/test/test_helper.exs index bc8763ae..ef6b774c 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,2 +1,3 @@ ExUnit.start() Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual) +Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.BackgroundRepo, :manual)