From c2ccb7dde233e147117b471b6b67fa6341091f5c Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 10 Dec 2024 14:57:28 +0300 Subject: [PATCH] Add LastFm.Feed.all_artists/0 and LastFm.Feed.all_albums/0 --- lib/last_fm/feed.ex | 26 ++++++++++++++++++++++++++ test/last_fm/feed_test.exs | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/last_fm/feed.ex b/lib/last_fm/feed.ex index 5d0361ae..7d658239 100644 --- a/lib/last_fm/feed.ex +++ b/lib/last_fm/feed.ex @@ -37,6 +37,32 @@ defmodule LastFm.Feed do :ets.select_reverse(__MODULE__, m) end + @spec all_artists() :: [LastFm.Artist.t()] + def all_artists do + m = [ + { + {:"$1", %{artist: :"$2"}}, + [], + [:"$2"] + } + ] + + :ets.select_reverse(__MODULE__, m) |> Enum.uniq() + end + + @spec all_albums() :: [LastFm.Album.t()] + def all_albums do + m = [ + { + {:"$1", %{album: :"$2"}}, + [], + [:"$2"] + } + ] + + :ets.select_reverse(__MODULE__, m) |> Enum.uniq() + end + @spec subscribe() :: :ok def subscribe do Phoenix.PubSub.subscribe(LastFm.PubSub, "feed:update") diff --git a/test/last_fm/feed_test.exs b/test/last_fm/feed_test.exs index 819df384..2a7a6864 100644 --- a/test/last_fm/feed_test.exs +++ b/test/last_fm/feed_test.exs @@ -46,5 +46,15 @@ defmodule LastFm.FeedTest do :ok = Feed.update([@track_two, @track_one]) assert [@track_two, @track_one] == Feed.all_tracks() end + + test "it returns artists in descending order of scrobble" do + :ok = Feed.update([@track_two, @track_one]) + assert [@track_one.artist] == Feed.all_artists() + end + + test "it returns albums in descending order of scrobble" do + :ok = Feed.update([@track_two, @track_one]) + assert [@track_one.album] == Feed.all_albums() + end end end