From 024d95da7c6be9c806239ab66f8c8a4af74a1c3c Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Wed, 2 Oct 2024 14:22:22 +0100 Subject: [PATCH] Use release instead of year --- lib/music_library/records.ex | 15 ++--------- lib/music_library/records/music_brainz.ex | 11 +------- lib/music_library/records/record.ex | 5 ++-- .../live/record_live/form_component.ex | 2 +- .../live/record_live/import_component.ex | 2 +- .../live/record_live/index.html.heex | 2 +- .../live/record_live/show.html.heex | 2 +- lib/obsidian/entry.ex | 2 +- lib/obsidian/parser.ex | 12 +++------ .../20241002130921_add_release_to_records.exs | 25 +++++++++++++++++++ .../live/record_live_test.exs | 2 +- test/obsidian/parser_test.exs | 4 +-- test/support/fixtures/records_fixtures.ex | 2 +- 13 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 priv/repo/migrations/20241002130921_add_release_to_records.exs diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 511256d2..4fadb529 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -4,7 +4,7 @@ defmodule MusicLibrary.Records do alias MusicLibrary.Records.{MusicBrainz, Record} - @fields [:id, :type, :format, :title, :year, :genres, :musicbrainz_id, :image_data_hash] + @fields [:id, :type, :format, :title, :release, :genres, :musicbrainz_id, :image_data_hash] def list_records(opts \\ []) do limit = Keyword.get(opts, :limit, 20) @@ -104,7 +104,7 @@ defmodule MusicLibrary.Records do "musicbrainz_id" => musicbrainz_id, "title" => release_group["title"], "artists" => artists_attrs, - "year" => parse_year(release_group["first-release-date"]), + "release" => 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), @@ -113,17 +113,6 @@ defmodule MusicLibrary.Records do } end - defp parse_year(iso_date) when is_binary(iso_date) do - case Date.from_iso8601(iso_date) do - {:ok, date} -> - date.year - - _error -> - {year, _rest} = Integer.parse(iso_date) - {:ok, year} - end - end - defp parse_subtype("Album"), do: :album defp parse_subtype("EP"), do: :ep defp parse_subtype("Live"), do: :live diff --git a/lib/music_library/records/music_brainz.ex b/lib/music_library/records/music_brainz.ex index 976c54c5..2fe9279b 100644 --- a/lib/music_library/records/music_brainz.ex +++ b/lib/music_library/records/music_brainz.ex @@ -173,7 +173,7 @@ defmodule MusicLibrary.Records.MusicBrainz do rg["artist-credit"] |> Enum.map(fn ac -> ac["artist"]["name"] end) |> Enum.join(", "), - year: parse_year(rg["first-release-date"]) + release: rg["first-release-date"] } end)} end @@ -231,15 +231,6 @@ defmodule MusicLibrary.Records.MusicBrainz do end end - defp parse_year(nil), do: "" - - defp parse_year(iso_date) do - case Date.from_iso8601(iso_date) do - {:ok, date} -> date.year - _error -> nil - end - end - defp parse_subtype("Album"), do: :album defp parse_subtype("EP"), do: :ep defp parse_subtype("Live"), do: :live diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index 0a6c437d..8f4fdbeb 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -16,6 +16,7 @@ defmodule MusicLibrary.Records.Record do field :year, :integer field :musicbrainz_id, Ecto.UUID field :genres, {:array, :string} + field :release, :string embeds_many :artists, Artist do field :name, :string @@ -35,14 +36,14 @@ defmodule MusicLibrary.Records.Record do :format, :title, :musicbrainz_id, - :year, + :release, :genres, :image_url, :image_data ]) |> cast_embed(:artists, with: &artist_changeset/2) |> generate_image_data_hash() - |> validate_required([:type, :title, :musicbrainz_id, :year, :genres]) + |> validate_required([:type, :title, :musicbrainz_id, :release, :genres]) end @doc false diff --git a/lib/music_library_web/live/record_live/form_component.ex b/lib/music_library_web/live/record_live/form_component.ex index c473e544..16092458 100644 --- a/lib/music_library_web/live/record_live/form_component.ex +++ b/lib/music_library_web/live/record_live/form_component.ex @@ -33,7 +33,7 @@ defmodule MusicLibraryWeb.RecordLive.FormComponent do prompt="Choose a value" options={Ecto.Enum.values(MusicLibrary.Records.Record, :type)} /> - <.input field={@form[:year]} type="number" label="Year" /> + <.input field={@form[:release]} type="text" label="Release" />
<.label for={@uploads.image_data.ref}> Cover art 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 75022e86..f1ae4fe7 100644 --- a/lib/music_library_web/live/record_live/import_component.ex +++ b/lib/music_library_web/live/record_live/import_component.ex @@ -41,7 +41,7 @@ defmodule MusicLibraryWeb.RecordLive.ImportComponent do <%= @release_group.title %> - <%= @release_group.year %> + <%= @release_group.release %>

