From 64ea8fe3faa261a28fa99b6a725bbccf2ee854ab Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sat, 28 Sep 2024 20:23:22 +0100 Subject: [PATCH] Extend import to support choosing format --- lib/music_library/records.ex | 10 ++++++---- lib/music_library/records/record.ex | 6 +++++- .../live/record_live/import_component.ex | 13 ++++++++++++- lib/music_library_web/live/record_live/index.ex | 4 ++-- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index e98632ec..1c2b8873 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -60,17 +60,18 @@ defmodule MusicLibrary.Records do MusicBrainz.search_release_group(query, limit: limit, offset: offset) end - def import_from_musicbrainz(musicbrainz_id) do - with {:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id), + def import_from_musicbrainz(musicbrainz_id, opts \\ []) do + with format = Keyword.get(opts, :format, "cd"), + {:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id), {:ok, image_data} <- MusicBrainz.get_cover_art(musicbrainz_id), - record_params = build_record_params(release_group, image_data) do + record_params = build_record_params(release_group, image_data, format) do create_record(record_params) else error -> error end end - defp build_record_params(release_group, image_data) do + defp build_record_params(release_group, image_data, format) do musicbrainz_id = release_group["id"] artists_attrs = @@ -91,6 +92,7 @@ defmodule MusicLibrary.Records do "artists" => artists_attrs, "year" => parse_year(release_group["first-release-date"]), "type" => parse_subtype(release_group["primary-type"]), + "format" => format, "genres" => Enum.map(release_group["genres"], fn g -> g["name"] end), "image_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front", "image_data" => image_data diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index bd58097a..5534038b 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -2,11 +2,13 @@ defmodule MusicLibrary.Records.Record do use Ecto.Schema import Ecto.Changeset + @formats [:cd, :vinyl, :blu_ray, :dvd, :multi] + @primary_key {:id, :binary_id, autogenerate: true} @foreign_key_type :binary_id schema "records" do field :type, Ecto.Enum, values: [:album, :ep, :live, :compilation, :single, :other] - field :format, Ecto.Enum, values: [:cd, :vinyl, :blu_ray, :dvd, :multi] + field :format, Ecto.Enum, values: @formats field :title, :string field :image_url, :string field :image_data, :binary @@ -58,6 +60,8 @@ defmodule MusicLibrary.Records.Record do change(record, image_data: image_data) end + def formats, do: @formats + def format_short_label(:cd), do: "CD" def format_short_label(:vinyl), do: "V" def format_short_label(:blu_ray), do: "BR" diff --git a/lib/music_library_web/live/record_live/import_component.ex b/lib/music_library_web/live/record_live/import_component.ex index 4fc50276..b8c9eda5 100644 --- a/lib/music_library_web/live/record_live/import_component.ex +++ b/lib/music_library_web/live/record_live/import_component.ex @@ -38,6 +38,7 @@ defmodule MusicLibraryWeb.RecordLive.ImportComponent do

+ <.type_badge type={@release_group.type} /> <%= @release_group.title %> @@ -47,8 +48,18 @@ defmodule MusicLibraryWeb.RecordLive.ImportComponent do

<%= @release_group.artists %>

+ """ diff --git a/lib/music_library_web/live/record_live/index.ex b/lib/music_library_web/live/record_live/index.ex index ef05d721..bca77147 100644 --- a/lib/music_library_web/live/record_live/index.ex +++ b/lib/music_library_web/live/record_live/index.ex @@ -95,8 +95,8 @@ defmodule MusicLibraryWeb.RecordLive.Index do {:noreply, push_patch(socket, to: ~s"/records?#{qs}")} end - def handle_event("import", %{"id" => musicbrainz_id}, socket) do - case Records.import_from_musicbrainz(musicbrainz_id) do + def handle_event("import", %{"id" => musicbrainz_id, "format" => format}, socket) do + case Records.import_from_musicbrainz(musicbrainz_id, format: format) do {:ok, record} -> notify_parent({:saved, record})