Add scrobble count to each artist/album/song view

This commit is contained in:
mitteneer 2025-03-25 15:44:40 -04:00
parent 7ee3abe857
commit 4adbd59e12
7 changed files with 32 additions and 16 deletions

View file

@ -206,13 +206,20 @@ pub const Albumsong = jetquery.Model(
},
);
pub const Scrobbleartist = jetquery.Model(@This(), "Scrobbleartists", struct {
id: i32,
scrobble_id: i32,
artist_id: i32,
created_at: jetquery.DateTime,
updated_at: jetquery.DateTime,
}, .{ .relations = .{
.scrobble = jetquery.belongsTo(.Scrobble, .{}),
.artist = jetquery.belongsTo(.Artist, .{}),
} });
pub const Scrobbleartist = jetquery.Model(
@This(),
"Scrobbleartists",
struct {
id: i32,
scrobble_id: i32,
artist_id: i32,
created_at: jetquery.DateTime,
updated_at: jetquery.DateTime,
},
.{
.relations = .{
.scrobble = jetquery.belongsTo(.Scrobble, .{}),
.artist = jetquery.belongsTo(.Artist, .{}),
},
},
);

View file

@ -23,9 +23,11 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
const query = jetzig.database.Query(.Albumsong).include(.song, .{ .select = .{ .name, .id } }).join(.inner, .album).where(.{ .album = .{ .id = id } });
const songs = try request.repo.all(query);
for (songs) |song| {
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.song.id }).count().execute(request.repo);
var song_view = try songs_view.append(.object);
try song_view.put("name", song.song.name);
try song_view.put("url", song.song.id);
try song_view.put("scrobbles", scrobbles);
}
return request.render(.ok);
}

View file

@ -12,6 +12,7 @@
@for (.songs) |song| {
<tr>
<td class=cell><a href="/songs/{{song.url}}">{{song.name}}</a></td>
<td class=cell>{{song.scrobbles}}</td>
</tr>
}
</table>

View file

@ -8,11 +8,13 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
const query = jetzig.database.Query(.Artist).select(.{}).orderBy(.{ .name = .asc });
const artists = try request.repo.all(query);
for (artists) |artist| {
const scrobbles = try jetzig.database.Query(.Scrobbleartist).where(.{ .artist_id = artist.id }).count().execute(request.repo);
var artist_view = try artists_view.append(.object);
//const output = try request.allocator.dupe(u8, artist.name);
//std.mem.replaceScalar(u8, output, ' ', '_');
try artist_view.put("name", artist.name);
try artist_view.put("url", artist.id);
try artist_view.put("scrobbles", scrobbles);
}
return request.render(.ok);
@ -23,7 +25,10 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
try root.put("artist", artist.?.name);
var albums_view = try root.put("albums", .array);
const query = jetzig.database.Query(.Albumartist).include(.album, .{ .select = .{ .name, .id } }).join(.inner, .artist).where(.{ .artist = .{ .id = id } });
const query = jetzig.database.Query(.Albumartist)
.include(.album, .{ .select = .{ .name, .id } })
.join(.inner, .artist)
.where(.{ .artist = .{ .id = id } });
//const query = jetzig.database.Query(.Albumartist)
// .select(.{ .artist_id, jetquery.sql.count(.album_id) })
@ -32,13 +37,13 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
const albums = try request.repo.all(query);
for (albums) |album| {
//const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .album_id = album.album.id }).count().execute(request.repo);
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .album_id = album.album.id }).count().execute(request.repo);
var album_view = try albums_view.append(.object);
try album_view.put("name", album.album.name);
try album_view.put("url", album.album.id);
//try album_view.put("name", album.count__album_id);
//try album_view.put("url", album.count__album_id);
try album_view.put("scrobbles", 6);
try album_view.put("scrobbles", scrobbles);
//std.log.debug("{s}", .{album.album.name});
}
return request.render(.ok);

View file

@ -12,7 +12,7 @@
@for (.albums) |album| {
<tr>
<td class=cell><a href="/albums/{{album.url}}">{{album.name}}</a></td>
<td calss=cell>{{album.scrobbles}}</td>
<td class=cell>{{album.scrobbles}}</td>
</tr>
}
</table>

View file

@ -12,6 +12,7 @@
@for (.artists) |artist| {
<tr>
<td class=cell><a href="/artists/{{artist.url}}">{{artist.name}}</a></td>
<td class=cell>{{artist.scrobbles}}</td>
</tr>
}
</table>