diff --git a/lib/music_library/wishlist.ex b/lib/music_library/wishlist.ex index 54c38555..599aa436 100644 --- a/lib/music_library/wishlist.ex +++ b/lib/music_library/wishlist.ex @@ -26,6 +26,14 @@ defmodule MusicLibrary.Wishlist do Repo.aggregate(search, :count) end + def count do + q = + from r in Record, + where: is_nil(r.purchased_at) + + Repo.aggregate(q, :count) + end + defp build_search(query) do {:ok, parsed_query} = SearchParser.parse(query) diff --git a/lib/music_library_web/controllers/stats_controller.ex b/lib/music_library_web/controllers/stats_controller.ex index 28459a98..42f71c30 100644 --- a/lib/music_library_web/controllers/stats_controller.ex +++ b/lib/music_library_web/controllers/stats_controller.ex @@ -1,7 +1,7 @@ defmodule MusicLibraryWeb.StatsController do use MusicLibraryWeb, :controller - alias MusicLibrary.Records + alias MusicLibrary.{Records, Wishlist} def index(conn, _params) do collection_count_by_format = @@ -15,6 +15,8 @@ defmodule MusicLibraryWeb.StatsController do collection_count = Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end) + wishlist_count = Wishlist.count() + latest_record = Records.get_latest_record!() conn @@ -23,6 +25,7 @@ defmodule MusicLibraryWeb.StatsController do collection_count_by_format: collection_count_by_format, collection_count_by_type: collection_count_by_type, collection_count: collection_count, + wishlist_count: wishlist_count, latest_record: latest_record, nav_section: :stats ) diff --git a/lib/music_library_web/controllers/stats_html/index.html.heex b/lib/music_library_web/controllers/stats_html/index.html.heex index cb1b48a5..d2778dc8 100644 --- a/lib/music_library_web/controllers/stats_html/index.html.heex +++ b/lib/music_library_web/controllers/stats_html/index.html.heex @@ -1,7 +1,7 @@
-
+
@@ -41,6 +41,18 @@
+
+
+

+ <%= gettext("Total wishlist") %> +

+
+
+ + <%= @wishlist_count %> + +
+
diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 548162f9..53ec9950 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -84,7 +84,7 @@ msgid "Logout" msgstr "" #: lib/music_library_web/components/layouts/app.html.heex:7 -#: lib/music_library_web/controllers/stats_controller.ex:21 +#: lib/music_library_web/controllers/stats_controller.ex:23 #, elixir-autogen, elixir-format msgid "Stats" msgstr "" @@ -324,3 +324,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Total collection" msgstr "" + +#: lib/music_library_web/controllers/stats_html/index.html.heex:47 +#, elixir-autogen, elixir-format +msgid "Total wishlist" +msgstr "" diff --git a/test/music_library_web/controllers/stats_controller_test.exs b/test/music_library_web/controllers/stats_controller_test.exs index 86cf4069..6003c29e 100644 --- a/test/music_library_web/controllers/stats_controller_test.exs +++ b/test/music_library_web/controllers/stats_controller_test.exs @@ -5,9 +5,14 @@ defmodule MusicLibraryWeb.StatsControllerTest do import MusicLibrary.RecordsFixtures - defp create_records(_) do - records = Enum.map(1..30, fn _ -> record_fixture() end) - %{records: records} + defp fill_collection(_) do + records = Enum.map(1..5, fn _ -> record_fixture() end) + %{collection: records} + end + + defp fill_wishlist(_) do + records = Enum.map(1..30, fn _ -> record_fixture(%{purchased_at: nil}) end) + %{wishlist: records} end defp escape(string) do @@ -17,23 +22,26 @@ defmodule MusicLibraryWeb.StatsControllerTest do end describe "GET /" do - setup [:create_records] + setup [:fill_collection, :fill_wishlist] - test "it shows the record counts (total, format, and type)", %{conn: conn, records: records} do + test "it shows the collection counts (total, format, and type)", %{ + conn: conn, + collection: collection + } do conn = get(conn, "/") response = html_response(conn, 200) - assert response =~ records |> length() |> Integer.to_string() + assert response =~ collection |> length() |> Integer.to_string() - records + collection |> Enum.frequencies_by(& &1.format) |> Enum.each(fn {format, count} -> assert response =~ "\n#{count}\n" assert response =~ "\n#{Record.format_long_label(format)}\n" end) - records + collection |> Enum.frequencies_by(& &1.type) |> Enum.each(fn {type, count} -> assert response =~ "\n#{count}\n" @@ -41,8 +49,8 @@ defmodule MusicLibraryWeb.StatsControllerTest do end) end - test "it shows the latest record", %{conn: conn, records: records} do - latest_record = Enum.max_by(records, & &1.purchased_at) + test "it shows the latest purchase", %{conn: conn, collection: collection} do + latest_record = Enum.max_by(collection, & &1.purchased_at) conn = get(conn, "/") @@ -52,5 +60,13 @@ defmodule MusicLibraryWeb.StatsControllerTest do assert html_response(conn, 200) =~ escape(artist.name) end end + + test "it shows the wishlist total count", %{conn: conn, wishlist: wishlist} do + conn = get(conn, "/") + + response = html_response(conn, 200) + + assert response =~ wishlist |> length() |> Integer.to_string() + end end end