When creating a record, queue a job to fetch artist info
This commit is contained in:
@@ -5,6 +5,7 @@ defmodule MusicLibrary.Records do
|
|||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
|
|
||||||
|
alias MusicLibrary.Artists
|
||||||
alias MusicLibrary.Records.{ArtistRecord, Cover, Record, SearchParser}
|
alias MusicLibrary.Records.{ArtistRecord, Cover, Record, SearchParser}
|
||||||
alias MusicLibrary.{BackgroundRepo, Repo, Worker}
|
alias MusicLibrary.{BackgroundRepo, Repo, Worker}
|
||||||
|
|
||||||
@@ -258,6 +259,18 @@ defmodule MusicLibrary.Records do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_record(attrs \\ %{}) do
|
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{}
|
||||||
|> Record.changeset(attrs)
|
|> Record.changeset(attrs)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
Enum.map_join(record.artists, ", ", fn artist -> artist.name end)
|
Enum.map_join(record.artists, ", ", fn artist -> artist.name end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def artist_ids(record) do
|
||||||
|
Enum.map(record.artists, fn artist -> artist.musicbrainz_id end)
|
||||||
|
end
|
||||||
|
|
||||||
def formats, do: @formats
|
def formats, do: @formats
|
||||||
def types, do: @types
|
def types, do: @types
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ defmodule MusicLibrary.Worker.FetchArtistInfo do
|
|||||||
|
|
||||||
@impl Oban.Worker
|
@impl Oban.Worker
|
||||||
def perform(%Oban.Job{args: %{"id" => artist_id}}) do
|
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
|
:ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -38,8 +38,15 @@ defmodule MusicLibrary.DataCase do
|
|||||||
Sets up the sandbox based on the test tags.
|
Sets up the sandbox based on the test tags.
|
||||||
"""
|
"""
|
||||||
def setup_sandbox(tags) do
|
def setup_sandbox(tags) do
|
||||||
pid = Sandbox.start_owner!(MusicLibrary.Repo, shared: not tags[:async])
|
repo_pid = Sandbox.start_owner!(MusicLibrary.Repo, shared: not tags[:async])
|
||||||
on_exit(fn -> Sandbox.stop_owner(pid) end)
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
ExUnit.start()
|
ExUnit.start()
|
||||||
Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual)
|
Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.Repo, :manual)
|
||||||
|
Ecto.Adapters.SQL.Sandbox.mode(MusicLibrary.BackgroundRepo, :manual)
|
||||||
|
|||||||
Reference in New Issue
Block a user