From 18df3683fcc59a48548b4e33e529b43dafd5c455 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 2 Dec 2024 17:35:47 +0000 Subject: [PATCH] Render a minimal artist page --- assets/css/app.css | 8 ++++++++ lib/music_library/records.ex | 14 +++++++------- lib/music_library_web/live/artist_live/show.ex | 4 +++- .../live/artist_live/show.html.heex | 4 ++-- test/last_fm/artist_test.exs | 2 +- test/music_library/records_test.exs | 4 ++-- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/assets/css/app.css b/assets/css/app.css index 378c8f90..ba330741 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -3,3 +3,11 @@ @import "tailwindcss/utilities"; /* This file is for your main application CSS */ + +/* The bio html comes directly from the Last.fm api, +* so there's no way to inject Tailwind classes in the elements. +* To work around that, we set a class to the bio container snd style its children. +*/ +.bio a { + @apply font-semibold text-zinc-700 hover:text-zinc-500 dark:text-zinc-300 dark:hover:text-zinc-200; +} diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 83bc8372..560ad63f 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -90,14 +90,14 @@ defmodule MusicLibrary.Records do def get_record!(id), do: Repo.get!(Record, id) - def get_artist(musicbrainz_id) do + def get_artist!(musicbrainz_id) do q = from ar in ArtistRecord, where: ar.musicbrainz_id == ^musicbrainz_id, limit: 1, select: ar.artist - Repo.one(q) + Repo.one!(q) end def get_artist_records(musicbrainz_id) do @@ -110,6 +110,10 @@ defmodule MusicLibrary.Records do Repo.all(q) end + def get_artist_info(musicbrainz_id) do + last_fm().get_artist_info(musicbrainz_id, last_fm_api_key()) + end + def get_cover(id) do q = from r in Record, @@ -186,10 +190,6 @@ defmodule MusicLibrary.Records do end end - def get_artist(musicbrainz_id) do - last_fm().get_artist_info(musicbrainz_id, last_fm_api_key()) - end - defp build_record_attrs(release_group, attrs) do release_group |> Record.attrs_from_release_group() @@ -226,6 +226,6 @@ defmodule MusicLibrary.Records do defp last_fm_api_key do Application.get_env(:music_library, LastFm) - |> Keyword.fetch!(:api_key) + |> Keyword.fetch!(:api_key) end end diff --git a/lib/music_library_web/live/artist_live/show.ex b/lib/music_library_web/live/artist_live/show.ex index 8bb98f10..5cc8230e 100644 --- a/lib/music_library_web/live/artist_live/show.ex +++ b/lib/music_library_web/live/artist_live/show.ex @@ -10,12 +10,14 @@ defmodule MusicLibraryWeb.ArtistLive.Show do @impl true def handle_params(%{"musicbrainz_id" => musicbrainz_id}, _, socket) do - {:ok, artist} = Records.get_artist(musicbrainz_id) + artist = Records.get_artist!(musicbrainz_id) + {:ok, artist_info} = Records.get_artist_info(musicbrainz_id) {:noreply, socket |> assign(:nav_section, :artists) |> assign(:artist, artist) + |> assign(:artist_info, artist_info) |> assign(:page_title, page_title(socket.assigns.live_action, artist))} end diff --git a/lib/music_library_web/live/artist_live/show.html.heex b/lib/music_library_web/live/artist_live/show.html.heex index 206e477a..70f94f4a 100644 --- a/lib/music_library_web/live/artist_live/show.html.heex +++ b/lib/music_library_web/live/artist_live/show.html.heex @@ -2,7 +2,7 @@

<%= @artist.name %>

-

- <%= raw(@artist.bio) || gettext("Biography not available") %> +

+ <%= (@artist_info.bio || gettext("Biography not available")) |> raw() %>

diff --git a/test/last_fm/artist_test.exs b/test/last_fm/artist_test.exs index 77569dcd..55bb37f5 100644 --- a/test/last_fm/artist_test.exs +++ b/test/last_fm/artist_test.exs @@ -1,4 +1,4 @@ -defmodule LastFm.TrackTest do +defmodule LastFm.ArtistTest do use ExUnit.Case, async: true @api_response_path Path.expand("../support/fixtures/artist.getinfo.json", __DIR__) diff --git a/test/music_library/records_test.exs b/test/music_library/records_test.exs index fdcc5ef2..bbca882f 100644 --- a/test/music_library/records_test.exs +++ b/test/music_library/records_test.exs @@ -171,9 +171,9 @@ defmodule MusicLibrary.RecordsTest do describe "get_artist/1" do test "it returns records with essential data" do record = record_fixture() - expected = record.artists |> hd() + [expected] = record.artists - artist = Records.get_artist(expected.musicbrainz_id) + artist = Records.get_artist!(expected.musicbrainz_id) assert expected == artist end