Merge pull request #7 from chrismccord/fix/avoid_raise_error_on_duplicated_song

Handle unique song title constraint error on upload
This commit is contained in:
Chris McCord 2021-11-22 09:58:02 -05:00 committed by GitHub
commit 632b211c44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View file

@ -196,7 +196,7 @@ defmodule LiveBeats.MediaLibrary do
{:error, failed_op, failed_val, _changes} ->
failed_op =
case failed_op do
{:song, _number} -> :invalid_song
{:song, _number} -> "Invalid song (#{failed_val.changes.title})"
:is_songs_count_updated? -> :invalid
failed_op -> failed_op
end

View file

@ -34,6 +34,10 @@ defmodule LiveBeats.MediaLibrary.Song do
song
|> cast(attrs, [:album_artist, :artist, :title, :attribution, :date_recorded, :date_released])
|> validate_required([:artist, :title])
|> unique_constraint(:title,
message: "is a duplicated from another song",
name: "songs_user_id_title_artist_index"
)
end
def put_user(%Ecto.Changeset{} = changeset, %Accounts.User{} = user) do