From 570ce4b14569607e05a1f35a64b375cf1171ae31 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sat, 22 Feb 2025 10:34:56 +0000 Subject: [PATCH] Can count records by genre --- lib/music_library/collection.ex | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index 198188df..625fb87b 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -71,6 +71,16 @@ defmodule MusicLibrary.Collection do q |> Repo.all() end + def count_records_by_genre do + q = + from r in fragment("records, json_each(records.genres)"), + group_by: r.value, + order_by: [desc: count(r.value)], + select: %{genre: r.value, count: count(r.value)} + + q |> Repo.all() + end + defp base_search do from r in SearchIndex, where: not is_nil(r.purchased_at)