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 @@