diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index 2c24826..84bcde8 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -12,17 +12,21 @@ pub fn index(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.id }).count().execute(request.repo); + 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); for (album.albumartists) |artist| { var artist_info = try artist_infos.append(.object); - const artist_data = try jetzig.database.Query(.Artist).find(artist.artist_id).select(.{ .id, .name }).execute(request.repo); - if (artist_data) |ad| { - try artist_info.put("name", ad.name); - try artist_info.put("id", ad.id); - } + const artist_data = try jetzig.database.Query(.Artist) + .find(artist.artist_id) + .select(.{ .id, .name }) + .execute(request.repo); + try artist_info.put("name", artist_data.?.name); + try artist_info.put("id", artist_data.?.id); } try album_view.put("name", album.name); @@ -33,7 +37,10 @@ pub fn index(request: *jetzig.Request) !jetzig.View { } pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { - const album = try jetzig.database.Query(.Album).find(id).execute(request.repo); + const album = try jetzig.database.Query(.Album) + .find(id) + .select(.{ .id, .name }) + .execute(request.repo); var root = try request.data(.object); try root.put("album", album.?.name); var songs_view = try root.put("songs", .array); @@ -42,9 +49,13 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { .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); + 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); diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index f03da7e..f007b85 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -10,10 +10,11 @@ pub fn index(request: *jetzig.Request) !jetzig.View { .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); + 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); @@ -23,7 +24,10 @@ pub fn index(request: *jetzig.Request) !jetzig.View { } pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { - const artist = try jetzig.database.Query(.Artist).find(id).execute(request.repo); + const artist = try jetzig.database.Query(.Artist) + .find(id) + .select(.{ .id, .name }) + .execute(request.repo); var root = try request.data(.object); try root.put("artist", artist.?.name); var albums_view = try root.put("albums", .array); @@ -33,21 +37,16 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { .join(.inner, .artist) .where(.{ .artist = .{ .id = id } }); - //const query = jetzig.database.Query(.Albumartist) - // .select(.{ .artist_id, jetquery.sql.count(.album_id) }) - // .groupBy(.{.artist_id}) - // .orderBy(.count__album_id); - 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", scrobbles); - //std.log.debug("{s}", .{album.album.name}); } return request.render(.ok); } diff --git a/src/app/views/scrobbles.zig b/src/app/views/scrobbles.zig index ecc96af..ebda828 100644 --- a/src/app/views/scrobbles.zig +++ b/src/app/views/scrobbles.zig @@ -4,7 +4,8 @@ const jetzig = @import("jetzig"); pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); var scrobbles_view = try root.put("scrobbles", .array); - const query = jetzig.database.Query(.Scrobble).select(.{ .id, .date }) + const query = jetzig.database.Query(.Scrobble) + .select(.{ .id, .date }) .include(.song, .{ .select = .{ .id, .name } }) .include(.album, .{ .select = .{ .id, .name } }) .include(.scrobbleartists, .{ .select = .{.artist_id} }) @@ -16,17 +17,16 @@ pub fn index(request: *jetzig.Request) !jetzig.View { var artist_infos = try scrobble_view.put("artist_info", .array); for (scrobble.scrobbleartists) |artist| { var artist_info = try artist_infos.append(.object); - const artist_data = try jetzig.database.Query(.Artist).where(.{ .id = artist.artist_id }).all(request.repo); - for (artist_data) |ad| { - try artist_info.put("name", ad.name); - try artist_info.put("id", ad.id); - } + const artist_data = try jetzig.database.Query(.Artist) + .find(artist.artist_id) + .select(.{ .id, .name }) + .execute(request.repo); + try artist_info.put("name", artist_data.?.name); + try artist_info.put("id", artist_data.?.id); } try scrobble_view.put("song_name", scrobble.song.name); try scrobble_view.put("song_id", scrobble.song.id); - //try scrobble_view.put("artist_name", "placeholder"); - //try scrobble_view.put("artist_id", "placeholder"); try scrobble_view.put("album_name", scrobble.album.name); try scrobble_view.put("album_id", scrobble.album.id); try scrobble_view.put("date", scrobble.date); diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index a44ed20..2064433 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -11,17 +11,21 @@ pub fn index(request: *jetzig.Request) !jetzig.View { const songs = try request.repo.all(query); for (songs) |song| { - const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.id }).count().execute(request.repo); + 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); for (song.songartists) |artist| { var artist_info = try artist_infos.append(.object); - const artist_data = try jetzig.database.Query(.Artist).find(artist.artist_id).select(.{ .id, .name }).execute(request.repo); - if (artist_data) |ad| { - try artist_info.put("name", ad.name); - try artist_info.put("id", ad.id); - } + const artist_data = try jetzig.database.Query(.Artist) + .find(artist.artist_id) + .select(.{ .id, .name }) + .execute(request.repo); + try artist_info.put("name", artist_data.?.name); + try artist_info.put("id", artist_data.?.id); } try song_view.put("name", song.name); try song_view.put("url", song.id);