Split entity_items and appears query into more granular queries

We can be a bit more specific about the information we get this way
This commit is contained in:
mitteneer 2025-06-06 14:28:15 -04:00
parent adcaff34ea
commit 3ef17fcd46
4 changed files with 69 additions and 32 deletions

View file

@ -20,7 +20,7 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
const album = try queries.entityQueryResult(request, queries.loadQuery(.album, .entity_info), .{id});
try root.put("album", album);
const songs = try queries.entityQueryResult(request, queries.loadQuery(.album, .entity_items), .{id});
const songs = try queries.entityQueryResult(request, queries.loadQuery(.album, .get_songs), .{id});
try root.put("songs", songs);
const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.album, .firstlast), .{id});

View file

@ -21,7 +21,7 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
const artist = try queries.entityQueryResult(request, queries.loadQuery(.artist, .entity_info), .{id});
try root.put("artist", artist);
const albums = try queries.entityQueryResult(request, queries.loadQuery(.artist, .entity_items), .{id});
const albums = try queries.entityQueryResult(request, queries.loadQuery(.artist, .get_albums), .{id});
try root.put("albums", albums);
const appears = try queries.entityQueryResult(request, queries.loadQuery(.artist, .appears), .{id});

View file

@ -17,11 +17,11 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
const song = try queries.entityQueryResult(request, queries.loadQuery(.song, .entity_info), .{id});
try root.put("song", song);
const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .entity_items), .{id});
const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_scrobbles), .{id});
try root.put("scrobbles", scrobbles);
const appears = try queries.entityQueryResult(request, queries.loadQuery(.song, .appears), .{id});
try root.put("appears", appears);
const albums = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_albums), .{id});
try root.put("albums", albums);
const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.song, .firstlast), .{id});
try root.put("firstlast", firstlast);

View file

@ -55,7 +55,7 @@ pub fn entityQueryResult(request: *jetzig.Request, query: GeneratedQuery, args:
}
const EntityType = enum { scrobble, song, album, artist };
const QueryTypeEnum = enum { firstlast, timescale, entities, entity_items, appears, entity_info, datestreak };
const QueryTypeEnum = enum { firstlast, timescale, entities, get_songs, get_albums, get_scrobbles, appears, entity_info, datestreak };
const GeneratedQuery = struct {
entity: EntityType,
@ -246,20 +246,9 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
},
.appears =>
//.ResultType = AppearsResult,
// Not sure how I feel about this one
switch (entity) {
.scrobble, .album => unreachable,
.song =>
\\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles
\\FROM artistalbums
\\INNER JOIN albums ON albums.id = artistalbums.album_id
\\INNER JOIN albumsongs ON albumsongs.album_id = albums.id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id
\\WHERE albumsongs.song_id = $1
\\GROUP BY albums.id
\\ORDER BY scrobbles DESC;
,
.scrobble, .song, .album => unreachable,
.artist =>
\\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles
\\FROM artistalbums
@ -273,19 +262,8 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
,
},
.entity_items =>
//.ResultType = EntityItemsResult,
switch (entity) {
.scrobble => unreachable,
.song =>
\\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date
\\FROM albumsongs
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\WHERE songs.id = $1
\\ORDER BY date ASC
,
.get_songs => switch (entity) {
.scrobble, .song => unreachable, // Might be able to use this with SongGroups?
.album =>
\\SELECT songs.name AS song_name, songs.id AS song_id, COUNT(scrobbles) AS scrobbles
\\FROM albumsongs
@ -296,6 +274,34 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
\\ORDER BY scrobbles DESC
,
.artist =>
\\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles
\\FROM albumsongs
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\WHERE albumsongsartists.artist_id = $1
\\GROUP BY songs.id, albums.id
\\ORDER BY scrobbles DESC
,
},
.get_albums =>
//.ResultType = EntityItemsResult,
switch (entity) {
.scrobble, .album => unreachable, // Might be able to use this with ReleaseGroups?
.song =>
\\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles
\\FROM artistalbums
\\INNER JOIN albums ON albums.id = artistalbums.album_id
\\INNER JOIN albumsongs ON albumsongs.album_id = albums.id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id
\\WHERE albumsongs.song_id = $1
\\GROUP BY albums.id
\\ORDER BY scrobbles DESC
,
.artist =>
\\SELECT albums.name AS album_name, albums.id AS album_id, COUNT(scrobbles) AS scrobbles
\\FROM artistalbums
\\INNER JOIN albums ON albums.id = artistalbums.album_id
@ -307,6 +313,37 @@ pub fn loadQuery(entity: EntityType, query_type: QueryTypeEnum) GeneratedQuery {
,
},
.get_scrobbles => switch (entity) {
.scrobble => unreachable,
.song =>
\\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date
\\FROM albumsongs
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\WHERE songs.id = $1
\\ORDER BY date ASC
,
.album =>
\\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date
\\FROM albumsongs
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\WHERE albums.id = $1
\\ORDER BY date ASC
,
.artist =>
\\SELECT songs.name AS song_name, songs.id AS song_id, albums.name AS album_name, albums.id AS album_id, TO_CHAR(scrobbles.datetime, 'YYYY-MM-DD HH24:MI:SS') AS date
\\FROM albumsongs
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\WHERE artists.id = $1
\\ORDER BY date ASC
,
},
.entity_info => switch (entity) {
.scrobble => unreachable,
.song =>