Insert default purchased_at date when importing a record
This commit is contained in:
@@ -123,16 +123,17 @@ defmodule MusicLibrary.Records do
|
|||||||
|
|
||||||
def import_from_musicbrainz(musicbrainz_id, opts \\ []) do
|
def import_from_musicbrainz(musicbrainz_id, opts \\ []) do
|
||||||
with format = Keyword.get(opts, :format, "cd"),
|
with format = Keyword.get(opts, :format, "cd"),
|
||||||
|
purchased_at = Keyword.get(opts, :purchased_at, DateTime.utc_now()),
|
||||||
{:ok, release_group} <- musicbrainz().get_release_group(musicbrainz_id),
|
{:ok, release_group} <- musicbrainz().get_release_group(musicbrainz_id),
|
||||||
{:ok, cover_data} <- musicbrainz().get_cover_art(musicbrainz_id),
|
{:ok, cover_data} <- musicbrainz().get_cover_art(musicbrainz_id),
|
||||||
record_params = build_record_params(release_group, cover_data, format) do
|
record_params = build_record_params(release_group, cover_data, format, purchased_at) do
|
||||||
create_record(record_params)
|
create_record(record_params)
|
||||||
else
|
else
|
||||||
error -> error
|
error -> error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp build_record_params(release_group, cover_data, format) do
|
defp build_record_params(release_group, cover_data, format, purchased_at) do
|
||||||
musicbrainz_id = release_group["id"]
|
musicbrainz_id = release_group["id"]
|
||||||
|
|
||||||
artists_attrs =
|
artists_attrs =
|
||||||
@@ -157,7 +158,8 @@ defmodule MusicLibrary.Records do
|
|||||||
"format" => format,
|
"format" => format,
|
||||||
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
|
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
|
||||||
"cover_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
|
"cover_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
|
||||||
"cover_data" => cover_data
|
"cover_data" => cover_data,
|
||||||
|
"purchased_at" => purchased_at
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
field :musicbrainz_data, :map
|
field :musicbrainz_data, :map
|
||||||
field :genres, {:array, :string}
|
field :genres, {:array, :string}
|
||||||
field :release, :string
|
field :release, :string
|
||||||
|
field :purchased_at, :utc_datetime
|
||||||
|
|
||||||
embeds_many :artists, Artist do
|
embeds_many :artists, Artist do
|
||||||
field :name, :string
|
field :name, :string
|
||||||
@@ -41,7 +42,8 @@ defmodule MusicLibrary.Records.Record do
|
|||||||
:release,
|
:release,
|
||||||
:genres,
|
:genres,
|
||||||
:cover_url,
|
:cover_url,
|
||||||
:cover_data
|
:cover_data,
|
||||||
|
:purchased_at
|
||||||
])
|
])
|
||||||
|> cast_embed(:artists, with: &artist_changeset/2)
|
|> cast_embed(:artists, with: &artist_changeset/2)
|
||||||
|> generate_cover_hash()
|
|> generate_cover_hash()
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
defmodule MusicLibrary.Repo.Migrations.AddPurchasedAtToRecords do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table(:records) do
|
||||||
|
add :purchased_at, :utc_datetime
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -399,6 +399,10 @@ defmodule MusicLibraryWeb.RecordIndexTest do
|
|||||||
|
|
||||||
assert record.cover_data == cover_data
|
assert record.cover_data == cover_data
|
||||||
|
|
||||||
|
assert record.inserted_at !== nil
|
||||||
|
assert record.updated_at !== nil
|
||||||
|
assert record.purchased_at !== nil
|
||||||
|
|
||||||
[marillion] = record.artists
|
[marillion] = record.artists
|
||||||
|
|
||||||
assert %MusicLibrary.Records.Record.Artist{
|
assert %MusicLibrary.Records.Record.Artist{
|
||||||
|
|||||||
Reference in New Issue
Block a user