Include artist(s) name in album GET view

This also makes the entity_info struct very similar to the UnifiedResult struct, so we'll probably see a merge at some point. Would be nice if we used the fields from the entity_info result more commonly.
This commit is contained in:
mitteneer 2025-05-31 15:48:30 -04:00
parent 906ba6d2e5
commit 566edf1818
4 changed files with 29 additions and 12 deletions

View file

@ -9,7 +9,8 @@
</head>
<body>
@partial partials/header
<h1>{{.album.name}}</h1>
<h1>{{.album.album_name}}</h1>
<h2><a href="/artists/{{.album.artist_id}}">{{.album.artist_name}}</a></h2>
<div>{{.album.scrobbles}} scrobbles ({{.album.rank}} place)</div>
<div>{{.album.song_num}} songs</div>
@partial partials/firstlast_listens(firstlast: .firstlast)

View file

@ -9,7 +9,7 @@
</head>
<body>
@partial partials/header
<h1>{{.artist.name}}</h1>
<h1>{{.artist.artist_name}}</h1>
<div>
<div>{{.artist.scrobbles}} scrobbles ({{.artist.rank}} place)</div>
<div>{{.artist.song_num}} songs</div>

View file

@ -9,7 +9,7 @@
</head>
<body>
@partial partials/header
<h1>{{.song.name}}</h1>
<h1>{{.song.song_name}}</h1>
<div>{{.song.scrobbles}} scrobbles ({{.song.rank}} place)</div>
@partial partials/firstlast_listens(firstlast: .firstlast)
<h3>Yearly Performance</h3>

View file

@ -17,7 +17,7 @@ pub fn entityQueryResult(request: *jetzig.Request, query: GeneratedQuery, args:
if (query.query_type == .entity_info) {
var out: *jetzig.Data.Value = try Data.object();
const entity = try (try result.next()).?.to(struct { name: []const u8, id: i32, scrobbles: i64, album_num: ?i64 = null, song_num: ?i64 = null, rank: []const u8 }, .{ .dupe = true, .allocator = request.allocator, .map = .name });
const entity = try (try result.next()).?.to(EntityInfoResult, .{ .dupe = true, .allocator = request.allocator, .map = .name });
try out.put("entity_info", entity);
try result.drain();
return out.get("entity_info").?;
@ -74,6 +74,20 @@ const UnifiedResult = struct {
date: ?[]const u8 = null,
};
const EntityInfoResult = struct {
album_name: ?[]const u8 = null,
album_id: ?i32 = null,
song_name: ?[]const u8 = null,
song_id: ?i32 = null,
artist_name: ?[]const u8 = null,
artist_id: ?i32 = null,
scrobbles: ?i64 = null,
date: ?[]const u8 = null,
rank: []const u8,
song_num: ?i64 = null,
album_num: ?i64 = null,
};
pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
return GeneratedQuery{
.entity = entity,
@ -301,28 +315,30 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
.song =>
\\SELECT * FROM
\\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM99999th') AS rank FROM
\\(SELECT songs.name AS name, songs.id AS id, COUNT(scrobbles) AS scrobbles
\\(SELECT songs.name AS song_name, songs.id AS song_id, COUNT(scrobbles) AS scrobbles
\\FROM albumsongs
\\INNER JOIN songs ON albumsongs.song_id = songs.id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\GROUP BY songs.id))
\\WHERE id = $1
\\WHERE song_id = $1
,
.album =>
\\SELECT * FROM
\\SELECT album_name, t.album_id, artists.name AS artist_name, artists.id AS artist_id, song_num, scrobbles, rank FROM
\\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM9999th') AS rank FROM
\\(SELECT albums.name AS name, albums.id AS id, COUNT(DISTINCT songs.id) AS song_num, COUNT(scrobbles) AS scrobbles
\\(SELECT albums.name AS album_name, albums.id AS album_id, COUNT(DISTINCT songs.id) AS song_num, COUNT(scrobbles) AS scrobbles
\\FROM albumsongs
\\INNER JOIN albums ON albumsongs.album_id = albums.id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\GROUP BY albums.id))
\\WHERE id = $1;
\\GROUP BY albums.id)) AS t
\\INNER JOIN artistalbums ON artistalbums.album_id = t.album_id
\\INNER JOIN artists ON artists.id = artistalbums.artist_id
\\WHERE t.album_id = $1
,
.artist =>
\\SELECT * FROM
\\(SELECT *, TO_CHAR(ROW_NUMBER() OVER (ORDER BY scrobbles DESC),'FM9999th') AS rank FROM
\\(SELECT artists.name AS name, artists.id AS id, COUNT(scrobbles) AS scrobbles, COUNT(DISTINCT albums.id) AS album_num, COUNT(DISTINCT songs.id) AS song_num
\\(SELECT artists.name AS artist_name, artists.id AS artist_id, COUNT(scrobbles) AS scrobbles, COUNT(DISTINCT albums.id) AS album_num, COUNT(DISTINCT songs.id) AS song_num
\\FROM albumsongsartists
\\INNER JOIN artists ON albumsongsartists.artist_id = artists.id
\\INNER JOIN albumsongs ON albumsongsartists.albumsong_id = albumsongs.id
@ -330,7 +346,7 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\GROUP BY artists.id))
\\WHERE id = $1
\\WHERE artist_id = $1
,
},