From dbdb698ef3321899761da1a3fae8c69c99c732e5 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Thu, 14 Nov 2024 15:16:19 +0000 Subject: [PATCH] Display count of child release groups --- lib/music_library/records.ex | 1 + lib/music_library/records/record.ex | 22 ++++++++++++++++++- .../live/record_live/index.html.heex | 12 ++++++++++ .../live/record_live/show.ex | 4 +++- priv/gettext/default.pot | 16 +++++++------- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 6fba134c..41aed6bc 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -18,6 +18,7 @@ defmodule MusicLibrary.Records do :release, :genres, :musicbrainz_id, + :musicbrainz_data, :cover_hash ] end diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index f8e2b757..cac951ba 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -15,7 +15,7 @@ defmodule MusicLibrary.Records.Record do field :cover_data, :binary field :cover_hash, :string field :musicbrainz_id, Ecto.UUID - field :musicbrainz_data, :map + field :musicbrainz_data, :map, default: %{} field :genres, {:array, :string} field :release, :string field :purchased_at, :utc_datetime @@ -53,6 +53,26 @@ defmodule MusicLibrary.Records.Record do |> unique_constraint(:musicbrainz_id, name: "records_musicbrainz_id_format_index") end + def child_release_groups(record) do + record.musicbrainz_data + |> Map.get("relations", []) + |> Enum.filter(fn relation -> + relation["target-type"] == "release_group" and + relation["type"] == "included in" and + relation["direction"] == "backward" + end) + end + + def child_release_groups_count(record) do + record.musicbrainz_data + |> Map.get("relations", []) + |> Enum.count(fn relation -> + relation["target-type"] == "release_group" and + relation["type"] == "included in" and + relation["direction"] == "backward" + end) + end + @doc false def artist_changeset(artist, attrs) do artist diff --git a/lib/music_library_web/live/record_live/index.html.heex b/lib/music_library_web/live/record_live/index.html.heex index beff5490..09ec3e76 100644 --- a/lib/music_library_web/live/record_live/index.html.heex +++ b/lib/music_library_web/live/record_live/index.html.heex @@ -65,6 +65,12 @@ · <%= Records.Record.format_long_label(record.format) %> · <%= Records.Record.type_long_label( record.type ) %> + 0}> + · + + <%= Records.Record.child_release_groups_count(record) %> + +

@@ -75,6 +81,12 @@ <%= Records.Record.format_long_label(record.format) %> · <%= Records.Record.type_long_label( record.type ) %> + 0}> + · + + <%= Records.Record.child_release_groups_count(record) %> + +

diff --git a/lib/music_library_web/live/record_live/show.ex b/lib/music_library_web/live/record_live/show.ex index ec0563f4..31977938 100644 --- a/lib/music_library_web/live/record_live/show.ex +++ b/lib/music_library_web/live/record_live/show.ex @@ -26,11 +26,13 @@ defmodule MusicLibraryWeb.RecordLive.Show do @impl true def handle_params(%{"id" => id}, _, socket) do + record = Records.get_record!(id) + {:noreply, socket |> assign(:nav_section, :records) |> assign(:page_title, page_title(socket.assigns.live_action)) - |> assign(:record, Records.get_record!(id))} + |> assign(:record, record)} end @impl true diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 268b80f1..2bb3e783 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -16,7 +16,7 @@ msgstr "" msgid "Actions" msgstr "" -#: lib/music_library_web/live/record_live/index.html.heex:153 +#: lib/music_library_web/live/record_live/index.html.heex:165 #: lib/music_library_web/live/record_live/show.html.heex:9 #: lib/music_library_web/live/wishlist_live/index.html.heex:163 #: lib/music_library_web/live/wishlist_live/show.html.heex:9 @@ -61,7 +61,7 @@ msgstr "" msgid "Cover art" msgstr "" -#: lib/music_library_web/live/record_live/index.html.heex:155 +#: lib/music_library_web/live/record_live/index.html.heex:167 #: lib/music_library_web/live/record_live/show.html.heex:12 #: lib/music_library_web/live/wishlist_live/index.html.heex:165 #: lib/music_library_web/live/wishlist_live/show.html.heex:12 @@ -70,8 +70,8 @@ msgid "Delete" msgstr "" #: lib/music_library_web/live/record_live/index.ex:55 -#: lib/music_library_web/live/record_live/index.html.heex:144 -#: lib/music_library_web/live/record_live/show.ex:50 +#: lib/music_library_web/live/record_live/index.html.heex:156 +#: lib/music_library_web/live/record_live/show.ex:52 #: lib/music_library_web/live/record_live/show.html.heex:4 #: lib/music_library_web/live/wishlist_live/index.ex:55 #: lib/music_library_web/live/wishlist_live/index.html.heex:144 @@ -176,7 +176,7 @@ msgstr "" msgid "No results" msgstr "" -#: lib/music_library_web/live/record_live/index.html.heex:89 +#: lib/music_library_web/live/record_live/index.html.heex:101 #: lib/music_library_web/live/stats_live/index.html.heex:145 #: lib/music_library_web/live/wishlist_live/index.html.heex:89 #, elixir-autogen, elixir-format @@ -253,8 +253,8 @@ msgstr "" msgid "Search for records" msgstr "" -#: lib/music_library_web/live/record_live/index.html.heex:124 -#: lib/music_library_web/live/record_live/show.ex:49 +#: lib/music_library_web/live/record_live/index.html.heex:136 +#: lib/music_library_web/live/record_live/show.ex:51 #: lib/music_library_web/live/wishlist_live/index.html.heex:124 #: lib/music_library_web/live/wishlist_live/show.ex:49 #, elixir-autogen, elixir-format @@ -317,7 +317,7 @@ msgstr "" msgid "Updated at" msgstr "" -#: lib/music_library_web/live/record_live/index.html.heex:134 +#: lib/music_library_web/live/record_live/index.html.heex:146 #: lib/music_library_web/live/wishlist_live/index.html.heex:134 #, elixir-autogen, elixir-format msgid "View on MusicBrainz"