Extend import to support choosing format
This commit is contained in:
@@ -60,17 +60,18 @@ defmodule MusicLibrary.Records do
|
||||
MusicBrainz.search_release_group(query, limit: limit, offset: offset)
|
||||
end
|
||||
|
||||
def import_from_musicbrainz(musicbrainz_id) do
|
||||
with {:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id),
|
||||
def import_from_musicbrainz(musicbrainz_id, opts \\ []) do
|
||||
with format = Keyword.get(opts, :format, "cd"),
|
||||
{:ok, release_group} <- MusicBrainz.get_release_group(musicbrainz_id),
|
||||
{:ok, image_data} <- MusicBrainz.get_cover_art(musicbrainz_id),
|
||||
record_params = build_record_params(release_group, image_data) do
|
||||
record_params = build_record_params(release_group, image_data, format) do
|
||||
create_record(record_params)
|
||||
else
|
||||
error -> error
|
||||
end
|
||||
end
|
||||
|
||||
defp build_record_params(release_group, image_data) do
|
||||
defp build_record_params(release_group, image_data, format) do
|
||||
musicbrainz_id = release_group["id"]
|
||||
|
||||
artists_attrs =
|
||||
@@ -91,6 +92,7 @@ defmodule MusicLibrary.Records do
|
||||
"artists" => artists_attrs,
|
||||
"year" => parse_year(release_group["first-release-date"]),
|
||||
"type" => parse_subtype(release_group["primary-type"]),
|
||||
"format" => format,
|
||||
"genres" => Enum.map(release_group["genres"], fn g -> g["name"] end),
|
||||
"image_url" => "https://coverartarchive.org/release-group/#{musicbrainz_id}/front",
|
||||
"image_data" => image_data
|
||||
|
||||
@@ -2,11 +2,13 @@ defmodule MusicLibrary.Records.Record do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@formats [:cd, :vinyl, :blu_ray, :dvd, :multi]
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "records" do
|
||||
field :type, Ecto.Enum, values: [:album, :ep, :live, :compilation, :single, :other]
|
||||
field :format, Ecto.Enum, values: [:cd, :vinyl, :blu_ray, :dvd, :multi]
|
||||
field :format, Ecto.Enum, values: @formats
|
||||
field :title, :string
|
||||
field :image_url, :string
|
||||
field :image_data, :binary
|
||||
@@ -58,6 +60,8 @@ defmodule MusicLibrary.Records.Record do
|
||||
change(record, image_data: image_data)
|
||||
end
|
||||
|
||||
def formats, do: @formats
|
||||
|
||||
def format_short_label(:cd), do: "CD"
|
||||
def format_short_label(:vinyl), do: "V"
|
||||
def format_short_label(:blu_ray), do: "BR"
|
||||
|
||||
@@ -38,6 +38,7 @@ defmodule MusicLibraryWeb.RecordLive.ImportComponent do
|
||||
<div class="flex min-w-0 gap-x-4">
|
||||
<div class="min-w-0 flex-auto">
|
||||
<p class="text-sm font-semibold leading-6 text-gray-900">
|
||||
<.type_badge type={@release_group.type} />
|
||||
<%= @release_group.title %>
|
||||
|
||||
<span class="mt-1 text-xs leading-5 text-gray-500">
|
||||
@@ -47,8 +48,18 @@ defmodule MusicLibraryWeb.RecordLive.ImportComponent do
|
||||
<p class="mt-1 truncate text-xs leading-5 text-gray-500"><%= @release_group.artists %></p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hidden shrink-0 sm:flex sm:flex-col sm:items-end">
|
||||
<.type_badge type={@release_group.type} />
|
||||
<span class="isolate inline-flex rounded-md shadow-sm">
|
||||
<button
|
||||
:for={format <- Records.Record.formats()}
|
||||
phx-click={JS.push("import", value: %{id: @release_group.id, format: format})}
|
||||
type="button"
|
||||
class="relative -ml-px inline-flex items-center first:rounded-l-md last:rounded-r-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-10"
|
||||
>
|
||||
<%= Records.Record.format_short_label(format) %>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
"""
|
||||
|
||||
@@ -95,8 +95,8 @@ defmodule MusicLibraryWeb.RecordLive.Index do
|
||||
{:noreply, push_patch(socket, to: ~s"/records?#{qs}")}
|
||||
end
|
||||
|
||||
def handle_event("import", %{"id" => musicbrainz_id}, socket) do
|
||||
case Records.import_from_musicbrainz(musicbrainz_id) do
|
||||
def handle_event("import", %{"id" => musicbrainz_id, "format" => format}, socket) do
|
||||
case Records.import_from_musicbrainz(musicbrainz_id, format: format) do
|
||||
{:ok, record} ->
|
||||
notify_parent({:saved, record})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user