Update artists when refreshing MusicBrainz data
This commit is contained in:
@@ -8,7 +8,7 @@ defmodule MusicLibrary.Artists.Artist do
|
|||||||
field :name, :string
|
field :name, :string
|
||||||
field :sort_name, :string
|
field :sort_name, :string
|
||||||
field :disambiguation, :string
|
field :disambiguation, :string
|
||||||
field :joinphrase, :string, default: " and "
|
field :joinphrase, :string, default: ""
|
||||||
end
|
end
|
||||||
|
|
||||||
def changeset(artist, attrs) do
|
def changeset(artist, attrs) do
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
field :release_ids, {:array, :string}, default: []
|
field :release_ids, {:array, :string}, default: []
|
||||||
field :included_release_group_ids, {:array, :string}, default: []
|
field :included_release_group_ids, {:array, :string}, default: []
|
||||||
|
|
||||||
embeds_many :artists, Artist
|
embeds_many :artists, Artist, on_replace: :delete
|
||||||
|
|
||||||
timestamps(type: :utc_datetime)
|
timestamps(type: :utc_datetime)
|
||||||
end
|
end
|
||||||
@@ -134,7 +134,9 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
|
|
||||||
def add_musicbrainz_data(record, musicbrainz_data) do
|
def add_musicbrainz_data(record, musicbrainz_data) do
|
||||||
record
|
record
|
||||||
|> change(musicbrainz_data: musicbrainz_data)
|
|> change()
|
||||||
|
|> force_change(:musicbrainz_data, musicbrainz_data)
|
||||||
|
|> update_artists()
|
||||||
|> update_release_ids()
|
|> update_release_ids()
|
||||||
|> update_included_release_group_ids()
|
|> update_included_release_group_ids()
|
||||||
end
|
end
|
||||||
@@ -163,6 +165,16 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp update_artists(changeset) do
|
||||||
|
case get_change(changeset, :musicbrainz_data) do
|
||||||
|
nil ->
|
||||||
|
changeset
|
||||||
|
|
||||||
|
musicbrainz_data ->
|
||||||
|
put_change(changeset, :artists, parse_artists(musicbrainz_data))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp update_included_release_group_ids(changeset) do
|
defp update_included_release_group_ids(changeset) do
|
||||||
case get_change(changeset, :musicbrainz_data) do
|
case get_change(changeset, :musicbrainz_data) do
|
||||||
nil ->
|
nil ->
|
||||||
@@ -180,18 +192,7 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
def attrs_from_release_group(release_group) do
|
def attrs_from_release_group(release_group) do
|
||||||
musicbrainz_id = release_group["id"]
|
musicbrainz_id = release_group["id"]
|
||||||
|
|
||||||
artists_attrs =
|
artists_attrs = parse_artists(release_group)
|
||||||
release_group
|
|
||||||
|> get_in(["artist-credit", Access.all(), "artist"])
|
|
||||||
|> Enum.map(fn artist ->
|
|
||||||
%{
|
|
||||||
name: artist["name"],
|
|
||||||
musicbrainz_id: artist["id"],
|
|
||||||
sort_name: artist["sort-name"],
|
|
||||||
disambiguation: artist["disambiguation"],
|
|
||||||
joinphrase: artist["joinphrase"]
|
|
||||||
}
|
|
||||||
end)
|
|
||||||
|
|
||||||
%{
|
%{
|
||||||
"musicbrainz_id" => musicbrainz_id,
|
"musicbrainz_id" => musicbrainz_id,
|
||||||
@@ -206,6 +207,20 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp parse_artists(musicbrainz_data) do
|
||||||
|
musicbrainz_data
|
||||||
|
|> get_in(["artist-credit", Access.all()])
|
||||||
|
|> Enum.map(fn artist_credit ->
|
||||||
|
%{
|
||||||
|
name: artist_credit["artist"]["name"],
|
||||||
|
musicbrainz_id: artist_credit["artist"]["id"],
|
||||||
|
sort_name: artist_credit["artist"]["sort-name"],
|
||||||
|
disambiguation: artist_credit["artist"]["disambiguation"],
|
||||||
|
joinphrase: artist_credit["joinphrase"]
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
defp parse_subtype("Album"), do: :album
|
defp parse_subtype("Album"), do: :album
|
||||||
defp parse_subtype("EP"), do: :ep
|
defp parse_subtype("EP"), do: :ep
|
||||||
defp parse_subtype("Live"), do: :live
|
defp parse_subtype("Live"), do: :live
|
||||||
|
|||||||
@@ -46,13 +46,17 @@ defmodule MusicLibraryWeb.RecordComponents do
|
|||||||
</div>
|
</div>
|
||||||
<div class="min-w-0 flex-auto">
|
<div class="min-w-0 flex-auto">
|
||||||
<h1 class="text-sm leading-6 text-zinc-700">
|
<h1 class="text-sm leading-6 text-zinc-700">
|
||||||
<.link
|
<span :for={artist <- record.artists}>
|
||||||
:for={artist <- record.artists}
|
<.link
|
||||||
class="text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300"
|
class="text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300"
|
||||||
navigate={~p"/artists/#{artist.musicbrainz_id}"}
|
navigate={~p"/artists/#{artist.musicbrainz_id}"}
|
||||||
>
|
>
|
||||||
{artist.name}
|
{artist.name}
|
||||||
</.link>
|
</.link>
|
||||||
|
<span class="text-xs leading-5 text-zinc-500 dark:text-zinc-400">
|
||||||
|
{artist.joinphrase}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
</h1>
|
</h1>
|
||||||
<h2 class="mt-1 flex font-semibold text-sm sm:text-base leading-5 text-zinc-700 dark:text-zinc-300 text-wrap">
|
<h2 class="mt-1 flex font-semibold text-sm sm:text-base leading-5 text-zinc-700 dark:text-zinc-300 text-wrap">
|
||||||
{record.title}
|
{record.title}
|
||||||
|
|||||||
@@ -11,13 +11,17 @@
|
|||||||
<div class="mt-4 md:mt-0 flex justify-between">
|
<div class="mt-4 md:mt-0 flex justify-between">
|
||||||
<div>
|
<div>
|
||||||
<h1 class="text-base font-medium leading-6 text-zinc-700">
|
<h1 class="text-base font-medium leading-6 text-zinc-700">
|
||||||
<.link
|
<span :for={artist <- @record.artists}>
|
||||||
:for={artist <- @record.artists}
|
<.link
|
||||||
class="text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300"
|
class="text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300"
|
||||||
navigate={~p"/artists/#{artist.musicbrainz_id}"}
|
navigate={~p"/artists/#{artist.musicbrainz_id}"}
|
||||||
>
|
>
|
||||||
{artist.name}
|
{artist.name}
|
||||||
</.link>
|
</.link>
|
||||||
|
<span class="text-sm leading-5 text-zinc-500 dark:text-zinc-400">
|
||||||
|
{artist.joinphrase}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
</h1>
|
</h1>
|
||||||
<h2 class="mt-1 flex font-semibold text-base sm:text-lg leading-5 text-zinc-700 dark:text-zinc-300 text-wrap">
|
<h2 class="mt-1 flex font-semibold text-base sm:text-lg leading-5 text-zinc-700 dark:text-zinc-300 text-wrap">
|
||||||
{@record.title}
|
{@record.title}
|
||||||
|
|||||||
@@ -11,13 +11,17 @@
|
|||||||
<div class="mt-4 md:mt-0 flex justify-between">
|
<div class="mt-4 md:mt-0 flex justify-between">
|
||||||
<div>
|
<div>
|
||||||
<h1 class="text-base font-medium leading-6 text-zinc-700">
|
<h1 class="text-base font-medium leading-6 text-zinc-700">
|
||||||
<.link
|
<span :for={artist <- @record.artists}>
|
||||||
:for={artist <- @record.artists}
|
<.link
|
||||||
class="text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300"
|
class="text-zinc-700 hover:text-zinc-500 dark:text-zinc-400 dark:hover:text-zinc-300"
|
||||||
navigate={~p"/artists/#{artist.musicbrainz_id}"}
|
navigate={~p"/artists/#{artist.musicbrainz_id}"}
|
||||||
>
|
>
|
||||||
{artist.name}
|
{artist.name}
|
||||||
</.link>
|
</.link>
|
||||||
|
<span class="text-sm leading-5 text-zinc-500 dark:text-zinc-400">
|
||||||
|
{artist.joinphrase}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
</h1>
|
</h1>
|
||||||
<h2 class="mt-1 flex font-semibold text-base sm:text-lg leading-5 text-zinc-700 dark:text-zinc-300 text-wrap">
|
<h2 class="mt-1 flex font-semibold text-base sm:text-lg leading-5 text-zinc-700 dark:text-zinc-300 text-wrap">
|
||||||
{@record.title}
|
{@record.title}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ defmodule MusicLibrary.RecordsTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "refresh_musicbrainz_data/1" do
|
describe "refresh_musicbrainz_data/1" do
|
||||||
test "updates release_ids and included_release_group_ids" do
|
test "updates release_ids, included_release_group_ids, and artists" do
|
||||||
release_group_id = release_group_id(:marbles)
|
release_group_id = release_group_id(:marbles)
|
||||||
|
|
||||||
record =
|
record =
|
||||||
@@ -101,6 +101,8 @@ defmodule MusicLibrary.RecordsTest do
|
|||||||
|
|
||||||
assert record.release_ids !== updated_record.release_ids
|
assert record.release_ids !== updated_record.release_ids
|
||||||
assert record.included_release_group_ids !== updated_record.included_release_group_ids
|
assert record.included_release_group_ids !== updated_record.included_release_group_ids
|
||||||
|
assert record.artists !== updated_record.artists
|
||||||
|
assert updated_record.artists !== []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user