Charts: count by artists and count by genre
Generated via Cursor and Claude 3.5 - initial version
This commit is contained in:
@@ -267,4 +267,38 @@ defmodule MusicLibrary.Records do
|
||||
def change_record(%Record{} = record, attrs \\ %{}) do
|
||||
Record.changeset(record, attrs)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of tuples containing artist names and their record count,
|
||||
ordered by count in descending order.
|
||||
"""
|
||||
def count_records_by_artist do
|
||||
q =
|
||||
from r in fragment("""
|
||||
select json_extract(artist.value, '$.name') as name, count(1) as count
|
||||
from records, json_each(records.artists) artist
|
||||
group by name
|
||||
order by count desc
|
||||
"""),
|
||||
select: {fragment("name"), fragment("count")}
|
||||
|
||||
Repo.all(q)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of tuples containing genre names and their record count,
|
||||
ordered by count in descending order.
|
||||
"""
|
||||
def count_records_by_genre do
|
||||
q =
|
||||
from r in fragment("""
|
||||
select genre.value as name, count(1) as count
|
||||
from records, json_each(records.genres) genre
|
||||
group by name
|
||||
order by count desc
|
||||
"""),
|
||||
select: {fragment("name"), fragment("count")}
|
||||
|
||||
Repo.all(q)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user