Move sorting to db query

This commit is contained in:
Claudio Ortolina
2024-11-01 19:04:40 +00:00
parent 893890a2e9
commit 4c57f47bca
3 changed files with 5 additions and 7 deletions
+2
View File
@@ -67,6 +67,7 @@ defmodule MusicLibrary.Records do
from r in Record,
where: not is_nil(r.purchased_at),
group_by: r.format,
order_by: [desc: count(r.id)],
select: {r.format, count(r.id)}
Repo.all(q)
@@ -77,6 +78,7 @@ defmodule MusicLibrary.Records do
from r in Record,
where: not is_nil(r.purchased_at),
group_by: r.type,
order_by: [desc: count(r.id)],
select: {r.type, count(r.id)}
Repo.all(q)
@@ -7,13 +7,9 @@ defmodule MusicLibraryWeb.StatsLive.Index do
alias Records.Record
def mount(_params, _session, socket) do
collection_count_by_format =
Records.count_records_by_format()
|> Enum.sort_by(fn {_format, count} -> count end, :desc)
collection_count_by_format = Records.count_records_by_format()
collection_count_by_type =
Records.count_records_by_type()
|> Enum.sort_by(fn {_type, count} -> count end, :desc)
collection_count_by_type = Records.count_records_by_type()
collection_count =
Enum.reduce(collection_count_by_format, 0, fn {_, count}, acc -> acc + count end)
+1 -1
View File
@@ -269,7 +269,7 @@ msgid "Something went wrong!"
msgstr ""
#: lib/music_library_web/components/layouts/app.html.heex:7
#: lib/music_library_web/live/stats_live/index.ex:28
#: lib/music_library_web/live/stats_live/index.ex:24
#, elixir-autogen, elixir-format
msgid "Stats"
msgstr ""