From da9934ae1e65d7e62e0c46b2cae14c386e1f81e0 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Wed, 2 Jul 2025 23:27:39 -0400 Subject: [PATCH] Create disambiguation for songs This was way easier than I expected, but I am rather unhappy with some things now. In particular, the GET page is pretty gross. I think there are some things I can do, but I'm not 100% confident. Maybe I'll bring some things up to bob once I have a better picture, but I really want to try to clean up my code --- src/app/views/songs.zig | 17 ++++++++++++----- src/app/views/songs/get.zmpl | 11 ++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index 4be4f43..24e4f86 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -24,13 +24,20 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); const id_int = blk: { const rn = try decode(request.allocator, id); - const songs = try jetzig.database.Query(.Song).select(.{.id}).where(.{ .name = rn }).all(request.repo); - if (songs.len == 0) { + // Try to find the song by name + const queried_songs = try jetzig.database.Query(.Song).select(.{.id}).where(.{ .name = rn }).all(request.repo); + if (queried_songs.len == 0) { + // Either we've been given an id in the db, or the song doesn't exist break :blk std.fmt.parseInt(i64, id, 10) catch return request.fail(.not_found); - } else if (songs.len == 1) { - break :blk songs[0].id; + } else if (queried_songs.len == 1) { + // It can only be one song + break :blk queried_songs[0].id; } else { - return request.redirect("http://127.0.0.1:8080", .found); + // It could be a variety of songs + const songs = try queries.entityQueryResult(request, queries.loadQuery(.song, .entities_by_name), .{rn}); + try root.put("songs", songs); + try root.put("disambiguation", true); + return request.render(.ok); } }; diff --git a/src/app/views/songs/get.zmpl b/src/app/views/songs/get.zmpl index 2acb7d4..e2f6b54 100644 --- a/src/app/views/songs/get.zmpl +++ b/src/app/views/songs/get.zmpl @@ -1,7 +1,7 @@ @zig { const ColumnChoices = []const enum{song, album, artist, artistlist, scrobbles, date}; const columns: ColumnChoices = &.{.song, .artistlist, .album, .date}; - const reviews = try zmpl.coerceArray(".reviews"); + const dis_columns: ColumnChoices = &.{.song, .album, .artistlist, .scrobbles}; } @@ -11,6 +11,14 @@ @partial partials/header +@if ($.disambiguation) +

Songs

+@partial partials/newtable(T: ColumnChoices, table_data: .songs, columns: dis_columns) +@else + +@zig { + const reviews = try zmpl.coerceArray(".reviews"); +}

{{.song.song_name}}

@@ -47,5 +55,6 @@ } +@end \ No newline at end of file