Use convenience functions for views

This commit is contained in:
mitteneer 2025-03-28 16:11:41 -04:00
parent cf84b4afdd
commit 29f8837f19
3 changed files with 10 additions and 22 deletions

View file

@ -5,18 +5,15 @@ const jetquery = @import("jetzig").jetquery;
pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
var albums_view = try root.put("albums", .array);
const query = jetzig.database.Query(.Album)
const albums = try jetzig.database.Query(.Album)
.select(.{ .id, .name })
.include(.albumartists, .{ .select = .{.artist_id} })
.include(.scrobbles, .{ .select = .{.id} })
.orderBy(.{ .name = .asc });
const albums = try request.repo.all(query);
.orderBy(.{ .name = .asc })
.all(request.repo);
//const albums = try request.repo.all(query);
for (albums) |album| {
//const scrobbles = try jetzig.database.Query(.Scrobble)
// .where(.{ .album_id = album.id })
// .count()
// .execute(request.repo);
var album_view = try albums_view.append(.object);
var artist_infos = try album_view.put("artist_info", .array);
@ -32,7 +29,6 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
try album_view.put("name", album.name);
try album_view.put("url", album.id);
//try album_view.put("scrobbles", scrobbles);
try album_view.put("scrobbles", (album.scrobbles).len);
}
return request.render(.ok);

View file

@ -5,16 +5,12 @@ const jetquery = @import("jetzig").jetquery;
pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
var artists_view = try root.put("artists", .array);
const query = jetzig.database.Query(.Artist)
const artists = try jetzig.database.Query(.Artist)
.select(.{ .id, .name })
.include(.scrobbleartists, .{ .select = .{.id} })
.orderBy(.{ .name = .asc });
const artists = try request.repo.all(query);
.orderBy(.{ .name = .asc })
.all(request.repo);
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);
try artist_view.put("name", artist.name);
try artist_view.put("url", artist.id);

View file

@ -4,18 +4,14 @@ const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
var songs_view = try root.put("songs", .array);
const query = jetzig.database.Query(.Song)
const songs = try jetzig.database.Query(.Song)
.select(.{ .id, .name })
.include(.songartists, .{ .select = .{.artist_id} })
.include(.scrobbles, .{ .select = .{.id} })
.orderBy(.{ .name = .asc });
const songs = try request.repo.all(query);
.orderBy(.{ .name = .asc })
.all(request.repo);
for (songs) |song| {
//const scrobbles = try jetzig.database.Query(.Scrobble)
// .where(.{ .song_id = song.id })
// .count()
// .execute(request.repo);
var song_view = try songs_view.append(.object);
var artist_infos = try song_view.put("artist_info", .array);