Store artists when importing a record

This commit is contained in:
Claudio Ortolina
2024-09-24 09:40:52 +01:00
parent 089e61a574
commit bcb70c9e1b
3 changed files with 28 additions and 1 deletions
+8
View File
@@ -27,9 +27,17 @@ defmodule MusicLibrary.Records.Record do
def changeset(record, attrs) do
record
|> cast(attrs, [:type, :title, :musicbrainz_id, :year, :genres, :image_url, :image_data])
|> cast_embed(:artists, with: &artist_changeset/2)
|> validate_required([:type, :title, :musicbrainz_id, :year, :genres])
end
@doc false
def artist_changeset(artist, attrs) do
artist
|> cast(attrs, [:name, :sort_name, :disambiguation, :musicbrainz_id])
|> validate_required([:name, :sort_name, :musicbrainz_id])
end
def add_artists(record, artists_attrs) do
record
|> change()
@@ -106,6 +106,8 @@ defmodule MusicLibraryWeb.RecordLive.SearchComponent 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
+18 -1
View File
@@ -34,7 +34,15 @@ defmodule MusicLibrary.RecordsTest do
image_url: "some image url",
year: 42,
musicbrainz_id: "7488a646-e31f-11e4-aace-600308960662",
genres: ["option1", "option2"]
genres: ["option1", "option2"],
artists: [
%{
name: "some artist",
sort_name: "some artist",
disambiguation: "some artist",
musicbrainz_id: "7488a646-e31f-11e4-aace-600308960663"
}
]
}
assert {:ok, %Record{} = record} = Records.create_record(valid_attrs)
@@ -44,6 +52,15 @@ defmodule MusicLibrary.RecordsTest do
assert record.year == 42
assert record.musicbrainz_id == "7488a646-e31f-11e4-aace-600308960662"
assert record.genres == ["option1", "option2"]
assert [
%Record.Artist{
name: "some artist",
sort_name: "some artist",
disambiguation: "some artist",
musicbrainz_id: "7488a646-e31f-11e4-aace-600308960663"
}
] = record.artists
end
test "create_record/1 with invalid data returns error changeset" do