From ec10447ca0ab476204481ef475fb3ed1687c63da Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 10 Mar 2025 17:41:17 +0000 Subject: [PATCH] Extend Last.fm Feed album/artist functions --- lib/last_fm/feed.ex | 12 ++++++------ test/last_fm/feed_test.exs | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/last_fm/feed.ex b/lib/last_fm/feed.ex index 7d658239..a1165dee 100644 --- a/lib/last_fm/feed.ex +++ b/lib/last_fm/feed.ex @@ -41,26 +41,26 @@ defmodule LastFm.Feed do def all_artists do m = [ { - {:"$1", %{artist: :"$2"}}, + {:"$1", %{scrobbled_at_label: :"$2", artist: :"$3"}}, [], - [:"$2"] + [%{scrobbled_at_uts: :"$1", scrobbled_at_label: :"$2", artist: :"$3"}] } ] - :ets.select_reverse(__MODULE__, m) |> Enum.uniq() + :ets.select_reverse(__MODULE__, m) |> Enum.uniq_by(fn pair -> pair.artist end) end @spec all_albums() :: [LastFm.Album.t()] def all_albums do m = [ { - {:"$1", %{album: :"$2"}}, + {:"$1", %{scrobbled_at_label: :"$2", album: :"$3", cover_url: :"$4"}}, [], - [:"$2"] + [%{scrobbled_at_uts: :"$1", scrobbled_at_label: :"$2", album: :"$3", cover_url: :"$4"}] } ] - :ets.select_reverse(__MODULE__, m) |> Enum.uniq() + :ets.select_reverse(__MODULE__, m) |> Enum.uniq_by(fn pair -> pair.album end) end @spec subscribe() :: :ok diff --git a/test/last_fm/feed_test.exs b/test/last_fm/feed_test.exs index 2a7a6864..50acaaad 100644 --- a/test/last_fm/feed_test.exs +++ b/test/last_fm/feed_test.exs @@ -49,12 +49,28 @@ defmodule LastFm.FeedTest do test "it returns artists in descending order of scrobble" do :ok = Feed.update([@track_two, @track_one]) - assert [@track_one.artist] == Feed.all_artists() + + assert [ + %{ + artist: @track_two.artist, + scrobbled_at_uts: @track_two.scrobbled_at_uts, + scrobbled_at_label: @track_two.scrobbled_at_label + } + ] == + 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() + + assert [ + %{ + album: @track_two.album, + cover_url: @track_two.cover_url, + scrobbled_at_uts: @track_two.scrobbled_at_uts, + scrobbled_at_label: @track_two.scrobbled_at_label + } + ] == Feed.all_albums() end end end