Include wishlist count in stats page
This commit is contained in:
@@ -26,6 +26,14 @@ defmodule MusicLibrary.Wishlist do
|
|||||||
Repo.aggregate(search, :count)
|
Repo.aggregate(search, :count)
|
||||||
end
|
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
|
defp build_search(query) do
|
||||||
{:ok, parsed_query} = SearchParser.parse(query)
|
{:ok, parsed_query} = SearchParser.parse(query)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
defmodule MusicLibraryWeb.StatsController do
|
defmodule MusicLibraryWeb.StatsController do
|
||||||
use MusicLibraryWeb, :controller
|
use MusicLibraryWeb, :controller
|
||||||
|
|
||||||
alias MusicLibrary.Records
|
alias MusicLibrary.{Records, Wishlist}
|
||||||
|
|
||||||
def index(conn, _params) do
|
def index(conn, _params) do
|
||||||
collection_count_by_format =
|
collection_count_by_format =
|
||||||
@@ -15,6 +15,8 @@ defmodule MusicLibraryWeb.StatsController do
|
|||||||
collection_count =
|
collection_count =
|
||||||
Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end)
|
Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end)
|
||||||
|
|
||||||
|
wishlist_count = Wishlist.count()
|
||||||
|
|
||||||
latest_record = Records.get_latest_record!()
|
latest_record = Records.get_latest_record!()
|
||||||
|
|
||||||
conn
|
conn
|
||||||
@@ -23,6 +25,7 @@ defmodule MusicLibraryWeb.StatsController do
|
|||||||
collection_count_by_format: collection_count_by_format,
|
collection_count_by_format: collection_count_by_format,
|
||||||
collection_count_by_type: collection_count_by_type,
|
collection_count_by_type: collection_count_by_type,
|
||||||
collection_count: collection_count,
|
collection_count: collection_count,
|
||||||
|
wishlist_count: wishlist_count,
|
||||||
latest_record: latest_record,
|
latest_record: latest_record,
|
||||||
nav_section: :stats
|
nav_section: :stats
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<div>
|
<div>
|
||||||
<dl class="mt-5 grid grid-cols-1 gap-5 sm:grid-cols-3">
|
<dl class="mt-5 grid grid-cols-2 gap-5 sm:grid-cols-5">
|
||||||
<div
|
<div
|
||||||
class="relative overflow-hidden rounded-lg bg-white px-4 pb-3 pt-5 shadow sm:px-6 sm:pt-6 sm:col-span-2 cursor-pointer"
|
class="relative overflow-hidden rounded-lg bg-white px-4 pb-3 pt-5 shadow sm:px-6 sm:pt-6 col-span-2 sm:col-span-3 cursor-pointer"
|
||||||
phx-click={JS.navigate(~p"/records/#{@latest_record}")}
|
phx-click={JS.navigate(~p"/records/#{@latest_record}")}
|
||||||
>
|
>
|
||||||
<dt>
|
<dt>
|
||||||
@@ -41,6 +41,18 @@
|
|||||||
</a>
|
</a>
|
||||||
</dd>
|
</dd>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="overflow-hidden rounded-lg bg-white px-4 pb-3 pt-5 shadow sm:px-6 sm:pt-6">
|
||||||
|
<dt>
|
||||||
|
<p class="truncate text-sm font-medium text-gray-500">
|
||||||
|
<%= gettext("Total wishlist") %>
|
||||||
|
</p>
|
||||||
|
</dt>
|
||||||
|
<dd class="flex items-baseline pb-6 sm:pb-7">
|
||||||
|
<a href={~p"/wishlist"} class="text-2xl font-semibold text-gray-900 hover:text-gray-500">
|
||||||
|
<%= @wishlist_count %>
|
||||||
|
</a>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ msgid "Logout"
|
|||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: lib/music_library_web/components/layouts/app.html.heex:7
|
#: 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
|
#, elixir-autogen, elixir-format
|
||||||
msgid "Stats"
|
msgid "Stats"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@@ -324,3 +324,8 @@ msgstr ""
|
|||||||
#, elixir-autogen, elixir-format
|
#, elixir-autogen, elixir-format
|
||||||
msgid "Total collection"
|
msgid "Total collection"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: lib/music_library_web/controllers/stats_html/index.html.heex:47
|
||||||
|
#, elixir-autogen, elixir-format
|
||||||
|
msgid "Total wishlist"
|
||||||
|
msgstr ""
|
||||||
|
|||||||
@@ -5,9 +5,14 @@ defmodule MusicLibraryWeb.StatsControllerTest do
|
|||||||
|
|
||||||
import MusicLibrary.RecordsFixtures
|
import MusicLibrary.RecordsFixtures
|
||||||
|
|
||||||
defp create_records(_) do
|
defp fill_collection(_) do
|
||||||
records = Enum.map(1..30, fn _ -> record_fixture() end)
|
records = Enum.map(1..5, fn _ -> record_fixture() end)
|
||||||
%{records: records}
|
%{collection: records}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fill_wishlist(_) do
|
||||||
|
records = Enum.map(1..30, fn _ -> record_fixture(%{purchased_at: nil}) end)
|
||||||
|
%{wishlist: records}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp escape(string) do
|
defp escape(string) do
|
||||||
@@ -17,23 +22,26 @@ defmodule MusicLibraryWeb.StatsControllerTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /" do
|
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, "/")
|
conn = get(conn, "/")
|
||||||
|
|
||||||
response = html_response(conn, 200)
|
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.frequencies_by(& &1.format)
|
||||||
|> Enum.each(fn {format, count} ->
|
|> Enum.each(fn {format, count} ->
|
||||||
assert response =~ "\n#{count}\n"
|
assert response =~ "\n#{count}\n"
|
||||||
assert response =~ "\n#{Record.format_long_label(format)}\n"
|
assert response =~ "\n#{Record.format_long_label(format)}\n"
|
||||||
end)
|
end)
|
||||||
|
|
||||||
records
|
collection
|
||||||
|> Enum.frequencies_by(& &1.type)
|
|> Enum.frequencies_by(& &1.type)
|
||||||
|> Enum.each(fn {type, count} ->
|
|> Enum.each(fn {type, count} ->
|
||||||
assert response =~ "\n#{count}\n"
|
assert response =~ "\n#{count}\n"
|
||||||
@@ -41,8 +49,8 @@ defmodule MusicLibraryWeb.StatsControllerTest do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it shows the latest record", %{conn: conn, records: records} do
|
test "it shows the latest purchase", %{conn: conn, collection: collection} do
|
||||||
latest_record = Enum.max_by(records, & &1.purchased_at)
|
latest_record = Enum.max_by(collection, & &1.purchased_at)
|
||||||
|
|
||||||
conn = get(conn, "/")
|
conn = get(conn, "/")
|
||||||
|
|
||||||
@@ -52,5 +60,13 @@ defmodule MusicLibraryWeb.StatsControllerTest do
|
|||||||
assert html_response(conn, 200) =~ escape(artist.name)
|
assert html_response(conn, 200) =~ escape(artist.name)
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user