From 014db1346ae8adcd69a7f39007efe136dd8291c9 Mon Sep 17 00:00:00 2001 From: Jeremy Brayton Date: Tue, 17 Jan 2023 17:43:01 -0500 Subject: [PATCH] #9 Artist show styling Show page first draft almost complete. TODOs: Add menu functionality from Livebeats. Investigate merging views. What we have here is technically functional and we can always hide the button. --- lib/beatseek/albums.ex | 16 ++++++++ lib/beatseek_web/live/artist_live/show.ex | 12 +++++- .../live/artist_live/show.html.heex | 37 +++++++++++++++++-- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/beatseek/albums.ex b/lib/beatseek/albums.ex index c333150..d4b45c2 100644 --- a/lib/beatseek/albums.ex +++ b/lib/beatseek/albums.ex @@ -6,6 +6,7 @@ defmodule Beatseek.Albums do import Ecto.Query, warn: false alias Beatseek.Repo + alias Beatseek.Artists.Artist alias Beatseek.Albums.Album @doc """ @@ -23,6 +24,21 @@ defmodule Beatseek.Albums do |> Repo.preload(:artist) end + @doc """ + Returns the list of albums by artist. + + ## Examples + + iex> list_albums_by_artist() + [%Album{}, ...] + + """ + def list_albums_by_artist(artist) do + Album + |> where([album], album.artist_id == ^artist.id) + |> Repo.all() + end + @doc """ Gets a single album. diff --git a/lib/beatseek_web/live/artist_live/show.ex b/lib/beatseek_web/live/artist_live/show.ex index eea212b..a156ca6 100644 --- a/lib/beatseek_web/live/artist_live/show.ex +++ b/lib/beatseek_web/live/artist_live/show.ex @@ -2,6 +2,7 @@ defmodule BeatseekWeb.ArtistLive.Show do use BeatseekWeb, :live_view alias Beatseek.Artists + alias Beatseek.Albums @impl true def mount(_params, _session, socket) do @@ -10,12 +11,21 @@ defmodule BeatseekWeb.ArtistLive.Show do @impl true def handle_params(%{"id" => id}, _, socket) do + artist = Artists.get_artist!(id) + albums = list_albums_by_artist(artist) + {:noreply, socket |> assign(:page_title, page_title(socket.assigns.live_action)) - |> assign(:artist, Artists.get_artist!(id))} + |> assign(:artist, artist) + |> assign(:albums, albums) + |> assign(:album_count, Enum.count(albums))} end defp page_title(:show), do: "Show Artist" defp page_title(:edit), do: "Edit Artist" + + defp list_albums_by_artist(artist) do + Albums.list_albums_by_artist(artist) + end end diff --git a/lib/beatseek_web/live/artist_live/show.html.heex b/lib/beatseek_web/live/artist_live/show.html.heex index 77e32f8..22d1902 100644 --- a/lib/beatseek_web/live/artist_live/show.html.heex +++ b/lib/beatseek_web/live/artist_live/show.html.heex @@ -1,7 +1,36 @@
<.back navigate={~p"/artists"}> - <.header> - <%= @artist.name %> - <:subtitle>This is a artist record from your database. - +
+
+
+
<%= @artist.name %>
+ + <%!-- + --%> +
+
<%= @album_count %> albums
+
+
+ +
+
<%= album.name %>
+
+ <%= album.genre %> ยท <%= album.year %> +
+
+
+