add unique constraint on songs changeset

This commit is contained in:
Berenice Medel 2021-11-19 13:01:15 -06:00
parent 023ddc2806
commit 11d5cd8cdd
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} -> {:error, failed_op, failed_val, _changes} ->
failed_op = failed_op =
case failed_op do case failed_op do
{:song, _number} -> :invalid_song {:song, _number} -> "Invalid song (#{failed_val.changes.title})"
:is_songs_count_updated? -> :invalid :is_songs_count_updated? -> :invalid
failed_op -> failed_op failed_op -> failed_op
end end

View file

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