Implement db searches using raw sql

This commit is contained in:
mitteneer 2025-04-17 00:24:48 -04:00
parent 09d4453665
commit 27358fe217
4 changed files with 226 additions and 109 deletions

View file

@ -4,33 +4,58 @@ 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 })
.include(.song, .{ .select = .{ .id, .name } })
.include(.album, .{ .select = .{ .id, .name } })
.include(.scrobbleartists, .{ .select = .{.artist_id} })
.orderBy(.{ .date = .desc });
const scrobbles = try request.repo.all(query);
for (scrobbles) |scrobble| {
//const query = jetzig.database.Query(.Scrobble)
// .select(.{ .id, .date })
// .include(.song, .{ .select = .{ .id, .name } })
// .include(.album, .{ .select = .{ .id, .name } })
// .include(.scrobbleartists, .{ .select = .{.artist_id} })
// .orderBy(.{ .date = .desc });
//const scrobbles = try request.repo.all(query);
const query =
\\SELECT songs.name, songs.id, albums.name, albums.id, scrobbles.datetime
\\FROM albumsongs
\\INNER JOIN songs ON songs.id = albumsongs.song_id
\\INNER JOIN albums ON albums.id = albumsongs.album_id
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
\\ORDER BY scrobbles.datetime ASC
;
var scrobbles_js_result = try request.repo.executeSql(query, .{});
defer scrobbles_js_result.deinit();
const Scrobble = struct { song_name: []const u8, song_id: i32, album_name: []const u8, album_id: i32, date: i64 };
while (try scrobbles_js_result.postgresql.result.next()) |scrobble_row| {
const scrobble = try scrobble_row.to(Scrobble, .{ .dupe = true, .allocator = request.allocator });
var scrobble_view = try scrobbles_view.append(.object);
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)
.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("album_name", scrobble.album.name);
try scrobble_view.put("album_id", scrobble.album.id);
try scrobble_view.put("song_name", scrobble.song_name);
try scrobble_view.put("song_id", scrobble.song_id);
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);
}
//for (scrobbles) |scrobble| {
// var scrobble_view = try scrobbles_view.append(.object);
// 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)
// .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("album_name", scrobble.album.name);
// try scrobble_view.put("album_id", scrobble.album.id);
// try scrobble_view.put("date", scrobble.date);
//}
return request.render(.ok);
}