<%= @release_group.artists %>

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 1f239ed6..b3c19ea4 100644 --- a/lib/music_library_web/live/record_live/index.html.heex +++ b/lib/music_library_web/live/record_live/index.html.heex @@ -38,7 +38,7 @@ <:col :let={{_id, record}} label="Record">

<%= Enum.map(record.artists, fn a -> a.name end) %>

<%= record.title %>

-

<%= record.year %>

+

<%= record.release %>

<:action :let={{_id, record}}>
diff --git a/lib/music_library_web/live/record_live/show.html.heex b/lib/music_library_web/live/record_live/show.html.heex index dc4de138..27a4a396 100644 --- a/lib/music_library_web/live/record_live/show.html.heex +++ b/lib/music_library_web/live/record_live/show.html.heex @@ -34,7 +34,7 @@ <%= @record.musicbrainz_id %> - <:item title="Year"><%= @record.year %> + <:item title="Release"><%= @record.release %> <:item title="Genres"> maybe_parse_year(), + release: meta["year"] |> parse_release(), image_url: meta["image"], genres: meta["genres"] }} @@ -41,11 +41,7 @@ defmodule Obsidian.Parser do defp parse_subtype("Single"), do: :single defp parse_subtype(_), do: :other - defp maybe_parse_year(nil), do: nil - defp maybe_parse_year(year) when is_integer(year), do: year - - defp maybe_parse_year(year) when is_binary(year) do - {integer, _remainder} = Integer.parse(year, 10) - integer - end + defp parse_release(nil), do: nil + defp parse_release(year) when is_integer(year), do: Integer.to_string(year) + defp parse_release(year) when is_binary(year), do: year end diff --git a/priv/repo/migrations/20241002130921_add_release_to_records.exs b/priv/repo/migrations/20241002130921_add_release_to_records.exs new file mode 100644 index 00000000..b0a508f2 --- /dev/null +++ b/priv/repo/migrations/20241002130921_add_release_to_records.exs @@ -0,0 +1,25 @@ +defmodule MusicLibrary.Repo.Migrations.AddReleaseToRecords do + use Ecto.Migration + import Ecto.Query + + def up do + alter table(:records) do + add :release, :string + end + + flush() + + query = + from(r in MusicLibrary.Records.Record, + update: [set: [release: r.year]] + ) + + MusicLibrary.Repo.update_all(query, []) + end + + def down do + alter table(:records) do + remove :release + end + end +end diff --git a/test/music_library_web/live/record_live_test.exs b/test/music_library_web/live/record_live_test.exs index 4513d5cc..06a0fa90 100644 --- a/test/music_library_web/live/record_live_test.exs +++ b/test/music_library_web/live/record_live_test.exs @@ -33,7 +33,7 @@ defmodule MusicLibraryWeb.RecordLiveTest do for record <- present do assert html =~ escape(record.title) - assert html =~ to_string(record.year) + assert html =~ to_string(record.release) for artist <- record.artists do assert html =~ escape(artist["name"]) diff --git a/test/obsidian/parser_test.exs b/test/obsidian/parser_test.exs index 98d01f93..3c5cf470 100644 --- a/test/obsidian/parser_test.exs +++ b/test/obsidian/parser_test.exs @@ -14,7 +14,7 @@ defmodule Obsidian.ParserTest do type: :album, musicbrainz_id: "20790e26-98e4-3ad3-a67f-b674758b942d", title: "Marbles", - year: 2004, + release: "2004", image_url: "https://coverartarchive.org/release-group/20790e26-98e4-3ad3-a67f-b674758b942d/front", genres: [ @@ -41,7 +41,7 @@ defmodule Obsidian.ParserTest do musicbrainz_id: "950092d6-45f6-4269-87da-99a9ff2fcc52", title: "Guardians of the Galaxy: Awesome Mix, Vol. 1", type: :album, - year: 2014 + release: "2014" }} end end diff --git a/test/support/fixtures/records_fixtures.ex b/test/support/fixtures/records_fixtures.ex index 25063f59..30c399a8 100644 --- a/test/support/fixtures/records_fixtures.ex +++ b/test/support/fixtures/records_fixtures.ex @@ -44,7 +44,7 @@ defmodule MusicLibrary.RecordsFixtures do title: Enum.random(@titles), type: :album, format: :cd, - year: Enum.random(1969..2024) + release: Enum.random(1969..2024) |> Integer.to_string() }) |> MusicLibrary.Records.create_record()