Use a map instead of 4 arguments
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
defmodule MusicLibrary.Records do
|
defmodule MusicLibrary.Records do
|
||||||
|
@moduledoc """
|
||||||
|
Provides function to work with records _irrespectively_ of their status as port of the collection or of the wishlist.
|
||||||
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias MusicLibrary.Repo
|
alias MusicLibrary.Repo
|
||||||
|
|
||||||
@@ -97,8 +101,13 @@ defmodule MusicLibrary.Records do
|
|||||||
purchased_at = Keyword.get(opts, :purchased_at),
|
purchased_at = Keyword.get(opts, :purchased_at),
|
||||||
{:ok, release_group} <- musicbrainz().get_release_group(musicbrainz_id),
|
{:ok, release_group} <- musicbrainz().get_release_group(musicbrainz_id),
|
||||||
{:ok, cover_data} <- musicbrainz().get_cover_art({:musicbrainz_id, musicbrainz_id}),
|
{:ok, cover_data} <- musicbrainz().get_cover_art({:musicbrainz_id, musicbrainz_id}),
|
||||||
record_params = build_record_params(release_group, cover_data, format, purchased_at) do
|
record_attrs =
|
||||||
create_record(record_params)
|
build_record_attrs(release_group, %{
|
||||||
|
"cover_data" => cover_data,
|
||||||
|
"format" => format,
|
||||||
|
"purchased_at" => purchased_at
|
||||||
|
}) do
|
||||||
|
create_record(record_attrs)
|
||||||
else
|
else
|
||||||
error -> error
|
error -> error
|
||||||
end
|
end
|
||||||
@@ -115,7 +124,7 @@ defmodule MusicLibrary.Records do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp build_record_params(release_group, cover_data, format, purchased_at) do
|
defp build_record_attrs(release_group, attrs) do
|
||||||
musicbrainz_id = release_group["id"]
|
musicbrainz_id = release_group["id"]
|
||||||
|
|
||||||
artists_attrs =
|
artists_attrs =
|
||||||
@@ -130,19 +139,19 @@ defmodule MusicLibrary.Records do
|
|||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
%{
|
Map.merge(
|
||||||
"musicbrainz_id" => musicbrainz_id,
|
attrs,
|
||||||
"musicbrainz_data" => release_group,
|
%{
|
||||||
"title" => release_group["title"],
|
"musicbrainz_id" => musicbrainz_id,
|
||||||
"artists" => artists_attrs,
|
"musicbrainz_data" => release_group,
|
||||||
"release" => release_group["first-release-date"],
|
"title" => release_group["title"],
|
||||||
"type" => parse_subtype(release_group["primary-type"]),
|
"artists" => artists_attrs,
|
||||||
"format" => format,
|
"release" => release_group["first-release-date"],
|
||||||
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
|
"type" => parse_subtype(release_group["primary-type"]),
|
||||||
"cover_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
|
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
|
||||||
"cover_data" => cover_data,
|
"cover_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front"
|
||||||
"purchased_at" => purchased_at
|
}
|
||||||
}
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp parse_subtype("Album"), do: :album
|
defp parse_subtype("Album"), do: :album
|
||||||
|
|||||||
Reference in New Issue
Block a user