diff --git a/lib/music_library/records/record.ex b/lib/music_library/records/record.ex index 370c4cf2..370b7b88 100644 --- a/lib/music_library/records/record.ex +++ b/lib/music_library/records/record.ex @@ -48,6 +48,7 @@ defmodule MusicLibrary.Records.Record do |> cast_embed(:artists, with: &artist_changeset/2) |> generate_cover_hash() |> validate_required([:type, :title, :musicbrainz_id, :release, :genres]) + |> unique_constraint(:musicbrainz_id, name: "records_musicbrainz_id_format_index") end @doc false diff --git a/lib/music_library_web/live/wishlist_live/index.ex b/lib/music_library_web/live/wishlist_live/index.ex index d6359bc0..e26ce7c0 100644 --- a/lib/music_library_web/live/wishlist_live/index.ex +++ b/lib/music_library_web/live/wishlist_live/index.ex @@ -107,7 +107,13 @@ defmodule MusicLibraryWeb.WishlistLive.Index do |> push_navigate(to: ~p"/wishlist/#{record.id}")} {:error, %Ecto.Changeset{} = changeset} -> - {:noreply, assign(socket, form: to_form(changeset))} + {:noreply, + socket + |> put_flash( + :error, + gettext("Error importing record") <> "," <> inspect(changeset.errors) + ) + |> push_patch(to: ~p"/wishlist")} {:error, reason} -> {:noreply, diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 0038c785..7b57ef9a 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -82,7 +82,8 @@ msgid "Edit" msgstr "" #: lib/music_library_web/live/record_live/index.ex:118 -#: lib/music_library_web/live/wishlist_live/index.ex:115 +#: lib/music_library_web/live/wishlist_live/index.ex:114 +#: lib/music_library_web/live/wishlist_live/index.ex:121 #, elixir-autogen, elixir-format msgid "Error importing record" msgstr "" @@ -213,7 +214,7 @@ msgid "Record imported successfully" msgstr "" #: lib/music_library_web/live/record_live/form_component.ex:117 -#: lib/music_library_web/live/wishlist_live/index.ex:128 +#: lib/music_library_web/live/wishlist_live/index.ex:134 #, elixir-autogen, elixir-format msgid "Record updated successfully" msgstr "" diff --git a/priv/repo/migrations/20241105113115_add_unique_index_to_records.exs b/priv/repo/migrations/20241105113115_add_unique_index_to_records.exs new file mode 100644 index 00000000..51e24047 --- /dev/null +++ b/priv/repo/migrations/20241105113115_add_unique_index_to_records.exs @@ -0,0 +1,7 @@ +defmodule MusicLibrary.Repo.Migrations.AddUniqueIndexToRecords do + use Ecto.Migration + + def change do + create unique_index(:records, [:musicbrainz_id, :format]) + end +end